forked from OSchip/llvm-project
parent
b6d2dc48c9
commit
fd61c60864
|
@ -338,6 +338,9 @@ void insertBB(Edge ed,
|
|||
//then we need to change branch destinations to include new BB
|
||||
|
||||
//std::cerr<<"before cast!\n";
|
||||
std::cerr<<"Method no in Edgecode:"<<Methno<<"\n";
|
||||
std::cerr<<"Instruction\n";
|
||||
std::cerr<<*TI;
|
||||
BranchInst *BI = cast<BranchInst>(TI);
|
||||
|
||||
if(BI->isUnconditional()){
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Transforms/Instrumentation/Graph.h"
|
||||
#include "llvm/iTerminators.h"
|
||||
#include "llvm/BasicBlock.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
@ -50,14 +51,48 @@ Graph::Graph(std::vector<Node*> n, std::vector<Edge> e,
|
|||
|
||||
}
|
||||
|
||||
//sorting edgelist, called by backEdgeVist ONLY!!!
|
||||
Graph::nodeList &Graph::sortNodeList(Node *par, nodeList &nl){
|
||||
assert(par && "null node pointer");
|
||||
BasicBlock *bbPar = par->getElement();
|
||||
|
||||
if(nl.size()<=1) 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;
|
||||
|
||||
|
||||
TerminatorInst *tti = par->getElement()->getTerminator();
|
||||
BranchInst *ti = cast<BranchInst>(tti);
|
||||
assert(ti && "not a branch");
|
||||
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;
|
||||
*min = *NLI;
|
||||
*NLI = tmpElmnt;
|
||||
}
|
||||
return nl;
|
||||
}
|
||||
|
||||
//check whether graph has an edge
|
||||
//having an edge simply means that there is an edge in the graph
|
||||
//which has same endpoints as the given edge
|
||||
bool Graph::hasEdge(Edge ed) const{
|
||||
bool Graph::hasEdge(Edge ed){
|
||||
if(ed.isNull())
|
||||
return false;
|
||||
|
||||
nodeList nli=getNodeList(ed.getFirst());
|
||||
nodeList &nli= nodes[ed.getFirst()]; //getNodeList(ed.getFirst());
|
||||
Node *nd2=ed.getSecond();
|
||||
|
||||
return (findNodeInList(nli,nd2)!=NULL);
|
||||
|
@ -69,12 +104,12 @@ bool Graph::hasEdge(Edge ed) const{
|
|||
//having an edge simply means that there is an edge in the graph
|
||||
//which has same endpoints as the given edge
|
||||
//This function checks, moreover, that the wt of edge matches too
|
||||
bool Graph::hasEdgeAndWt(Edge ed) const{
|
||||
bool Graph::hasEdgeAndWt(Edge ed){
|
||||
if(ed.isNull())
|
||||
return false;
|
||||
|
||||
Node *nd2=ed.getSecond();
|
||||
nodeList nli=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)
|
||||
|
@ -109,6 +144,7 @@ void Graph::addEdge(Edge ed, int w){
|
|||
|
||||
//ndList.push_front(graphListElement(nd2,w, ed.getRandId()));
|
||||
ndList.push_back(graphListElement(nd2,w, ed.getRandId()));//chng
|
||||
//sortNodeList(ed.getFirst(), ndList);
|
||||
|
||||
//sort(ndList.begin(), ndList.end(), NodeListSort());
|
||||
}
|
||||
|
@ -123,6 +159,7 @@ void Graph::addEdgeForce(Edge ed){
|
|||
nodes[ed.getFirst()].push_back
|
||||
(graphListElement(ed.getSecond(), ed.getWeight(), ed.getRandId()));
|
||||
|
||||
//sortNodeList(ed.getFirst(), nodes[ed.getFirst()]);
|
||||
//sort(nodes[ed.getFirst()].begin(), nodes[ed.getFirst()].end(), NodeListSort());
|
||||
}
|
||||
|
||||
|
@ -166,10 +203,10 @@ void Graph::setWeight(Edge ed){
|
|||
|
||||
|
||||
//get the list of successor nodes
|
||||
vector<Node *> Graph::getSuccNodes(Node *nd) const {
|
||||
vector<Node *> Graph::getSuccNodes(Node *nd){
|
||||
nodeMapTy::const_iterator nli = nodes.find(nd);
|
||||
assert(nli != nodes.end() && "Node must be in nodes map");
|
||||
const nodeList &nl = nli->second;
|
||||
const nodeList &nl = getNodeList(nd);//getSortedNodeList(nd);
|
||||
|
||||
vector<Node *> lt;
|
||||
for(nodeList::const_iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI)
|
||||
|
@ -192,7 +229,7 @@ int Graph::getNumberOfOutgoingEdges(Node *nd) const {
|
|||
}
|
||||
|
||||
//get the list of predecessor nodes
|
||||
vector<Node *> Graph::getPredNodes(Node *nd) const{
|
||||
vector<Node *> Graph::getPredNodes(Node *nd){
|
||||
vector<Node *> lt;
|
||||
for(nodeMapTy::const_iterator EI=nodes.begin(), EE=nodes.end(); EI!=EE ;++EI){
|
||||
Node *lnode=EI->first;
|
||||
|
@ -205,7 +242,7 @@ vector<Node *> Graph::getPredNodes(Node *nd) const{
|
|||
}
|
||||
|
||||
//get the number of predecessor nodes
|
||||
int Graph::getNumberOfIncomingEdges(Node *nd) const{
|
||||
int Graph::getNumberOfIncomingEdges(Node *nd){
|
||||
int count=0;
|
||||
for(nodeMapTy::const_iterator EI=nodes.begin(), EE=nodes.end(); EI!=EE ;++EI){
|
||||
Node *lnode=EI->first;
|
||||
|
@ -371,20 +408,27 @@ void Graph::printGraph(){
|
|||
//get a list of nodes in the graph
|
||||
//in r-topological sorted order
|
||||
//note that we assumed graph to be connected
|
||||
vector<Node *> Graph::reverseTopologicalSort() const{
|
||||
vector<Node *> Graph::reverseTopologicalSort(){
|
||||
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);
|
||||
}
|
||||
|
||||
//print nodes
|
||||
//std::cerr<<"Topological sort--------\n";
|
||||
//for(vector<Node *>::iterator VI = toReturn.begin(), VE = toReturn.end();
|
||||
// VI!=VE; ++VI)
|
||||
//std::cerr<<(*VI)->getElement()->getName()<<"->";
|
||||
//std::cerr<<"\n----------------------\n";
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
//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, vector<Node *> &toReturn) const {
|
||||
void Graph::DFS_Visit(Node *nd, vector<Node *> &toReturn){
|
||||
nd->setWeight(GREY);
|
||||
vector<Node *> lt=getSuccNodes(nd);
|
||||
for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){
|
||||
|
@ -441,38 +485,48 @@ void Graph::reverseWts(){
|
|||
//have been visited
|
||||
//So we have a back edge when we meet a successor of
|
||||
//a node with smaller time, and GREY color
|
||||
void Graph::getBackEdges(vector<Edge > &be) const{
|
||||
void Graph::getBackEdges(vector<Edge > &be, map<Node *, int> &d){
|
||||
map<Node *, Color > color;
|
||||
map<Node *, int > d;
|
||||
vector<Node *> allNodes=getAllNodes();
|
||||
//map<Node *, int > d;
|
||||
//vector<Node *> allNodes=getAllNodes();
|
||||
int time=0;
|
||||
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);
|
||||
}
|
||||
//for(vector<Node *>::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);
|
||||
//}
|
||||
}
|
||||
|
||||
//helper function to get back edges: it is called by
|
||||
//the "getBackEdges" function above
|
||||
void Graph::getBackEdgesVisit(Node *u, vector<Edge > &be,
|
||||
map<Node *, Color > &color,
|
||||
map<Node *, int > &d, int &time) const{
|
||||
map<Node *, int > &d, int &time) {
|
||||
color[u]=GREY;
|
||||
time++;
|
||||
d[u]=time;
|
||||
|
||||
vector<graphListElement> succ_list=getNodeList(u);
|
||||
for(vector<graphListElement>::const_iterator vl=succ_list.begin(),
|
||||
//std::cerr<<"Node list-----\n";
|
||||
vector<graphListElement> succ_list = getSortedNodeList(u);
|
||||
|
||||
//for(vector<graphListElement>::iterator vl=succ_list.begin(),
|
||||
// ve=succ_list.end(); vl!=ve; ++vl){
|
||||
//Node *v=vl->element;
|
||||
//std::cerr<<v->getElement()->getName()<<"->";
|
||||
//}
|
||||
//std::cerr<<"\n-------- end Node list\n";
|
||||
|
||||
for(vector<graphListElement>::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);
|
||||
}
|
||||
|
||||
// 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){
|
||||
//so v is ancestor of u if time of u > time of v
|
||||
|
|
|
@ -12,19 +12,14 @@
|
|||
#include "Support/StatisticReporter.h"
|
||||
|
||||
#include <map>
|
||||
//#include <list>
|
||||
//#include <set>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "llvm/BasicBlock.h"
|
||||
|
||||
class BasicBlock;
|
||||
//class Method;
|
||||
class Module;
|
||||
//=======
|
||||
class Function;
|
||||
//>>>>>>> 1.4
|
||||
class Instruction;
|
||||
|
||||
//Class Node
|
||||
|
@ -43,7 +38,7 @@ public:
|
|||
inline bool operator<(Node& nd) const { return element<nd.element; }
|
||||
inline bool operator==(Node& nd) const { return element==nd.element; }
|
||||
};
|
||||
////////////////////////
|
||||
|
||||
|
||||
//Class Edge
|
||||
//Denotes an edge in the graph
|
||||
|
@ -102,7 +97,7 @@ public:
|
|||
|
||||
inline bool operator!=(const Edge& ed) const{return !(*this==ed);}
|
||||
};
|
||||
////////////////////////
|
||||
|
||||
|
||||
//graphListElement
|
||||
//This forms the "adjacency list element" of a
|
||||
|
@ -117,7 +112,7 @@ struct graphListElement{
|
|||
randId=rand;
|
||||
}
|
||||
};
|
||||
/////////////////////////
|
||||
|
||||
|
||||
namespace std {
|
||||
struct less<Node *> : public binary_function<Node *, Node *,bool> {
|
||||
|
@ -167,7 +162,7 @@ struct EdgeCompare{
|
|||
}
|
||||
};
|
||||
|
||||
////////////////////
|
||||
|
||||
|
||||
//this is used to color vertices
|
||||
//during DFS
|
||||
|
@ -205,7 +200,7 @@ private:
|
|||
//a private method for doing DFS traversal of graph
|
||||
//this is used in determining the reverse topological sort
|
||||
//of the graph
|
||||
void DFS_Visit(Node *nd, std::vector<Node *> &toReturn) const;
|
||||
void DFS_Visit(Node *nd, std::vector<Node *> &toReturn);
|
||||
|
||||
//Its a variation of DFS to get the backedges in the graph
|
||||
//We get back edges by associating a time
|
||||
|
@ -221,7 +216,7 @@ private:
|
|||
std::vector<Edge > &be,
|
||||
std::map<Node *, Color> &clr,
|
||||
std::map<Node *, int> &d,
|
||||
int &time) const;
|
||||
int &time);
|
||||
|
||||
public:
|
||||
typedef nodeMapTy::iterator elementIterator;
|
||||
|
@ -269,23 +264,23 @@ public:
|
|||
//having an edge simply means that there is an edge in the graph
|
||||
//which has same endpoints as the given edge
|
||||
//it may possibly have different weight though
|
||||
bool hasEdge(Edge ed) const;
|
||||
bool hasEdge(Edge ed);
|
||||
|
||||
//check whether graph has an edge, with a given wt
|
||||
bool hasEdgeAndWt(Edge ed) const;
|
||||
bool hasEdgeAndWt(Edge ed);
|
||||
|
||||
//get the list of successor nodes
|
||||
std::vector<Node *> getSuccNodes(Node *nd) const;
|
||||
std::vector<Node *> getSuccNodes(Node *nd);
|
||||
|
||||
//get the number of outgoing edges
|
||||
int getNumberOfOutgoingEdges(Node *nd) const;
|
||||
|
||||
//get the list of predecessor nodes
|
||||
std::vector<Node *> getPredNodes(Node *nd) const;
|
||||
std::vector<Node *> getPredNodes(Node *nd);
|
||||
|
||||
|
||||
//to get the no of incoming edges
|
||||
int getNumberOfIncomingEdges(Node *nd) const;
|
||||
int getNumberOfIncomingEdges(Node *nd);
|
||||
|
||||
//get the list of all the vertices in graph
|
||||
std::vector<Node *> getAllNodes() const;
|
||||
|
@ -294,7 +289,7 @@ public:
|
|||
//get a list of nodes in the graph
|
||||
//in r-topological sorted order
|
||||
//note that we assumed graph to be connected
|
||||
std::vector<Node *> reverseTopologicalSort() const;
|
||||
std::vector<Node *> reverseTopologicalSort();
|
||||
|
||||
//reverse the sign of weights on edges
|
||||
//this way, max-spanning tree could be obtained
|
||||
|
@ -312,7 +307,9 @@ public:
|
|||
void printGraph();
|
||||
|
||||
//get a vector of back edges in the graph
|
||||
void getBackEdges(std::vector<Edge> &be) const;
|
||||
void getBackEdges(std::vector<Edge> &be, std::map<Node *, int> &d);
|
||||
|
||||
nodeList &sortNodeList(Node *par, nodeList &nl);
|
||||
|
||||
//Get the Maximal spanning tree (also a graph)
|
||||
//of the graph
|
||||
|
@ -321,18 +318,18 @@ public:
|
|||
//get the nodeList adjacent to a node
|
||||
//a nodeList element contains a node, and the weight
|
||||
//corresponding to the edge for that element
|
||||
inline const nodeList &getNodeList(Node *nd) const {
|
||||
constElementIterator nli = nodes.find(nd);
|
||||
assert(nli != nodes.end() && "Node must be in nodes map");
|
||||
return nli->second;
|
||||
}
|
||||
|
||||
inline nodeList &getNodeList(Node *nd) {
|
||||
elementIterator nli = nodes.find(nd);
|
||||
assert(nli != nodes.end() && "Node must be in nodes map");
|
||||
return nli->second;
|
||||
return nodes[nd];//sortNodeList(nd, nli->second);
|
||||
}
|
||||
|
||||
|
||||
nodeList &getSortedNodeList(Node *nd) {
|
||||
elementIterator nli = nodes.find(nd);
|
||||
assert(nli != nodes.end() && "Node must be in nodes map");
|
||||
return sortNodeList(nd, nodes[nd]);
|
||||
}
|
||||
|
||||
//get the root of the graph
|
||||
inline Node *getRoot() {return strt; }
|
||||
inline Node * const getRoot() const {return strt; }
|
||||
|
@ -409,12 +406,8 @@ public:
|
|||
|
||||
//get the code to be inserted on the edge
|
||||
//This is determined from cond (1-6)
|
||||
//<<<<<<< Graph.h
|
||||
void getCode(Instruction *a, Instruction *b, Function *M, BasicBlock *BB,
|
||||
int numPaths, int MethNo);
|
||||
//=======
|
||||
//void getCode(Instruction *a, Instruction *b, Function *F, BasicBlock *BB);
|
||||
//>>>>>>> 1.4
|
||||
};
|
||||
|
||||
|
||||
|
@ -426,7 +419,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);
|
||||
void processGraph(Graph &g, Instruction *rInst, Instruction *countInst, std::vector<Edge> &be, std::vector<Edge> &stDummy, std::vector<Edge> &exDummy, int n, int MethNo);
|
||||
|
||||
//print the graph (for debugging)
|
||||
void printGraph(Graph &g);
|
||||
|
@ -457,7 +450,7 @@ void addDummyEdges(std::vector<Edge> &stDummy, std::vector<Edge> &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);
|
||||
int valueAssignmentToEdges(Graph& g, std::map<Node *, int> nodePriority);
|
||||
|
||||
void getBBtrace(std::vector<BasicBlock *> &vBB, int pathNo, Function *M);
|
||||
#endif
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "llvm/BasicBlock.h"
|
||||
#include "llvm/InstrTypes.h"
|
||||
#include "llvm/Transforms/Instrumentation/Graph.h"
|
||||
#include "llvm/iTerminators.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
@ -68,7 +69,7 @@ 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){
|
||||
int valueAssignmentToEdges(Graph& g, map<Node *, int> nodePriority){
|
||||
vector<Node *> revtop=g.reverseTopologicalSort();
|
||||
map<Node *,int > NumPaths;
|
||||
for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
|
||||
|
@ -83,31 +84,36 @@ int valueAssignmentToEdges(Graph& g){
|
|||
|
||||
int sz=nlist.size();
|
||||
|
||||
//printing BB list
|
||||
//std::cerr<<"node list------------\n";
|
||||
//for(Graph::nodeList::iterator NLI=nlist.begin(), NLE=nlist.end();
|
||||
// NLI!=NLE; ++NLI)
|
||||
//std::cerr<<NLI->element->getElement()->getName()<<"->";
|
||||
|
||||
//std::cerr<<"\n-----------\n";
|
||||
|
||||
for(int i=0;i<sz-1; i++){
|
||||
int min=i;
|
||||
for(int j=i+1; j<sz; j++){
|
||||
BasicBlock *bb1 = nlist[j].element->getElement();
|
||||
BasicBlock *bb2 = nlist[min].element->getElement();
|
||||
assert(bb1->getParent() == bb2->getParent() &&
|
||||
"BBs with diff parents");
|
||||
TerminatorInst *ti = bb1->getTerminator();
|
||||
|
||||
if(bb1 == bb2) continue;
|
||||
|
||||
if(*RI == g.getRoot()){
|
||||
assert(nodePriority[nlist[min].element]!=
|
||||
nodePriority[nlist[j].element]
|
||||
&& "priorities can't be same!");
|
||||
|
||||
if(nodePriority[nlist[j].element] <
|
||||
nodePriority[nlist[min].element])
|
||||
min = j;
|
||||
}
|
||||
|
||||
//compare the order of BBs in the terminator instruction
|
||||
for(int x=0, y = ti->getNumSuccessors(); x < y; x++){
|
||||
if(ti->getSuccessor(x) == bb1){ //bb1 occurs first
|
||||
else{
|
||||
TerminatorInst *tti = (*RI)->getElement()->getTerminator();
|
||||
//std::cerr<<*tti<<std::endl;
|
||||
BranchInst *ti = cast<BranchInst>(tti);
|
||||
assert(ti && "not a branch");
|
||||
assert(ti->getNumSuccessors()==2 && "less successors!");
|
||||
|
||||
BasicBlock *tB = ti->getSuccessor(0);
|
||||
BasicBlock *fB = ti->getSuccessor(1);
|
||||
|
||||
if(tB == bb1 || fB == bb2)
|
||||
min = j;
|
||||
break;
|
||||
}
|
||||
if(ti->getSuccessor(x) == bb2) //bb2 occurs first
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -117,11 +123,14 @@ int valueAssignmentToEdges(Graph& g){
|
|||
}
|
||||
|
||||
//sorted now!
|
||||
//std::cerr<<"Considering Order-----\n";
|
||||
for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
|
||||
GLI!=GLE; ++GLI){
|
||||
//std::cerr<<GLI->element->getElement()->getName()<<"->";
|
||||
GLI->weight=NumPaths[*RI];
|
||||
NumPaths[*RI]+=NumPaths[GLI->element];
|
||||
}
|
||||
//std::cerr<<"\nend order $$$$$$$$$$$$$$$$$$$$$$$$\n";
|
||||
}
|
||||
}
|
||||
return NumPaths[g.getRoot()];
|
||||
|
@ -474,10 +483,8 @@ void processGraph(Graph &g,
|
|||
vector<Edge >& be,
|
||||
vector<Edge >& stDummy,
|
||||
vector<Edge >& exDummy,
|
||||
int numPaths){
|
||||
int numPaths, int MethNo){
|
||||
|
||||
static int MethNo=-1;
|
||||
MethNo++;
|
||||
//Given a graph: with exit->root edge, do the following in seq:
|
||||
//1. get back edges
|
||||
//2. insert dummy edges and remove back edges
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "llvm/BasicBlock.h"
|
||||
#include "llvm/InstrTypes.h"
|
||||
#include "llvm/Transforms/Instrumentation/Graph.h"
|
||||
#include "llvm/iTerminators.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
@ -68,7 +69,7 @@ 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){
|
||||
int valueAssignmentToEdges(Graph& g, map<Node *, int> nodePriority){
|
||||
vector<Node *> revtop=g.reverseTopologicalSort();
|
||||
map<Node *,int > NumPaths;
|
||||
for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
|
||||
|
@ -83,31 +84,36 @@ int valueAssignmentToEdges(Graph& g){
|
|||
|
||||
int sz=nlist.size();
|
||||
|
||||
//printing BB list
|
||||
//std::cerr<<"node list------------\n";
|
||||
//for(Graph::nodeList::iterator NLI=nlist.begin(), NLE=nlist.end();
|
||||
// NLI!=NLE; ++NLI)
|
||||
//std::cerr<<NLI->element->getElement()->getName()<<"->";
|
||||
|
||||
//std::cerr<<"\n-----------\n";
|
||||
|
||||
for(int i=0;i<sz-1; i++){
|
||||
int min=i;
|
||||
for(int j=i+1; j<sz; j++){
|
||||
BasicBlock *bb1 = nlist[j].element->getElement();
|
||||
BasicBlock *bb2 = nlist[min].element->getElement();
|
||||
assert(bb1->getParent() == bb2->getParent() &&
|
||||
"BBs with diff parents");
|
||||
TerminatorInst *ti = bb1->getTerminator();
|
||||
|
||||
if(bb1 == bb2) continue;
|
||||
|
||||
if(*RI == g.getRoot()){
|
||||
assert(nodePriority[nlist[min].element]!=
|
||||
nodePriority[nlist[j].element]
|
||||
&& "priorities can't be same!");
|
||||
|
||||
if(nodePriority[nlist[j].element] <
|
||||
nodePriority[nlist[min].element])
|
||||
min = j;
|
||||
}
|
||||
|
||||
//compare the order of BBs in the terminator instruction
|
||||
for(int x=0, y = ti->getNumSuccessors(); x < y; x++){
|
||||
if(ti->getSuccessor(x) == bb1){ //bb1 occurs first
|
||||
else{
|
||||
TerminatorInst *tti = (*RI)->getElement()->getTerminator();
|
||||
//std::cerr<<*tti<<std::endl;
|
||||
BranchInst *ti = cast<BranchInst>(tti);
|
||||
assert(ti && "not a branch");
|
||||
assert(ti->getNumSuccessors()==2 && "less successors!");
|
||||
|
||||
BasicBlock *tB = ti->getSuccessor(0);
|
||||
BasicBlock *fB = ti->getSuccessor(1);
|
||||
|
||||
if(tB == bb1 || fB == bb2)
|
||||
min = j;
|
||||
break;
|
||||
}
|
||||
if(ti->getSuccessor(x) == bb2) //bb2 occurs first
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -117,11 +123,14 @@ int valueAssignmentToEdges(Graph& g){
|
|||
}
|
||||
|
||||
//sorted now!
|
||||
//std::cerr<<"Considering Order-----\n";
|
||||
for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
|
||||
GLI!=GLE; ++GLI){
|
||||
//std::cerr<<GLI->element->getElement()->getName()<<"->";
|
||||
GLI->weight=NumPaths[*RI];
|
||||
NumPaths[*RI]+=NumPaths[GLI->element];
|
||||
}
|
||||
//std::cerr<<"\nend order $$$$$$$$$$$$$$$$$$$$$$$$\n";
|
||||
}
|
||||
}
|
||||
return NumPaths[g.getRoot()];
|
||||
|
@ -474,10 +483,8 @@ void processGraph(Graph &g,
|
|||
vector<Edge >& be,
|
||||
vector<Edge >& stDummy,
|
||||
vector<Edge >& exDummy,
|
||||
int numPaths){
|
||||
int numPaths, int MethNo){
|
||||
|
||||
static int MethNo=-1;
|
||||
MethNo++;
|
||||
//Given a graph: with exit->root edge, do the following in seq:
|
||||
//1. get back edges
|
||||
//2. insert dummy edges and remove back edges
|
||||
|
|
|
@ -70,10 +70,12 @@ bool ProfilePaths::runOnFunction(Function &F){
|
|||
|
||||
static int mn = -1;
|
||||
|
||||
if(F.size() <=1) {
|
||||
if(F.isExternal()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//std::cerr<<"Instrumenting\n-----------------\n";
|
||||
//std::cerr<<F;
|
||||
//increment counter for instrumented functions. mn is now function#
|
||||
mn++;
|
||||
|
||||
|
@ -118,18 +120,25 @@ bool ProfilePaths::runOnFunction(Function &F){
|
|||
|
||||
Graph g(nodes,edges, startNode, exitNode);
|
||||
|
||||
#ifdef DEBUG_PATH_PROFILES
|
||||
std::cerr<<"Original graph\n";
|
||||
printGraph(g);
|
||||
#endif
|
||||
//#ifdef DEBUG_PATH_PROFILES
|
||||
//std::cerr<<"Original graph\n";
|
||||
//printGraph(g);
|
||||
//#endif
|
||||
|
||||
BasicBlock *fr = &F.front();
|
||||
|
||||
// The graph is made acyclic: this is done
|
||||
// by removing back edges for now, and adding them later on
|
||||
vector<Edge> be;
|
||||
g.getBackEdges(be);
|
||||
|
||||
std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
|
||||
g.getBackEdges(be, nodePriority);
|
||||
/*
|
||||
std::cerr<<"Node priority--------------\n";
|
||||
for(std::map<Node *, int>::iterator MI = nodePriority.begin(),
|
||||
ME = nodePriority.end(); MI!=ME; ++MI)
|
||||
std::cerr<<MI->first->getElement()->getName()<<"->"<<MI->second<<"\n";
|
||||
std::cerr<<"End Node priority--------------\n";
|
||||
*/
|
||||
//std::cerr<<"BackEdges-------------\n";
|
||||
// for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
|
||||
//printEdge(*VI);
|
||||
|
@ -159,8 +168,9 @@ bool ProfilePaths::runOnFunction(Function &F){
|
|||
// All paths for now are acyclic,
|
||||
// since no back edges in the graph now
|
||||
// numPaths is the number of acyclic paths in the graph
|
||||
int numPaths=valueAssignmentToEdges(g);
|
||||
int numPaths=valueAssignmentToEdges(g, nodePriority);
|
||||
|
||||
if(numPaths<=1 || numPaths >5000) return false;
|
||||
//std::cerr<<"Numpaths="<<numPaths<<std::endl;
|
||||
//printGraph(g);
|
||||
//create instruction allocation r and count
|
||||
|
@ -186,33 +196,7 @@ bool ProfilePaths::runOnFunction(Function &F){
|
|||
//get increments along different paths,
|
||||
//and assign "increments" and "updates" (to r and count)
|
||||
//"optimally". Finally, insert llvm code along various edges
|
||||
processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths);
|
||||
/*
|
||||
//get the paths
|
||||
static std::ofstream to("paths.sizes");
|
||||
static std::ofstream bbs("paths.look");
|
||||
assert(to && "Cannot open file\n");
|
||||
assert(bbs && "Cannot open file\n");
|
||||
for(int i=0;i<numPaths; ++i){
|
||||
std::vector<BasicBlock *> vBB;
|
||||
|
||||
getBBtrace(vBB, i, M);
|
||||
//get total size of vector
|
||||
int size=0;
|
||||
bbs<<"Meth:"<<mn<<" Path:"<<i<<"\n-------------\n";
|
||||
for(vector<BasicBlock *>::iterator VBI=vBB.begin(); VBI!=vBB.end();
|
||||
++VBI){
|
||||
BasicBlock *BB=*VBI;
|
||||
size+=BB->size();
|
||||
if(BB==M->front())
|
||||
size-=numPaths;
|
||||
bbs<<BB->getName()<<"->";
|
||||
}
|
||||
bbs<<"\n--------------\n";
|
||||
to<<"::::: "<<mn<<" "<<i<<" "<<size<<"\n";
|
||||
}
|
||||
*/
|
||||
//}
|
||||
|
||||
processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn);
|
||||
|
||||
return true; // Always modifies function
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue