From fd1717d03bf3fb5b44e49d9d53bd2fb7236937e1 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 26 Feb 2002 19:40:28 +0000 Subject: [PATCH] * Changes to compile successfully with GCC 3.0 * Eliminated memory leak in processGraph * Pass vectors by const reference to moveDummyCode instead of by copy git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1808 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Instrumentation/ProfilePaths/Graph.cpp | 19 ++- .../ProfilePaths/GraphAuxiliary.cpp | 144 +++++++----------- .../ProfilePaths/GraphAuxillary.cpp | 144 +++++++----------- .../ProfilePaths/ProfilePaths.cpp | 10 +- 4 files changed, 132 insertions(+), 185 deletions(-) diff --git a/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp b/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp index b3e3ca1c86d..206af1f682d 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp @@ -1,13 +1,20 @@ //===--Graph.cpp--- implements Graph class ---------------- ------*- C++ -*--=// // // This implements Graph for helping in trace generation -// This graph gets used by "PathProfile" class +// This graph gets used by "ProfilePaths" class // //===----------------------------------------------------------------------===// #include "Graph.h" #include "llvm/BasicBlock.h" #include +#include + +using std::list; +using std::set; +using std::map; +using std::vector; +using std::cerr; static const graphListElement *findNodeInList(const Graph::nodeList &NL, Node *N) { @@ -180,8 +187,8 @@ struct compare_nodes { }; -void printNode(Node *nd){ - cerr<<"Node:"<getElement()->getName()<getElement()->getName()<<"\n"; } //Get the Maximal spanning tree (also a graph) @@ -233,7 +240,7 @@ Graph* Graph::getMaxSpanningTree(){ while(!vt.empty()){ Node *u=*(min_element(vt.begin(), vt.end(), compare_nodes())); #ifdef DEBUG_PATH_PROFILES - cerr<<"popped wt"<<(u)->getWeight()<getWeight()<<"\n"; printNode(u); #endif if(parent[u]!=NULL){ //so not root @@ -270,8 +277,8 @@ Graph* Graph::getMaxSpanningTree(){ } } #ifdef DEBUG_PATH_PROFILES - cerr<<"wt:v->wt"<getWeight()<wt"<getWeight()<<"\n"; + printNode(v);cerr<<"node wt:"<<(*v).weight<<"\n"; #endif //so if v in in vt, change wt(v) to wt(u->v) //only if wt(u->v) +#include + +using std::list; +using std::map; +using std::vector; +using std::cerr; //check if 2 edges are equal (same endpoints and same weight) static bool edgesEqual(Edge ed1, Edge ed2); @@ -35,17 +41,16 @@ static map getEdgeIncrements(Graph& g, Graph& t); //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 map* -getCodeInsertions(Graph &g, - vector &chords, - map &edIncrements); +static void getCodeInsertions(Graph &g, map &Insertions, + vector &chords, + map &edIncrements); //Move the incoming dummy edge code and outgoing dummy //edge code over to the corresponding back edge -static void moveDummyCode(vector stDummy, - vector exDummy, - vector be, - map &insertions); +static void moveDummyCode(const vector &stDummy, + const vector &exDummy, + const vector &be, + map &insertions); @@ -162,7 +167,7 @@ void processGraph(Graph &g, 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<second<<"\n"; } #endif @@ -172,10 +177,11 @@ void processGraph(Graph &g, //the kind of code to be inserted along an edge //The idea here is to minimize the computation //by inserting only the needed code - map* codeInsertions; - vector chords; + vector chords; getChords(chords, g, *t); - codeInsertions=getCodeInsertions(g,chords,increment); + + map codeInsertions; + getCodeInsertions(g, codeInsertions, chords,increment); #ifdef DEBUG_PATH_PROFILES //print edges with code for debugging @@ -183,7 +189,7 @@ void processGraph(Graph &g, 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()<second->getCond()<<":"<second->getInc()<<"\n"; } cerr<<"-----end insertions\n"; #endif @@ -191,24 +197,24 @@ void processGraph(Graph &g, //Move the incoming dummy edge code and outgoing dummy //edge code over to the corresponding back edge - moveDummyCode(stDummy, exDummy, be, *codeInsertions); + moveDummyCode(stDummy, exDummy, be, codeInsertions); #ifdef DEBUG_PATH_PROFILES //debugging info cerr<<"After moving dummy code\n"; - for(map::iterator cd_i=codeInsertions->begin(), - cd_e=codeInsertions->end(); cd_i!=cd_e; ++cd_i){ + 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()<second->getInc()<<"\n"; } cerr<<"Dummy end------------\n"; #endif //see what it looks like... //now insert code along edges which have codes on them - for(map::iterator MI=codeInsertions->begin(), - ME=codeInsertions->end(); MI!=ME; ++MI){ + for(map::iterator MI=codeInsertions.begin(), + ME=codeInsertions.end(); MI!=ME; ++MI){ Edge ed=MI->first; insertBB(ed, MI->second, rInst, countInst); } @@ -406,20 +412,16 @@ static map getEdgeIncrements(Graph& g, Graph& t){ //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 map* -getCodeInsertions(Graph &g, - vector &chords, - map &edIncrements){ - //map of instrumented edges that's returned in the end - map *instr= - new map; +static void getCodeInsertions(Graph &g, map &instr, + vector &chords, + map &edIncrements){ //Register initialization code vector ws; ws.push_back(g.getRoot()); while(ws.size()>0){ - Node *v=ws[0]; - ws.erase(ws.begin()); + Node *v=ws.back(); + ws.pop_back(); //for each edge v->w Graph::nodeList succs=g.getNodeList(v); @@ -441,7 +443,7 @@ getCodeInsertions(Graph &g, getEdgeCode *edCd=new getEdgeCode(); edCd->setCond(1); edCd->setInc(edIncrements[ed]); - (*instr)[ed]=edCd; + instr[ed]=edCd; } else if((g.getPredNodes(w)).size()==1){ ws.push_back(w); @@ -450,7 +452,7 @@ getCodeInsertions(Graph &g, getEdgeCode *edCd=new getEdgeCode(); edCd->setCond(2); edCd->setInc(0); - (*instr)[ed]=edCd; + instr[ed]=edCd; } } } @@ -458,9 +460,9 @@ getCodeInsertions(Graph &g, /////Memory increment code ws.push_back(g.getExit()); - while(ws.size()>0){ - Node *w=ws[0]; - ws.erase(&ws[0]); + while(!ws.empty()) { + Node *w=ws.back(); + ws.pop_back(); //for each edge v->w list preds=g.getPredNodes(w); @@ -480,13 +482,13 @@ getCodeInsertions(Graph &g, } if(hasEdge){ char str[100]; - if((*instr)[ed]!=NULL && (*instr)[ed]->getCond()==1){ - (*instr)[ed]->setCond(4); + if(instr[ed]!=NULL && instr[ed]->getCond()==1){ + instr[ed]->setCond(4); } else{ edCd->setCond(5); edCd->setInc(edIncrements[ed]); - (*instr)[ed]=edCd; + instr[ed]=edCd; } } @@ -494,7 +496,7 @@ getCodeInsertions(Graph &g, ws.push_back(v); else{ edCd->setCond(6); - (*instr)[ed]=edCd; + instr[ed]=edCd; } } } @@ -502,14 +504,12 @@ getCodeInsertions(Graph &g, ///// Register increment code for(vector::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; ++CI){ getEdgeCode *edCd=new getEdgeCode(); - if((*instr)[*CI]==NULL){ - edCd->setCond(3); - edCd->setInc(edIncrements[*CI]); - (*instr)[*CI]=edCd; - } + if(instr[*CI]==NULL){ + edCd->setCond(3); + edCd->setInc(edIncrements[*CI]); + instr[*CI]=edCd; + } } - - return instr; } //Add dummy edges corresponding to the back edges @@ -518,7 +518,7 @@ getCodeInsertions(Graph &g, //and outgoing dummy edge is a->exit void addDummyEdges(vector &stDummy, vector &exDummy, - Graph &g, vector be){ + Graph &g, vector &be){ for(vector::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){ Edge ed=*VI; Node *first=ed.getFirst(); @@ -529,45 +529,15 @@ void addDummyEdges(vector &stDummy, Edge *st=new Edge(g.getRoot(), second); //check if stDummy doesn't have it already - bool hasIt=false; - - if(find(stDummy.begin(), stDummy.end(), *st)!=stDummy.end()) - hasIt=true; - - /* - for(vector::iterator DM=stDummy.begin(), DE=stDummy.end(); DM!=DE; - ++DM){ - if(*DM==*st){ - hasIt=true; - break; - } - } - */ - - if(!hasIt){ + if(find(stDummy.begin(), stDummy.end(), *st) == stDummy.end()) stDummy.push_back(*st); - g.addEdgeForce(*st); - } + g.addEdgeForce(*st); } if(!(*first==*(g.getExit()))){ Edge *ex=new Edge(first, g.getExit()); - bool hasIt=false; - if(find(exDummy.begin(), exDummy.end(), *ex)!=exDummy.end()) - hasIt=true; - - /* - for(vector::iterator DM=exDummy.begin(), DE=exDummy.end(); DM!=DE; - ++DM){ - if(*DM==*ex){ - hasIt=true; - break; - } - } - */ - - if(!hasIt){ + if (find(exDummy.begin(), exDummy.end(), *ex) == exDummy.end()) { exDummy.push_back(*ex); g.addEdgeForce(*ex); } @@ -580,16 +550,16 @@ void printEdge(Edge ed){ cerr<<((ed.getFirst())->getElement()) ->getName()<<"->"<<((ed.getSecond()) ->getElement())->getName()<< - ":"< stDummy, - vector exDummy, - vector be, - map &insertions){ - typedef vector::iterator vec_iter; +static void moveDummyCode(const vector &stDummy, + const vector &exDummy, + const vector &be, + map &insertions){ + typedef vector::const_iterator vec_iter; #ifdef DEBUG_PATH_PROFILES //print all back, st and ex dummy @@ -605,7 +575,7 @@ static void moveDummyCode(vector stDummy, cerr<<"------end all edges\n"; #endif - std::vector toErase; + std::vector toErase; for(map::iterator MI=insertions.begin(), ME=insertions.end(); MI!=ME; ++MI){ Edge ed=MI->first; @@ -682,7 +652,7 @@ static void moveDummyCode(vector stDummy, } #ifdef DEBUG_PATH_PROFILES - cerr<<"size of deletions: "<::iterator vmi=toErase.begin(), vme=toErase.end(); vmi!=vme; @@ -690,7 +660,7 @@ static void moveDummyCode(vector stDummy, insertions.erase(*vmi); #ifdef DEBUG_PATH_PROFILES - cerr<<"SIZE OF INSERTIONS AFTER DEL "< +#include + +using std::list; +using std::map; +using std::vector; +using std::cerr; //check if 2 edges are equal (same endpoints and same weight) static bool edgesEqual(Edge ed1, Edge ed2); @@ -35,17 +41,16 @@ static map getEdgeIncrements(Graph& g, Graph& t); //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 map* -getCodeInsertions(Graph &g, - vector &chords, - map &edIncrements); +static void getCodeInsertions(Graph &g, map &Insertions, + vector &chords, + map &edIncrements); //Move the incoming dummy edge code and outgoing dummy //edge code over to the corresponding back edge -static void moveDummyCode(vector stDummy, - vector exDummy, - vector be, - map &insertions); +static void moveDummyCode(const vector &stDummy, + const vector &exDummy, + const vector &be, + map &insertions); @@ -162,7 +167,7 @@ void processGraph(Graph &g, 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<second<<"\n"; } #endif @@ -172,10 +177,11 @@ void processGraph(Graph &g, //the kind of code to be inserted along an edge //The idea here is to minimize the computation //by inserting only the needed code - map* codeInsertions; - vector chords; + vector chords; getChords(chords, g, *t); - codeInsertions=getCodeInsertions(g,chords,increment); + + map codeInsertions; + getCodeInsertions(g, codeInsertions, chords,increment); #ifdef DEBUG_PATH_PROFILES //print edges with code for debugging @@ -183,7 +189,7 @@ void processGraph(Graph &g, 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()<second->getCond()<<":"<second->getInc()<<"\n"; } cerr<<"-----end insertions\n"; #endif @@ -191,24 +197,24 @@ void processGraph(Graph &g, //Move the incoming dummy edge code and outgoing dummy //edge code over to the corresponding back edge - moveDummyCode(stDummy, exDummy, be, *codeInsertions); + moveDummyCode(stDummy, exDummy, be, codeInsertions); #ifdef DEBUG_PATH_PROFILES //debugging info cerr<<"After moving dummy code\n"; - for(map::iterator cd_i=codeInsertions->begin(), - cd_e=codeInsertions->end(); cd_i!=cd_e; ++cd_i){ + 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()<second->getInc()<<"\n"; } cerr<<"Dummy end------------\n"; #endif //see what it looks like... //now insert code along edges which have codes on them - for(map::iterator MI=codeInsertions->begin(), - ME=codeInsertions->end(); MI!=ME; ++MI){ + for(map::iterator MI=codeInsertions.begin(), + ME=codeInsertions.end(); MI!=ME; ++MI){ Edge ed=MI->first; insertBB(ed, MI->second, rInst, countInst); } @@ -406,20 +412,16 @@ static map getEdgeIncrements(Graph& g, Graph& t){ //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 map* -getCodeInsertions(Graph &g, - vector &chords, - map &edIncrements){ - //map of instrumented edges that's returned in the end - map *instr= - new map; +static void getCodeInsertions(Graph &g, map &instr, + vector &chords, + map &edIncrements){ //Register initialization code vector ws; ws.push_back(g.getRoot()); while(ws.size()>0){ - Node *v=ws[0]; - ws.erase(ws.begin()); + Node *v=ws.back(); + ws.pop_back(); //for each edge v->w Graph::nodeList succs=g.getNodeList(v); @@ -441,7 +443,7 @@ getCodeInsertions(Graph &g, getEdgeCode *edCd=new getEdgeCode(); edCd->setCond(1); edCd->setInc(edIncrements[ed]); - (*instr)[ed]=edCd; + instr[ed]=edCd; } else if((g.getPredNodes(w)).size()==1){ ws.push_back(w); @@ -450,7 +452,7 @@ getCodeInsertions(Graph &g, getEdgeCode *edCd=new getEdgeCode(); edCd->setCond(2); edCd->setInc(0); - (*instr)[ed]=edCd; + instr[ed]=edCd; } } } @@ -458,9 +460,9 @@ getCodeInsertions(Graph &g, /////Memory increment code ws.push_back(g.getExit()); - while(ws.size()>0){ - Node *w=ws[0]; - ws.erase(&ws[0]); + while(!ws.empty()) { + Node *w=ws.back(); + ws.pop_back(); //for each edge v->w list preds=g.getPredNodes(w); @@ -480,13 +482,13 @@ getCodeInsertions(Graph &g, } if(hasEdge){ char str[100]; - if((*instr)[ed]!=NULL && (*instr)[ed]->getCond()==1){ - (*instr)[ed]->setCond(4); + if(instr[ed]!=NULL && instr[ed]->getCond()==1){ + instr[ed]->setCond(4); } else{ edCd->setCond(5); edCd->setInc(edIncrements[ed]); - (*instr)[ed]=edCd; + instr[ed]=edCd; } } @@ -494,7 +496,7 @@ getCodeInsertions(Graph &g, ws.push_back(v); else{ edCd->setCond(6); - (*instr)[ed]=edCd; + instr[ed]=edCd; } } } @@ -502,14 +504,12 @@ getCodeInsertions(Graph &g, ///// Register increment code for(vector::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; ++CI){ getEdgeCode *edCd=new getEdgeCode(); - if((*instr)[*CI]==NULL){ - edCd->setCond(3); - edCd->setInc(edIncrements[*CI]); - (*instr)[*CI]=edCd; - } + if(instr[*CI]==NULL){ + edCd->setCond(3); + edCd->setInc(edIncrements[*CI]); + instr[*CI]=edCd; + } } - - return instr; } //Add dummy edges corresponding to the back edges @@ -518,7 +518,7 @@ getCodeInsertions(Graph &g, //and outgoing dummy edge is a->exit void addDummyEdges(vector &stDummy, vector &exDummy, - Graph &g, vector be){ + Graph &g, vector &be){ for(vector::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){ Edge ed=*VI; Node *first=ed.getFirst(); @@ -529,45 +529,15 @@ void addDummyEdges(vector &stDummy, Edge *st=new Edge(g.getRoot(), second); //check if stDummy doesn't have it already - bool hasIt=false; - - if(find(stDummy.begin(), stDummy.end(), *st)!=stDummy.end()) - hasIt=true; - - /* - for(vector::iterator DM=stDummy.begin(), DE=stDummy.end(); DM!=DE; - ++DM){ - if(*DM==*st){ - hasIt=true; - break; - } - } - */ - - if(!hasIt){ + if(find(stDummy.begin(), stDummy.end(), *st) == stDummy.end()) stDummy.push_back(*st); - g.addEdgeForce(*st); - } + g.addEdgeForce(*st); } if(!(*first==*(g.getExit()))){ Edge *ex=new Edge(first, g.getExit()); - bool hasIt=false; - if(find(exDummy.begin(), exDummy.end(), *ex)!=exDummy.end()) - hasIt=true; - - /* - for(vector::iterator DM=exDummy.begin(), DE=exDummy.end(); DM!=DE; - ++DM){ - if(*DM==*ex){ - hasIt=true; - break; - } - } - */ - - if(!hasIt){ + if (find(exDummy.begin(), exDummy.end(), *ex) == exDummy.end()) { exDummy.push_back(*ex); g.addEdgeForce(*ex); } @@ -580,16 +550,16 @@ void printEdge(Edge ed){ cerr<<((ed.getFirst())->getElement()) ->getName()<<"->"<<((ed.getSecond()) ->getElement())->getName()<< - ":"< stDummy, - vector exDummy, - vector be, - map &insertions){ - typedef vector::iterator vec_iter; +static void moveDummyCode(const vector &stDummy, + const vector &exDummy, + const vector &be, + map &insertions){ + typedef vector::const_iterator vec_iter; #ifdef DEBUG_PATH_PROFILES //print all back, st and ex dummy @@ -605,7 +575,7 @@ static void moveDummyCode(vector stDummy, cerr<<"------end all edges\n"; #endif - std::vector toErase; + std::vector toErase; for(map::iterator MI=insertions.begin(), ME=insertions.end(); MI!=ME; ++MI){ Edge ed=MI->first; @@ -682,7 +652,7 @@ static void moveDummyCode(vector stDummy, } #ifdef DEBUG_PATH_PROFILES - cerr<<"size of deletions: "<::iterator vmi=toErase.begin(), vme=toErase.end(); vmi!=vme; @@ -690,7 +660,7 @@ static void moveDummyCode(vector stDummy, insertions.erase(*vmi); #ifdef DEBUG_PATH_PROFILES - cerr<<"SIZE OF INSERTIONS AFTER DEL "< &st, BasicBlock *BB){ - for(set::iterator si=st.begin(); si!=st.end(); ++si){ +static Node *findBB(std::set &st, BasicBlock *BB){ + for(std::set::iterator si=st.begin(); si!=st.end(); ++si){ if(((*si)->getElement())==BB){ return *si; } @@ -95,7 +95,7 @@ bool ProfilePaths::runOnMethod(Method *M){ if (M->getBasicBlocks().size() == 1) { //The graph is made acyclic: this is done //by removing back edges for now, and adding them later on - vector be; + vector be; g.getBackEdges(be); #ifdef DEBUG_PATH_PROFILES cerr<<"Backedges:"<b (in vector stDummy) //and 2. from a->exit (in vector exDummy) - vector stDummy; - vector exDummy; + vector stDummy; + vector exDummy; addDummyEdges(stDummy, exDummy, g, be); //Now, every edge in the graph is assigned a weight