static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
- Value *cnt){
+ Value *cnt, Instruction *rInst){
vector<const Type*> args;
//args.push_back(PointerType::get(Type::SByteTy));
args.push_back(Type::IntTy);
//args.push_back(Type::IntTy);
args.push_back(PointerType::get(Type::IntTy));
+ args.push_back(PointerType::get(Type::IntTy));
const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false);
+ vector<Value *> tmpVec;
+ tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+ tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+ Instruction *Idx = new GetElementPtrInst(cnt, tmpVec, "");//,
+ BB->getInstList().push_back(Idx);
+
Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
assert(trigMeth && "trigger method could not be inserted!");
trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
trargs.push_back(pathNo);
- trargs.push_back(cnt);
+ trargs.push_back(Idx);
+ trargs.push_back(rInst);
Instruction *callInst=new CallInst(trigMeth, trargs, "");//, BB->begin());
BB->getInstList().push_back(callInst);
//get the code to be inserted on the edge
//This is determined from cond (1-6)
-void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
+void getEdgeCode::getCode(Instruction *rInst, Value *countInst,
Function *M, BasicBlock *BB,
vector<Value *> &retVec){
//count[inc]++
case 4:{
- Instruction *Idx = new GetElementPtrInst(countInst,
- vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
- "");//, InsertPos);
+ vector<Value *> tmpVec;
+ tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+ tmpVec.push_back(ConstantSInt::get(Type::LongTy, inc));
+ Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
+
+ //Instruction *Idx = new GetElementPtrInst(countInst,
+ // vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
+ // "");//, InsertPos);
BB->getInstList().push_back(Idx);
Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos);
Type::LongTy,"ctin");//, InsertPos);
BB->getInstList().push_back(castInst);
- Instruction *Idx = new GetElementPtrInst(countInst,
- vector<Value*>(1,castInst), "");//,
+ vector<Value *> tmpVec;
+ tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+ tmpVec.push_back(castInst);
+ Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
// InsertPos);
BB->getInstList().push_back(Idx);
Value *cons=ConstantSInt::get(Type::IntTy,1);
//count[addIndex]++
+ //std::cerr<<"Type ldInst:"<<ldInst->getType()<<"\t cons:"<<cons->getType()<<"\n";
Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
cons,"");
BB->getInstList().push_back(newCount);
Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin");
BB->getInstList().push_back(castInst2);
- Instruction *Idx = new GetElementPtrInst(countInst,
- vector<Value*>(1,castInst2), "");
+ vector<Value *> tmpVec;
+ tmpVec.push_back(Constant::getNullValue(Type::LongTy));
+ tmpVec.push_back(castInst2);
+ Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
+
+ //Instruction *Idx = new GetElementPtrInst(countInst,
+ // vector<Value*>(1,castInst2), "");
+
BB->getInstList().push_back(Idx);
Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos);
//the number of executions of path k
void insertInTopBB(BasicBlock *front,
int k,
- Instruction *rVar,
- Instruction *countVar, Value *threshold){
+ Instruction *rVar, Value *threshold){
//rVar is variable r,
- //countVar is array Count, and these are allocatted outside
+ //countVar is count[]
Value *Int0 = ConstantInt::get(Type::IntTy, 0);
//now push all instructions in front of the BB
BasicBlock::iterator here=front->begin();
front->getInstList().insert(here, rVar);
- front->getInstList().insert(here,countVar);
+ //front->getInstList().insert(here,countVar);
//Initialize Count[...] with 0
new StoreInst(Int0, rVar, here);
//insert initialize function for initializing
- vector<const Type*> inCountArgs;
- inCountArgs.push_back(PointerType::get(Type::IntTy));
- inCountArgs.push_back(Type::IntTy);
-
- const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs,
- false);
- Function *inCountMth = front->getParent()->getParent()->getOrInsertFunction("llvmInitializeCounter", cFty);
- assert(inCountMth && "Initialize method could not be inserted!");
-
- vector<Value *> iniArgs;
- iniArgs.push_back(countVar);
- iniArgs.push_back(ConstantSInt::get(Type::IntTy, k));
- new CallInst(inCountMth, iniArgs, "", here);
+ //vector<const Type*> inCountArgs;
+ //inCountArgs.push_back(PointerType::get(Type::IntTy));
+ //inCountArgs.push_back(Type::IntTy);
+
+ //const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs,
+ // false);
+//Function *inCountMth = front->getParent()->getParent()->getOrInsertFunction("llvmInitializeCounter", cFty);
+//assert(inCountMth && "Initialize method could not be inserted!");
+
+//vector<Value *> iniArgs;
+//iniArgs.push_back(countVar);
+//iniArgs.push_back(ConstantSInt::get(Type::IntTy, k));
+//new CallInst(inCountMth, iniArgs, "", here);
if(front->getParent()->getName() == "main"){
void insertBB(Edge ed,
getEdgeCode *edgeCode,
Instruction *rInst,
- Instruction *countInst,
+ Value *countInst,
int numPaths, int Methno, Value *threshold){
BasicBlock* BB1=ed.getFirst()->getElement();
if(retVec.size()>0){
triggerBB = new BasicBlock("trigger", BB1->getParent());
getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno,
- retVec[1], countInst);//retVec[0]);
+ retVec[1], countInst, rInst);//retVec[0]);
//Instruction *castInst = new CastInst(retVec[0], Type::IntTy, "");
Instruction *etr = new LoadInst(threshold, "threshold");
return false;
Node *nd2=ed.getSecond();
- nodeList nli = nodes[ed.getFirst()];//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)
//assign wt(v) to all adjacent vertices v of u
//only if v is in vt
- Graph::nodeList nl=getNodeList(u);
+ Graph::nodeList &nl = getNodeList(u);
for(nodeList::iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI){
Node *v=NI->element;
int weight=-NI->weight;
cerr<<"Graph---------------------\n";
for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){
cerr<<((*LI)->getElement())->getName()<<"->";
- Graph::nodeList nl=getNodeList(*LI);
+ Graph::nodeList &nl = getNodeList(*LI);
for(Graph::nodeList::iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI){
cerr<<":"<<"("<<(NI->element->getElement())
->getName()<<":"<<NI->element->getWeight()<<","<<NI->weight<<")";
vector<Node* > allNodes=getAllNodes();
for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
++NI) {
- nodeList nl=getNodeList(*NI);
+ nodeList &nl = getNodeList(*NI);
for(nodeList::iterator NLI=nl.begin(), NLE=nl.end(); NLI!=NLE; ++NLI){
Edge ed(NLI->element, *NI, NLI->weight);
if(!hasEdgeAndWt(ed)){
vector<Node *> allNodes=getAllNodes();
for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
++NI) {
- nodeList node_list=getNodeList(*NI);
+ nodeList &node_list = getNodeList(*NI);
for(nodeList::iterator NLI=nodes[*NI].begin(), NLE=nodes[*NI].end();
NLI!=NLE; ++NLI)
NLI->weight=-NLI->weight;
inline bool operator==(Node& nd) const { return element==nd.element; }
};
-
//Class Edge
//Denotes an edge in the graph
class Edge{
//get the code to be inserted on the edge
//This is determined from cond (1-6)
- void getCode(Instruction *a, Instruction *b, Function *M, BasicBlock *BB,
+ void getCode(Instruction *a, Value *b, Function *M, BasicBlock *BB,
std::vector<Value *> &retVec);
};
//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, int MethNo, Value *threshold);
+void processGraph(Graph &g, Instruction *rInst, Value *countInst, std::vector<Edge> &be, std::vector<Edge> &stDummy, std::vector<Edge> &exDummy, int n, int MethNo, Value *threshold);
//print the graph (for debugging)
void printGraph(Graph &g);
//void printGraph(const Graph g);
//insert a basic block with appropriate code
//along a given edge
-void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, Instruction *countInst, int n, int Methno, Value *threshold);
+void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, Value *countInst, int n, int Methno, Value *threshold);
//Insert the initialization code in the top BB
//this includes initializing r, and count
//number of that path
//Count is an array, where Count[k] represents
//the number of executions of path k
-void insertInTopBB(BasicBlock *front, int k, Instruction *rVar, Instruction *countVar, Value *threshold);
+void insertInTopBB(BasicBlock *front, int k, Instruction *rVar, Value *threshold);
//Add dummy edges corresponding to the back edges
//If a->b is a backedge
//appropriate locations
void processGraph(Graph &g,
Instruction *rInst,
- Instruction *countInst,
+ Value *countInst,
vector<Edge >& be,
vector<Edge >& stDummy,
vector<Edge >& exDummy,
//appropriate locations
void processGraph(Graph &g,
Instruction *rInst,
- Instruction *countInst,
+ Value *countInst,
vector<Edge >& be,
vector<Edge >& stDummy,
vector<Edge >& exDummy,
// numPaths is the number of acyclic paths in the graph
int numPaths=valueAssignmentToEdges(g, nodePriority, be);
+ //if(numPaths<=1) return false;
+
if(numPaths<=1 || numPaths >5000) return false;
#ifdef DEBUG_PATH_PROFILES
AllocaInst(Type::IntTy,
ConstantUInt::get(Type::UIntTy,1),"R");
- Instruction *countVar=new
- AllocaInst(Type::IntTy,
- ConstantUInt::get(Type::UIntTy, numPaths), "Count");
-
+ //Instruction *countVar=new
+ //AllocaInst(Type::IntTy,
+ // ConstantUInt::get(Type::UIntTy, numPaths), "Count");
+
+ //initialize counter array!
+ std::vector<Constant*> arrayInitialize;
+ for(int xi=0; xi<numPaths; xi++)
+ arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
+
+ Constant *initializer = ConstantArray::get(ArrayType::get(Type::IntTy, numPaths), arrayInitialize);
+ GlobalVariable *countVar = new GlobalVariable(ArrayType::get(Type::IntTy, numPaths), false, true, initializer, "Count", F.getParent());
static GlobalVariable *threshold = NULL;
static bool insertedThreshold = false;
// insert initialization code in first (entry) BB
// this includes initializing r and count
- insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar, threshold);
+ insertInTopBB(&F.getEntryNode(),numPaths, rVar, threshold);
//now process the graph: get path numbers,
//get increments along different paths,
//Routines to get the path trace!
-void getPathFrmNode(Node *n, vector<BasicBlock*> &vBB, int pathNo, Graph g,
+void getPathFrmNode(Node *n, vector<BasicBlock*> &vBB, int pathNo, Graph &g,
vector<Edge> &stDummy, vector<Edge> &exDummy,
vector<Edge> &be,
double strand){
- Graph::nodeList nlist=g.getNodeList(n);
+ Graph::nodeList &nlist = g.getNodeList(n);
+ //printGraph(g);
+ //std::cerr<<"Path No: "<<pathNo<<"\n";
int maxCount=-9999999;
bool isStart=false;
double edgeRnd=0;
Node *nextRoot=n;
- for(Graph::nodeList::iterator NLI=nlist.begin(), NLE=nlist.end(); NLI!=NLE;
+ for(Graph::nodeList::iterator NLI = nlist.begin(), NLE=nlist.end(); NLI!=NLE;
++NLI){
if(NLI->weight>maxCount && NLI->weight<=pathNo){
maxCount=NLI->weight;
return NULL;
}
-void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){
+void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){//,
+ // vector<Instruction *> &instToErase){
//step 1: create graph
//Transform the cfg s.t. we have just one exit node
std::vector<Edge> edges;
Node *tmp;
Node *exitNode=0, *startNode=0;
- static std::map<Function *, Graph *> graphMap;
+
+ //Creat cfg just once for each function!
+ static std::map<Function *, Graph *> graphMap;
+
+ //get backedges, exit and start edges for the graphs and store them
static std::map<Function *, vector<Edge> > stMap, exMap, beMap;
+ static std::map<Function *, Value *> pathReg; //path register
+
if(!graphMap[M]){
BasicBlock *ExitNode = 0;
//The nodes must be uniquely identified:
//That is, no two nodes must hav same BB*
+ //keep a map for trigger basicblocks!
+ std::map<BasicBlock *, unsigned char> triggerBBs;
//First enter just nodes: later enter edges
for(Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
- if(BB->size()==2){
- const Instruction *inst = BB->getInstList().begin();
- if(isa<CallInst>(inst)){
- Instruction *ii1 = BB->getInstList().begin();
- CallInst *callInst = dyn_cast<CallInst>(ii1);
- if(callInst->getCalledFunction()->getName()=="trigger")
- continue;
+ bool cont = false;
+
+ if(BB->size()==3 || BB->size() ==2){
+ for(BasicBlock::iterator II = BB->begin(), IE = BB->end();
+ II != IE; ++II){
+ if(CallInst *callInst = dyn_cast<CallInst>(&*II)){
+ //std::cerr<<*callInst;
+ Function *calledFunction = callInst->getCalledFunction();
+ if(calledFunction && calledFunction->getName() == "trigger"){
+ triggerBBs[BB] = 9;
+ cont = true;
+ //std::cerr<<"Found trigger!\n";
+ break;
+ }
+ }
}
}
+
+ if(cont)
+ continue;
+
+ // const Instruction *inst = BB->getInstList().begin();
+ // if(isa<CallInst>(inst)){
+ // Instruction *ii1 = BB->getInstList().begin();
+ // CallInst *callInst = dyn_cast<CallInst>(ii1);
+ // if(callInst->getCalledFunction()->getName()=="trigger")
+ // continue;
+ // }
+
Node *nd=new Node(BB);
nodes.push_back(nd);
if(&*BB==ExitNode)
assert(exitNode!=0 && startNode!=0 && "Start or exit not found!");
for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
- if(BB->size()==2){
- const Instruction *inst = BB->getInstList().begin();
- if(isa<CallInst>(inst)){
- Instruction *ii1 = BB->getInstList().begin();
- CallInst *callInst = dyn_cast<CallInst>(ii1);
- if(callInst->getCalledFunction()->getName()=="trigger")
- continue;
- }
- }
-
+ if(triggerBBs[BB] == 9)
+ continue;
+
+ //if(BB->size()==3)
+ //if(CallInst *callInst = dyn_cast<CallInst>(&*BB->getInstList().begin()))
+ //if(callInst->getCalledFunction()->getName() == "trigger")
+ //continue;
+
+ // if(BB->size()==2){
+ // const Instruction *inst = BB->getInstList().begin();
+ // if(isa<CallInst>(inst)){
+ // Instruction *ii1 = BB->getInstList().begin();
+ // CallInst *callInst = dyn_cast<CallInst>(ii1);
+ // if(callInst->getCalledFunction()->getName()=="trigger")
+ // continue;
+ // }
+ // }
+
Node *nd=findBB(nodes, BB);
assert(nd && "No node for this edge!");
-
+
for(BasicBlock::succ_iterator s=succ_begin(&*BB), se=succ_end(&*BB);
s!=se; ++s){
- if((*s)->size()==2){
- const Instruction *inst = (*s)->getInstList().begin();
- if(isa<CallInst>(inst)){
- Instruction *ii1 = (*s)->getInstList().begin();
- CallInst *callInst = dyn_cast<CallInst>(ii1);
- if(callInst->getCalledFunction()->getName()=="trigger")
- continue;
- }
+
+ if(triggerBBs[*s] == 9){
+ //if(!pathReg[M]){ //Get the path register for this!
+ //if(BB->size()>8)
+ // if(LoadInst *ldInst = dyn_cast<LoadInst>(&*BB->getInstList().begin()))
+ // pathReg[M] = ldInst->getPointerOperand();
+ //}
+ continue;
}
-
- Node *nd2=findBB(nodes,*s);
+ //if((*s)->size()==3)
+ //if(CallInst *callInst =
+ // dyn_cast<CallInst>(&*(*s)->getInstList().begin()))
+ // if(callInst->getCalledFunction()->getName() == "trigger")
+ // continue;
+
+ // if((*s)->size()==2){
+ // const Instruction *inst = (*s)->getInstList().begin();
+ // if(isa<CallInst>(inst)){
+ // Instruction *ii1 = (*s)->getInstList().begin();
+ // CallInst *callInst = dyn_cast<CallInst>(ii1);
+ // if(callInst->getCalledFunction()->getName()=="trigger")
+ // continue;
+ // }
+ // }
+
+ Node *nd2 = findBB(nodes,*s);
assert(nd2 && "No node for this edge!");
Edge ed(nd,nd2,0);
edges.push_back(ed);
Graph *g = graphMap[M];
if (M->size() <= 1) return; //uninstrumented
-
+
//step 2: getBackEdges
//vector<Edge> be;
std::map<Node *, int> nodePriority;
g->getBackEdges(beMap[M], nodePriority);
-
+
//step 3: add dummy edges
//vector<Edge> stDummy;
//vector<Edge> exDummy;
addDummyEdges(stMap[M], exMap[M], *g, beMap[M]);
-
+
//step 4: value assgn to edges
int numPaths = valueAssignmentToEdges(*g, nodePriority, beMap[M]);
}
-
+
+
//step 5: now travel from root, select max(edge) < pathNo,
//and go on until reach the exit
- return getPathFrmNode(graphMap[M]->getRoot(), vBB, pathNo, *graphMap[M],
- stMap[M], exMap[M], beMap[M], -1);
+ getPathFrmNode(graphMap[M]->getRoot(), vBB, pathNo, *graphMap[M],
+ stMap[M], exMap[M], beMap[M], -1);
+
+
+ //post process vBB to locate instructions to be erased
+ /*
+ if(pathReg[M]){
+ for(vector<BasicBlock *>::iterator VBI = vBB.begin(), VBE = vBB.end();
+ VBI != VBE; ++VBI){
+ for(BasicBlock::iterator BBI = (*VBI)->begin(), BBE = (*VBI)->end();
+ BBI != BBE; ++BBI){
+ if(LoadInst *ldInst = dyn_cast<LoadInst>(&*BBI)){
+ if(pathReg[M] == ldInst->getPointerOperand())
+ instToErase.push_back(ldInst);
+ }
+ else if(StoreInst *stInst = dyn_cast<StoreInst>(&*BBI)){
+ if(pathReg[M] == stInst->getPointerOperand())
+ instToErase.push_back(stInst);
+ }
+ }
+ }
+ }
+ */
}