mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-09-25 17:20:48 +00:00
minor corrections
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2971 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -338,6 +338,9 @@ void insertBB(Edge ed,
|
|||||||
//then we need to change branch destinations to include new BB
|
//then we need to change branch destinations to include new BB
|
||||||
|
|
||||||
//std::cerr<<"before cast!\n";
|
//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);
|
BranchInst *BI = cast<BranchInst>(TI);
|
||||||
|
|
||||||
if(BI->isUnconditional()){
|
if(BI->isUnconditional()){
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "llvm/Transforms/Instrumentation/Graph.h"
|
#include "llvm/Transforms/Instrumentation/Graph.h"
|
||||||
|
#include "llvm/iTerminators.h"
|
||||||
#include "llvm/BasicBlock.h"
|
#include "llvm/BasicBlock.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#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
|
//check whether graph has an edge
|
||||||
//having an edge simply means that there is an edge in the graph
|
//having an edge simply means that there is an edge in the graph
|
||||||
//which has same endpoints as the given edge
|
//which has same endpoints as the given edge
|
||||||
bool Graph::hasEdge(Edge ed) const{
|
bool Graph::hasEdge(Edge ed){
|
||||||
if(ed.isNull())
|
if(ed.isNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
nodeList nli=getNodeList(ed.getFirst());
|
nodeList &nli= nodes[ed.getFirst()]; //getNodeList(ed.getFirst());
|
||||||
Node *nd2=ed.getSecond();
|
Node *nd2=ed.getSecond();
|
||||||
|
|
||||||
return (findNodeInList(nli,nd2)!=NULL);
|
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
|
//having an edge simply means that there is an edge in the graph
|
||||||
//which has same endpoints as the given edge
|
//which has same endpoints as the given edge
|
||||||
//This function checks, moreover, that the wt of edge matches too
|
//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())
|
if(ed.isNull())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Node *nd2=ed.getSecond();
|
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)
|
for(nodeList::iterator NI=nli.begin(), NE=nli.end(); NI!=NE; ++NI)
|
||||||
if(*NI->element == *nd2 && ed.getWeight()==NI->weight)
|
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_front(graphListElement(nd2,w, ed.getRandId()));
|
||||||
ndList.push_back(graphListElement(nd2,w, ed.getRandId()));//chng
|
ndList.push_back(graphListElement(nd2,w, ed.getRandId()));//chng
|
||||||
|
//sortNodeList(ed.getFirst(), ndList);
|
||||||
|
|
||||||
//sort(ndList.begin(), ndList.end(), NodeListSort());
|
//sort(ndList.begin(), ndList.end(), NodeListSort());
|
||||||
}
|
}
|
||||||
@@ -123,6 +159,7 @@ void Graph::addEdgeForce(Edge ed){
|
|||||||
nodes[ed.getFirst()].push_back
|
nodes[ed.getFirst()].push_back
|
||||||
(graphListElement(ed.getSecond(), ed.getWeight(), ed.getRandId()));
|
(graphListElement(ed.getSecond(), ed.getWeight(), ed.getRandId()));
|
||||||
|
|
||||||
|
//sortNodeList(ed.getFirst(), nodes[ed.getFirst()]);
|
||||||
//sort(nodes[ed.getFirst()].begin(), nodes[ed.getFirst()].end(), NodeListSort());
|
//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
|
//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);
|
nodeMapTy::const_iterator nli = nodes.find(nd);
|
||||||
assert(nli != nodes.end() && "Node must be in nodes map");
|
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;
|
vector<Node *> lt;
|
||||||
for(nodeList::const_iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI)
|
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
|
//get the list of predecessor nodes
|
||||||
vector<Node *> Graph::getPredNodes(Node *nd) const{
|
vector<Node *> Graph::getPredNodes(Node *nd){
|
||||||
vector<Node *> lt;
|
vector<Node *> lt;
|
||||||
for(nodeMapTy::const_iterator EI=nodes.begin(), EE=nodes.end(); EI!=EE ;++EI){
|
for(nodeMapTy::const_iterator EI=nodes.begin(), EE=nodes.end(); EI!=EE ;++EI){
|
||||||
Node *lnode=EI->first;
|
Node *lnode=EI->first;
|
||||||
@@ -205,7 +242,7 @@ vector<Node *> Graph::getPredNodes(Node *nd) const{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//get the number of predecessor nodes
|
//get the number of predecessor nodes
|
||||||
int Graph::getNumberOfIncomingEdges(Node *nd) const{
|
int Graph::getNumberOfIncomingEdges(Node *nd){
|
||||||
int count=0;
|
int count=0;
|
||||||
for(nodeMapTy::const_iterator EI=nodes.begin(), EE=nodes.end(); EI!=EE ;++EI){
|
for(nodeMapTy::const_iterator EI=nodes.begin(), EE=nodes.end(); EI!=EE ;++EI){
|
||||||
Node *lnode=EI->first;
|
Node *lnode=EI->first;
|
||||||
@@ -371,20 +408,27 @@ void Graph::printGraph(){
|
|||||||
//get a list of nodes in the graph
|
//get a list of nodes in the graph
|
||||||
//in r-topological sorted order
|
//in r-topological sorted order
|
||||||
//note that we assumed graph to be connected
|
//note that we assumed graph to be connected
|
||||||
vector<Node *> Graph::reverseTopologicalSort() const{
|
vector<Node *> Graph::reverseTopologicalSort(){
|
||||||
vector <Node *> toReturn;
|
vector <Node *> toReturn;
|
||||||
vector<Node *> lt=getAllNodes();
|
vector<Node *> lt=getAllNodes();
|
||||||
for(vector<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)->getWeight()!=GREY && (*LI)->getWeight()!=BLACK)
|
if((*LI)->getWeight()!=GREY && (*LI)->getWeight()!=BLACK)
|
||||||
DFS_Visit(*LI, toReturn);
|
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;
|
return toReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
//a private method for doing DFS traversal of graph
|
//a private method for doing DFS traversal of graph
|
||||||
//this is used in determining the reverse topological sort
|
//this is used in determining the reverse topological sort
|
||||||
//of the graph
|
//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);
|
nd->setWeight(GREY);
|
||||||
vector<Node *> lt=getSuccNodes(nd);
|
vector<Node *> lt=getSuccNodes(nd);
|
||||||
for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){
|
for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){
|
||||||
@@ -441,38 +485,48 @@ void Graph::reverseWts(){
|
|||||||
//have been visited
|
//have been visited
|
||||||
//So we have a back edge when we meet a successor of
|
//So we have a back edge when we meet a successor of
|
||||||
//a node with smaller time, and GREY color
|
//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 *, Color > color;
|
||||||
map<Node *, int > d;
|
//map<Node *, int > d;
|
||||||
vector<Node *> allNodes=getAllNodes();
|
//vector<Node *> allNodes=getAllNodes();
|
||||||
int time=0;
|
int time=0;
|
||||||
for(vector<Node *>::const_iterator NI=allNodes.begin(), NE=allNodes.end();
|
//for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end();
|
||||||
NI!=NE; ++NI){
|
// NI!=NE; ++NI){
|
||||||
if(color[*NI]!=GREY && color[*NI]!=BLACK)
|
//if(color[*NI]!=GREY && color[*NI]!=BLACK)
|
||||||
getBackEdgesVisit(*NI, be, color, d, time);
|
//printGraph();
|
||||||
}
|
getBackEdgesVisit(getRoot(), be, color, d, time);//*NI, be, color, d, time);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
//helper function to get back edges: it is called by
|
//helper function to get back edges: it is called by
|
||||||
//the "getBackEdges" function above
|
//the "getBackEdges" function above
|
||||||
void Graph::getBackEdgesVisit(Node *u, vector<Edge > &be,
|
void Graph::getBackEdgesVisit(Node *u, vector<Edge > &be,
|
||||||
map<Node *, Color > &color,
|
map<Node *, Color > &color,
|
||||||
map<Node *, int > &d, int &time) const{
|
map<Node *, int > &d, int &time) {
|
||||||
color[u]=GREY;
|
color[u]=GREY;
|
||||||
time++;
|
time++;
|
||||||
d[u]=time;
|
d[u]=time;
|
||||||
|
|
||||||
vector<graphListElement> succ_list=getNodeList(u);
|
//std::cerr<<"Node list-----\n";
|
||||||
for(vector<graphListElement>::const_iterator vl=succ_list.begin(),
|
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){
|
ve=succ_list.end(); vl!=ve; ++vl){
|
||||||
Node *v=vl->element;
|
Node *v=vl->element;
|
||||||
// for(vector<Node *>::const_iterator v=succ_list.begin(), ve=succ_list.end();
|
// for(vector<Node *>::const_iterator v=succ_list.begin(), ve=succ_list.end();
|
||||||
// v!=ve; ++v){
|
// v!=ve; ++v){
|
||||||
|
|
||||||
if(color[v]!=GREY && color[v]!=BLACK){
|
if(color[v]!=GREY && color[v]!=BLACK){
|
||||||
getBackEdgesVisit(v, be, color, d, time);
|
getBackEdgesVisit(v, be, color, d, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
//now checking for d and f vals
|
//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
|
//so v is ancestor of u if time of u > time of v
|
||||||
|
@@ -12,19 +12,14 @@
|
|||||||
#include "Support/StatisticReporter.h"
|
#include "Support/StatisticReporter.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
//#include <list>
|
|
||||||
//#include <set>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
#include "llvm/BasicBlock.h"
|
#include "llvm/BasicBlock.h"
|
||||||
|
|
||||||
class BasicBlock;
|
class BasicBlock;
|
||||||
//class Method;
|
|
||||||
class Module;
|
class Module;
|
||||||
//=======
|
|
||||||
class Function;
|
class Function;
|
||||||
//>>>>>>> 1.4
|
|
||||||
class Instruction;
|
class Instruction;
|
||||||
|
|
||||||
//Class Node
|
//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; }
|
||||||
inline bool operator==(Node& nd) const { return element==nd.element; }
|
inline bool operator==(Node& nd) const { return element==nd.element; }
|
||||||
};
|
};
|
||||||
////////////////////////
|
|
||||||
|
|
||||||
//Class Edge
|
//Class Edge
|
||||||
//Denotes an edge in the graph
|
//Denotes an edge in the graph
|
||||||
@@ -102,7 +97,7 @@ public:
|
|||||||
|
|
||||||
inline bool operator!=(const Edge& ed) const{return !(*this==ed);}
|
inline bool operator!=(const Edge& ed) const{return !(*this==ed);}
|
||||||
};
|
};
|
||||||
////////////////////////
|
|
||||||
|
|
||||||
//graphListElement
|
//graphListElement
|
||||||
//This forms the "adjacency list element" of a
|
//This forms the "adjacency list element" of a
|
||||||
@@ -117,7 +112,7 @@ struct graphListElement{
|
|||||||
randId=rand;
|
randId=rand;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/////////////////////////
|
|
||||||
|
|
||||||
namespace std {
|
namespace std {
|
||||||
struct less<Node *> : public binary_function<Node *, Node *,bool> {
|
struct less<Node *> : public binary_function<Node *, Node *,bool> {
|
||||||
@@ -167,7 +162,7 @@ struct EdgeCompare{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////
|
|
||||||
|
|
||||||
//this is used to color vertices
|
//this is used to color vertices
|
||||||
//during DFS
|
//during DFS
|
||||||
@@ -205,7 +200,7 @@ private:
|
|||||||
//a private method for doing DFS traversal of graph
|
//a private method for doing DFS traversal of graph
|
||||||
//this is used in determining the reverse topological sort
|
//this is used in determining the reverse topological sort
|
||||||
//of the graph
|
//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
|
//Its a variation of DFS to get the backedges in the graph
|
||||||
//We get back edges by associating a time
|
//We get back edges by associating a time
|
||||||
@@ -221,7 +216,7 @@ private:
|
|||||||
std::vector<Edge > &be,
|
std::vector<Edge > &be,
|
||||||
std::map<Node *, Color> &clr,
|
std::map<Node *, Color> &clr,
|
||||||
std::map<Node *, int> &d,
|
std::map<Node *, int> &d,
|
||||||
int &time) const;
|
int &time);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef nodeMapTy::iterator elementIterator;
|
typedef nodeMapTy::iterator elementIterator;
|
||||||
@@ -269,23 +264,23 @@ public:
|
|||||||
//having an edge simply means that there is an edge in the graph
|
//having an edge simply means that there is an edge in the graph
|
||||||
//which has same endpoints as the given edge
|
//which has same endpoints as the given edge
|
||||||
//it may possibly have different weight though
|
//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
|
//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
|
//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
|
//get the number of outgoing edges
|
||||||
int getNumberOfOutgoingEdges(Node *nd) const;
|
int getNumberOfOutgoingEdges(Node *nd) const;
|
||||||
|
|
||||||
//get the list of predecessor nodes
|
//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
|
//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
|
//get the list of all the vertices in graph
|
||||||
std::vector<Node *> getAllNodes() const;
|
std::vector<Node *> getAllNodes() const;
|
||||||
@@ -294,7 +289,7 @@ public:
|
|||||||
//get a list of nodes in the graph
|
//get a list of nodes in the graph
|
||||||
//in r-topological sorted order
|
//in r-topological sorted order
|
||||||
//note that we assumed graph to be connected
|
//note that we assumed graph to be connected
|
||||||
std::vector<Node *> reverseTopologicalSort() const;
|
std::vector<Node *> reverseTopologicalSort();
|
||||||
|
|
||||||
//reverse the sign of weights on edges
|
//reverse the sign of weights on edges
|
||||||
//this way, max-spanning tree could be obtained
|
//this way, max-spanning tree could be obtained
|
||||||
@@ -312,7 +307,9 @@ public:
|
|||||||
void printGraph();
|
void printGraph();
|
||||||
|
|
||||||
//get a vector of back edges in the graph
|
//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)
|
//Get the Maximal spanning tree (also a graph)
|
||||||
//of the graph
|
//of the graph
|
||||||
@@ -321,18 +318,18 @@ public:
|
|||||||
//get the nodeList adjacent to a node
|
//get the nodeList adjacent to a node
|
||||||
//a nodeList element contains a node, and the weight
|
//a nodeList element contains a node, and the weight
|
||||||
//corresponding to the edge for that element
|
//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) {
|
inline nodeList &getNodeList(Node *nd) {
|
||||||
elementIterator nli = nodes.find(nd);
|
elementIterator nli = nodes.find(nd);
|
||||||
assert(nli != nodes.end() && "Node must be in nodes map");
|
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
|
//get the root of the graph
|
||||||
inline Node *getRoot() {return strt; }
|
inline Node *getRoot() {return strt; }
|
||||||
inline Node * const getRoot() const {return strt; }
|
inline Node * const getRoot() const {return strt; }
|
||||||
@@ -409,12 +406,8 @@ public:
|
|||||||
|
|
||||||
//get the code to be inserted on the edge
|
//get the code to be inserted on the edge
|
||||||
//This is determined from cond (1-6)
|
//This is determined from cond (1-6)
|
||||||
//<<<<<<< Graph.h
|
|
||||||
void getCode(Instruction *a, Instruction *b, Function *M, BasicBlock *BB,
|
void getCode(Instruction *a, Instruction *b, Function *M, BasicBlock *BB,
|
||||||
int numPaths, int MethNo);
|
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,
|
//Do graph processing: to determine minimal edge increments,
|
||||||
//appropriate code insertions etc and insert the code at
|
//appropriate code insertions etc and insert the code at
|
||||||
//appropriate locations
|
//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)
|
//print the graph (for debugging)
|
||||||
void printGraph(Graph &g);
|
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
|
//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
|
//add up the edge values, we get a path number that uniquely
|
||||||
//refers to the path we travelled
|
//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);
|
void getBBtrace(std::vector<BasicBlock *> &vBB, int pathNo, Function *M);
|
||||||
#endif
|
#endif
|
||||||
|
@@ -12,6 +12,7 @@
|
|||||||
#include "llvm/BasicBlock.h"
|
#include "llvm/BasicBlock.h"
|
||||||
#include "llvm/InstrTypes.h"
|
#include "llvm/InstrTypes.h"
|
||||||
#include "llvm/Transforms/Instrumentation/Graph.h"
|
#include "llvm/Transforms/Instrumentation/Graph.h"
|
||||||
|
#include "llvm/iTerminators.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#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
|
//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
|
//add up the edge values, we get a path number that uniquely
|
||||||
//refers to the path we travelled
|
//refers to the path we travelled
|
||||||
int valueAssignmentToEdges(Graph& g){
|
int valueAssignmentToEdges(Graph& g, map<Node *, int> nodePriority){
|
||||||
vector<Node *> revtop=g.reverseTopologicalSort();
|
vector<Node *> revtop=g.reverseTopologicalSort();
|
||||||
map<Node *,int > NumPaths;
|
map<Node *,int > NumPaths;
|
||||||
for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
|
for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
|
||||||
@@ -83,31 +84,36 @@ int valueAssignmentToEdges(Graph& g){
|
|||||||
|
|
||||||
int sz=nlist.size();
|
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++){
|
for(int i=0;i<sz-1; i++){
|
||||||
int min=i;
|
int min=i;
|
||||||
for(int j=i+1; j<sz; j++){
|
for(int j=i+1; j<sz; j++){
|
||||||
BasicBlock *bb1 = nlist[j].element->getElement();
|
BasicBlock *bb1 = nlist[j].element->getElement();
|
||||||
BasicBlock *bb2 = nlist[min].element->getElement();
|
BasicBlock *bb2 = nlist[min].element->getElement();
|
||||||
assert(bb1->getParent() == bb2->getParent() &&
|
|
||||||
"BBs with diff parents");
|
if(bb1 == bb2) continue;
|
||||||
TerminatorInst *ti = bb1->getTerminator();
|
|
||||||
|
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
|
else{
|
||||||
for(int x=0, y = ti->getNumSuccessors(); x < y; x++){
|
TerminatorInst *tti = (*RI)->getElement()->getTerminator();
|
||||||
if(ti->getSuccessor(x) == bb1){ //bb1 occurs first
|
//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;
|
min = j;
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(ti->getSuccessor(x) == bb2) //bb2 occurs first
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -117,11 +123,14 @@ int valueAssignmentToEdges(Graph& g){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//sorted now!
|
//sorted now!
|
||||||
|
//std::cerr<<"Considering Order-----\n";
|
||||||
for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
|
for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
|
||||||
GLI!=GLE; ++GLI){
|
GLI!=GLE; ++GLI){
|
||||||
|
//std::cerr<<GLI->element->getElement()->getName()<<"->";
|
||||||
GLI->weight=NumPaths[*RI];
|
GLI->weight=NumPaths[*RI];
|
||||||
NumPaths[*RI]+=NumPaths[GLI->element];
|
NumPaths[*RI]+=NumPaths[GLI->element];
|
||||||
}
|
}
|
||||||
|
//std::cerr<<"\nend order $$$$$$$$$$$$$$$$$$$$$$$$\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NumPaths[g.getRoot()];
|
return NumPaths[g.getRoot()];
|
||||||
@@ -474,10 +483,8 @@ void processGraph(Graph &g,
|
|||||||
vector<Edge >& be,
|
vector<Edge >& be,
|
||||||
vector<Edge >& stDummy,
|
vector<Edge >& stDummy,
|
||||||
vector<Edge >& exDummy,
|
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:
|
//Given a graph: with exit->root edge, do the following in seq:
|
||||||
//1. get back edges
|
//1. get back edges
|
||||||
//2. insert dummy edges and remove back edges
|
//2. insert dummy edges and remove back edges
|
||||||
|
@@ -12,6 +12,7 @@
|
|||||||
#include "llvm/BasicBlock.h"
|
#include "llvm/BasicBlock.h"
|
||||||
#include "llvm/InstrTypes.h"
|
#include "llvm/InstrTypes.h"
|
||||||
#include "llvm/Transforms/Instrumentation/Graph.h"
|
#include "llvm/Transforms/Instrumentation/Graph.h"
|
||||||
|
#include "llvm/iTerminators.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#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
|
//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
|
//add up the edge values, we get a path number that uniquely
|
||||||
//refers to the path we travelled
|
//refers to the path we travelled
|
||||||
int valueAssignmentToEdges(Graph& g){
|
int valueAssignmentToEdges(Graph& g, map<Node *, int> nodePriority){
|
||||||
vector<Node *> revtop=g.reverseTopologicalSort();
|
vector<Node *> revtop=g.reverseTopologicalSort();
|
||||||
map<Node *,int > NumPaths;
|
map<Node *,int > NumPaths;
|
||||||
for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
|
for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
|
||||||
@@ -83,31 +84,36 @@ int valueAssignmentToEdges(Graph& g){
|
|||||||
|
|
||||||
int sz=nlist.size();
|
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++){
|
for(int i=0;i<sz-1; i++){
|
||||||
int min=i;
|
int min=i;
|
||||||
for(int j=i+1; j<sz; j++){
|
for(int j=i+1; j<sz; j++){
|
||||||
BasicBlock *bb1 = nlist[j].element->getElement();
|
BasicBlock *bb1 = nlist[j].element->getElement();
|
||||||
BasicBlock *bb2 = nlist[min].element->getElement();
|
BasicBlock *bb2 = nlist[min].element->getElement();
|
||||||
assert(bb1->getParent() == bb2->getParent() &&
|
|
||||||
"BBs with diff parents");
|
if(bb1 == bb2) continue;
|
||||||
TerminatorInst *ti = bb1->getTerminator();
|
|
||||||
|
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
|
else{
|
||||||
for(int x=0, y = ti->getNumSuccessors(); x < y; x++){
|
TerminatorInst *tti = (*RI)->getElement()->getTerminator();
|
||||||
if(ti->getSuccessor(x) == bb1){ //bb1 occurs first
|
//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;
|
min = j;
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(ti->getSuccessor(x) == bb2) //bb2 occurs first
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -117,11 +123,14 @@ int valueAssignmentToEdges(Graph& g){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//sorted now!
|
//sorted now!
|
||||||
|
//std::cerr<<"Considering Order-----\n";
|
||||||
for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
|
for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
|
||||||
GLI!=GLE; ++GLI){
|
GLI!=GLE; ++GLI){
|
||||||
|
//std::cerr<<GLI->element->getElement()->getName()<<"->";
|
||||||
GLI->weight=NumPaths[*RI];
|
GLI->weight=NumPaths[*RI];
|
||||||
NumPaths[*RI]+=NumPaths[GLI->element];
|
NumPaths[*RI]+=NumPaths[GLI->element];
|
||||||
}
|
}
|
||||||
|
//std::cerr<<"\nend order $$$$$$$$$$$$$$$$$$$$$$$$\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NumPaths[g.getRoot()];
|
return NumPaths[g.getRoot()];
|
||||||
@@ -474,10 +483,8 @@ void processGraph(Graph &g,
|
|||||||
vector<Edge >& be,
|
vector<Edge >& be,
|
||||||
vector<Edge >& stDummy,
|
vector<Edge >& stDummy,
|
||||||
vector<Edge >& exDummy,
|
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:
|
//Given a graph: with exit->root edge, do the following in seq:
|
||||||
//1. get back edges
|
//1. get back edges
|
||||||
//2. insert dummy edges and remove back edges
|
//2. insert dummy edges and remove back edges
|
||||||
|
@@ -70,10 +70,12 @@ bool ProfilePaths::runOnFunction(Function &F){
|
|||||||
|
|
||||||
static int mn = -1;
|
static int mn = -1;
|
||||||
|
|
||||||
if(F.size() <=1) {
|
if(F.isExternal()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//std::cerr<<"Instrumenting\n-----------------\n";
|
||||||
|
//std::cerr<<F;
|
||||||
//increment counter for instrumented functions. mn is now function#
|
//increment counter for instrumented functions. mn is now function#
|
||||||
mn++;
|
mn++;
|
||||||
|
|
||||||
@@ -118,18 +120,25 @@ bool ProfilePaths::runOnFunction(Function &F){
|
|||||||
|
|
||||||
Graph g(nodes,edges, startNode, exitNode);
|
Graph g(nodes,edges, startNode, exitNode);
|
||||||
|
|
||||||
#ifdef DEBUG_PATH_PROFILES
|
//#ifdef DEBUG_PATH_PROFILES
|
||||||
std::cerr<<"Original graph\n";
|
//std::cerr<<"Original graph\n";
|
||||||
printGraph(g);
|
//printGraph(g);
|
||||||
#endif
|
//#endif
|
||||||
|
|
||||||
BasicBlock *fr = &F.front();
|
BasicBlock *fr = &F.front();
|
||||||
|
|
||||||
// The graph is made acyclic: this is done
|
// The graph is made acyclic: this is done
|
||||||
// by removing back edges for now, and adding them later on
|
// by removing back edges for now, and adding them later on
|
||||||
vector<Edge> be;
|
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";
|
//std::cerr<<"BackEdges-------------\n";
|
||||||
// for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
|
// for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
|
||||||
//printEdge(*VI);
|
//printEdge(*VI);
|
||||||
@@ -159,8 +168,9 @@ bool ProfilePaths::runOnFunction(Function &F){
|
|||||||
// All paths for now are acyclic,
|
// All paths for now are acyclic,
|
||||||
// since no back edges in the graph now
|
// since no back edges in the graph now
|
||||||
// numPaths is the number of acyclic paths in the graph
|
// 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;
|
//std::cerr<<"Numpaths="<<numPaths<<std::endl;
|
||||||
//printGraph(g);
|
//printGraph(g);
|
||||||
//create instruction allocation r and count
|
//create instruction allocation r and count
|
||||||
@@ -186,33 +196,7 @@ bool ProfilePaths::runOnFunction(Function &F){
|
|||||||
//get increments along different paths,
|
//get increments along different paths,
|
||||||
//and assign "increments" and "updates" (to r and count)
|
//and assign "increments" and "updates" (to r and count)
|
||||||
//"optimally". Finally, insert llvm code along various edges
|
//"optimally". Finally, insert llvm code along various edges
|
||||||
processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths);
|
processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn);
|
||||||
/*
|
|
||||||
//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";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
//}
|
|
||||||
|
|
||||||
return true; // Always modifies function
|
return true; // Always modifies function
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user