From 92d5e30e762601ce1076e548d2281fac39e40fe3 Mon Sep 17 00:00:00 2001 From: Anand Shukla Date: Fri, 14 Feb 2003 20:41:53 +0000 Subject: [PATCH] Changes to runtime framework git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5572 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Instrumentation/ProfilePaths/EdgeCode.cpp | 75 ++++++--- .../Instrumentation/ProfilePaths/Graph.cpp | 10 +- .../Instrumentation/ProfilePaths/Graph.h | 9 +- .../ProfilePaths/GraphAuxiliary.cpp | 2 +- .../ProfilePaths/GraphAuxillary.cpp | 2 +- .../ProfilePaths/ProfilePaths.cpp | 19 ++- .../ProfilePaths/RetracePath.cpp | 154 +++++++++++++----- 7 files changed, 188 insertions(+), 83 deletions(-) diff --git a/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp b/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp index ba4a9013a73..8fabbc9ca39 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp @@ -25,7 +25,7 @@ using std::vector; static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, - Value *cnt){ + Value *cnt, Instruction *rInst){ vector args; //args.push_back(PointerType::get(Type::SByteTy)); @@ -33,8 +33,15 @@ static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, 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 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!"); @@ -42,7 +49,8 @@ static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, 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); @@ -52,7 +60,7 @@ static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, //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 &retVec){ @@ -105,9 +113,14 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst, //count[inc]++ case 4:{ - Instruction *Idx = new GetElementPtrInst(countInst, - vector(1,ConstantSInt::get(Type::LongTy, inc)), - "");//, InsertPos); + vector 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(1,ConstantSInt::get(Type::LongTy, inc)), + // "");//, InsertPos); BB->getInstList().push_back(Idx); Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos); @@ -156,8 +169,10 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst, Type::LongTy,"ctin");//, InsertPos); BB->getInstList().push_back(castInst); - Instruction *Idx = new GetElementPtrInst(countInst, - vector(1,castInst), "");//, + vector tmpVec; + tmpVec.push_back(Constant::getNullValue(Type::LongTy)); + tmpVec.push_back(castInst); + Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//, // InsertPos); BB->getInstList().push_back(Idx); @@ -166,6 +181,7 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst, Value *cons=ConstantSInt::get(Type::IntTy,1); //count[addIndex]++ + //std::cerr<<"Type ldInst:"<getType()<<"\t cons:"<getType()<<"\n"; Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst, cons,""); BB->getInstList().push_back(newCount); @@ -194,8 +210,14 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst, Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin"); BB->getInstList().push_back(castInst2); - Instruction *Idx = new GetElementPtrInst(countInst, - vector(1,castInst2), ""); + vector tmpVec; + tmpVec.push_back(Constant::getNullValue(Type::LongTy)); + tmpVec.push_back(castInst2); + Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//, + + //Instruction *Idx = new GetElementPtrInst(countInst, + // vector(1,castInst2), ""); + BB->getInstList().push_back(Idx); Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos); @@ -233,17 +255,16 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst, //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 @@ -258,19 +279,19 @@ void insertInTopBB(BasicBlock *front, new StoreInst(Int0, rVar, here); //insert initialize function for initializing - vector inCountArgs; - inCountArgs.push_back(PointerType::get(Type::IntTy)); - inCountArgs.push_back(Type::IntTy); + //vector 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!"); + //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 iniArgs; - iniArgs.push_back(countVar); - iniArgs.push_back(ConstantSInt::get(Type::IntTy, k)); - new CallInst(inCountMth, iniArgs, "", here); +//vector iniArgs; +//iniArgs.push_back(countVar); +//iniArgs.push_back(ConstantSInt::get(Type::IntTy, k)); +//new CallInst(inCountMth, iniArgs, "", here); if(front->getParent()->getName() == "main"){ @@ -296,7 +317,7 @@ void insertInTopBB(BasicBlock *front, void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, - Instruction *countInst, + Value *countInst, int numPaths, int Methno, Value *threshold){ BasicBlock* BB1=ed.getFirst()->getElement(); @@ -338,7 +359,7 @@ void insertBB(Edge ed, 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"); diff --git a/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp b/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp index c6a98712fac..50497fdf10e 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp @@ -151,7 +151,7 @@ bool Graph::hasEdgeAndWt(Edge ed){ 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) @@ -400,7 +400,7 @@ Graph* Graph::getMaxSpanningTree(){ //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; @@ -437,7 +437,7 @@ void Graph::printGraph(){ cerr<<"Graph---------------------\n"; for(vector::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()<<":"<element->getWeight()<<","<weight<<")"; @@ -483,7 +483,7 @@ void Graph::makeUnDirectional(){ vector allNodes=getAllNodes(); for(vector::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)){ @@ -502,7 +502,7 @@ void Graph::reverseWts(){ vector allNodes=getAllNodes(); for(vector::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; diff --git a/lib/Transforms/Instrumentation/ProfilePaths/Graph.h b/lib/Transforms/Instrumentation/ProfilePaths/Graph.h index 11713ebb955..3c22d627e4f 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/Graph.h +++ b/lib/Transforms/Instrumentation/ProfilePaths/Graph.h @@ -33,7 +33,6 @@ public: inline bool operator==(Node& nd) const { return element==nd.element; } }; - //Class Edge //Denotes an edge in the graph class Edge{ @@ -410,7 +409,7 @@ public: //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 &retVec); }; @@ -423,7 +422,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 &be, std::vector &stDummy, std::vector &exDummy, int n, int MethNo, Value *threshold); +void processGraph(Graph &g, Instruction *rInst, Value *countInst, std::vector &be, std::vector &stDummy, std::vector &exDummy, int n, int MethNo, Value *threshold); //print the graph (for debugging) void printGraph(Graph &g); @@ -432,7 +431,7 @@ 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 @@ -442,7 +441,7 @@ void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, Instruction *c //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 diff --git a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp index dad3958c866..e910616d290 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp @@ -479,7 +479,7 @@ static void moveDummyCode(vector &stDummy, //appropriate locations void processGraph(Graph &g, Instruction *rInst, - Instruction *countInst, + Value *countInst, vector& be, vector& stDummy, vector& exDummy, diff --git a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxillary.cpp b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxillary.cpp index dad3958c866..e910616d290 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxillary.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxillary.cpp @@ -479,7 +479,7 @@ static void moveDummyCode(vector &stDummy, //appropriate locations void processGraph(Graph &g, Instruction *rInst, - Instruction *countInst, + Value *countInst, vector& be, vector& stDummy, vector& exDummy, diff --git a/lib/Transforms/Instrumentation/ProfilePaths/ProfilePaths.cpp b/lib/Transforms/Instrumentation/ProfilePaths/ProfilePaths.cpp index 4c689b12eec..39e7c351210 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/ProfilePaths.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/ProfilePaths.cpp @@ -155,6 +155,8 @@ bool ProfilePaths::runOnFunction(Function &F){ // 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 @@ -172,10 +174,17 @@ bool ProfilePaths::runOnFunction(Function &F){ 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 arrayInitialize; + for(int xi=0; xi &vBB, int pathNo, Graph g, +void getPathFrmNode(Node *n, vector &vBB, int pathNo, Graph &g, vector &stDummy, vector &exDummy, vector &be, double strand){ - Graph::nodeList nlist=g.getNodeList(n); + Graph::nodeList &nlist = g.getNodeList(n); + //printGraph(g); + //std::cerr<<"Path No: "< &vBB, int pathNo, Graph g, 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; @@ -116,7 +118,8 @@ static Node *findBB(std::vector &st, BasicBlock *BB){ return NULL; } -void getBBtrace(vector &vBB, int pathNo, Function *M){ +void getBBtrace(vector &vBB, int pathNo, Function *M){//, + // vector &instToErase){ //step 1: create graph //Transform the cfg s.t. we have just one exit node @@ -124,8 +127,14 @@ void getBBtrace(vector &vBB, int pathNo, Function *M){ std::vector edges; Node *tmp; Node *exitNode=0, *startNode=0; - static std::map graphMap; + + //Creat cfg just once for each function! + static std::map graphMap; + + //get backedges, exit and start edges for the graphs and store them static std::map > stMap, exMap, beMap; + static std::map pathReg; //path register + if(!graphMap[M]){ BasicBlock *ExitNode = 0; @@ -142,17 +151,39 @@ void getBBtrace(vector &vBB, int pathNo, Function *M){ //The nodes must be uniquely identified: //That is, no two nodes must hav same BB* + //keep a map for trigger basicblocks! + std::map 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(inst)){ - Instruction *ii1 = BB->getInstList().begin(); - CallInst *callInst = dyn_cast(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(&*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(inst)){ + // Instruction *ii1 = BB->getInstList().begin(); + // CallInst *callInst = dyn_cast(ii1); + // if(callInst->getCalledFunction()->getName()=="trigger") + // continue; + // } + Node *nd=new Node(BB); nodes.push_back(nd); if(&*BB==ExitNode) @@ -164,32 +195,55 @@ void getBBtrace(vector &vBB, int pathNo, Function *M){ 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(inst)){ - Instruction *ii1 = BB->getInstList().begin(); - CallInst *callInst = dyn_cast(ii1); - if(callInst->getCalledFunction()->getName()=="trigger") - continue; - } - } - + if(triggerBBs[BB] == 9) + continue; + + //if(BB->size()==3) + //if(CallInst *callInst = dyn_cast(&*BB->getInstList().begin())) + //if(callInst->getCalledFunction()->getName() == "trigger") + //continue; + + // if(BB->size()==2){ + // const Instruction *inst = BB->getInstList().begin(); + // if(isa(inst)){ + // Instruction *ii1 = BB->getInstList().begin(); + // CallInst *callInst = dyn_cast(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(inst)){ - Instruction *ii1 = (*s)->getInstList().begin(); - CallInst *callInst = dyn_cast(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(&*BB->getInstList().begin())) + // pathReg[M] = ldInst->getPointerOperand(); + //} + continue; } - - Node *nd2=findBB(nodes,*s); + //if((*s)->size()==3) + //if(CallInst *callInst = + // dyn_cast(&*(*s)->getInstList().begin())) + // if(callInst->getCalledFunction()->getName() == "trigger") + // continue; + + // if((*s)->size()==2){ + // const Instruction *inst = (*s)->getInstList().begin(); + // if(isa(inst)){ + // Instruction *ii1 = (*s)->getInstList().begin(); + // CallInst *callInst = dyn_cast(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); @@ -201,23 +255,45 @@ void getBBtrace(vector &vBB, int pathNo, Function *M){ Graph *g = graphMap[M]; if (M->size() <= 1) return; //uninstrumented - + //step 2: getBackEdges //vector be; std::map nodePriority; g->getBackEdges(beMap[M], nodePriority); - + //step 3: add dummy edges //vector stDummy; //vector 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::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(&*BBI)){ + if(pathReg[M] == ldInst->getPointerOperand()) + instToErase.push_back(ldInst); + } + else if(StoreInst *stInst = dyn_cast(&*BBI)){ + if(pathReg[M] == stInst->getPointerOperand()) + instToErase.push_back(stInst); + } + } + } + } + */ }