-//===-- EdgeCode.cpp - generate LLVM instrumentation code --------*- C++ -*--=//
+//===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===//
//It implements the class EdgeCode: which provides
//support for inserting "appropriate" instrumentation at
//designated points in the graph
//top block of cfg
//===----------------------------------------------------------------------===//
-#include "Graph.h"
-#include "llvm/ConstantVals.h"
+#include "llvm/Transforms/Instrumentation/Graph.h"
+#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/iMemory.h"
#include "llvm/iTerminators.h"
#include "llvm/iOther.h"
#include "llvm/iOperators.h"
#include "llvm/iPHINode.h"
+#include "llvm/Module.h"
+#include "llvm/Instruction.h"
+#include <stdio.h>
+
+#define INSERT_LOAD_COUNT
+#define INSERT_STORE
using std::vector;
+
+static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
+ Value *cnt){
+
+ vector<const Type*> args;
+ //args.push_back(PointerType::get(Type::SByteTy));
+ args.push_back(Type::IntTy);
+ args.push_back(Type::IntTy);
+ //args.push_back(Type::IntTy);
+ args.push_back(PointerType::get(Type::IntTy));
+ const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false);
+
+ Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
+ assert(trigMeth && "trigger method could not be inserted!");
+
+ vector<Value *> trargs;
+
+ trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
+ trargs.push_back(pathNo);
+ trargs.push_back(cnt);
+
+ Instruction *callInst=new CallInst(trigMeth, trargs, "");//, BB->begin());
+ BB->getInstList().push_back(callInst);
+ //triggerInst = new CallInst(trigMeth, trargs, "");//, InsertPos);
+}
+
+
//get the code to be inserted on the edge
//This is determined from cond (1-6)
-void getEdgeCode::getCode(Instruction *rInst,
- Instruction *countInst,
- Method *M,
- BasicBlock *BB){
+void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
+ Function *M, BasicBlock *BB,
+ vector<Value *> &retVec){
+
+ //Instruction *InsertPos = BB->getInstList().begin();
- BasicBlock::InstListType& instList=BB->getInstList();
- BasicBlock::iterator here=instList.begin();
+ //now check for cdIn and cdOut
+ //first put cdOut
+ if(cdOut!=NULL){
+ cdOut->getCode(rInst, countInst, M, BB, retVec);
+ }
+ if(cdIn!=NULL){
+ cdIn->getCode(rInst, countInst, M, BB, retVec);
+ }
+
//case: r=k code to be inserted
switch(cond){
case 1:{
Value *val=ConstantSInt::get(Type::IntTy,inc);
- Instruction *stInst=new StoreInst(val, rInst);
- here=instList.insert(here,stInst)+1;
+#ifdef INSERT_STORE
+ Instruction *stInst=new StoreInst(val, rInst);//, InsertPos);
+ BB->getInstList().push_back(stInst);
+#endif
break;
}
//case: r=0 to be inserted
case 2:{
- Value *val=ConstantSInt::get(Type::IntTy,0);
- Instruction *stInst=new StoreInst(val, rInst);
- here=instList.insert(here,stInst)+1;
+#ifdef INSERT_STORE
+ Instruction *stInst = new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst);//, InsertPos);
+ BB->getInstList().push_back(stInst);
+#endif
break;
}
//r+=k
case 3:{
- Instruction *ldInst=new LoadInst(rInst, "ti1");
- Value *val=ConstantSInt::get(Type::IntTy,inc);
- Instruction *addIn=BinaryOperator::
- create(Instruction::Add, ldInst, val,"ti2");
-
- Instruction *stInst=new StoreInst(addIn, rInst);
- here=instList.insert(here,ldInst)+1;
- here=instList.insert(here,addIn)+1;
- here=instList.insert(here,stInst)+1;
+ Instruction *ldInst = new LoadInst(rInst, "ti1");//, InsertPos);
+ BB->getInstList().push_back(ldInst);
+ Value *val = ConstantSInt::get(Type::IntTy,inc);
+ Instruction *addIn = BinaryOperator::create(Instruction::Add, ldInst, val,
+ "ti2");//, InsertPos);
+ BB->getInstList().push_back(addIn);
+#ifdef INSERT_STORE
+ Instruction *stInst = new StoreInst(addIn, rInst);//, InsertPos);
+ BB->getInstList().push_back(stInst);
+#endif
break;
}
//count[inc]++
case 4:{
- Instruction *ldInst=new
- LoadInst(countInst,vector<Value *>
- (1,ConstantUInt::get(Type::UIntTy, inc)), "ti1");
- Value *val=ConstantSInt::get(Type::IntTy,1);
- Instruction *addIn=BinaryOperator::
- create(Instruction::Add, ldInst, val,"ti2");
+ Instruction *Idx = new GetElementPtrInst(countInst,
+ vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
+ "");//, InsertPos);
+ BB->getInstList().push_back(Idx);
- assert(inc>=0 && "IT MUST BE POSITIVE NOW");
- Instruction *stInst=new
- StoreInst(addIn, countInst, vector<Value *>
- (1, ConstantUInt::get(Type::UIntTy,inc)));
+ Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos);
+ BB->getInstList().push_back(ldInst);
+
+ Value *val = ConstantSInt::get(Type::IntTy, 1);
+ //Instruction *addIn =
+ Instruction *newCount =
+ BinaryOperator::create(Instruction::Add, ldInst, val,"ti2");
+ BB->getInstList().push_back(newCount);
+
+
+#ifdef INSERT_STORE
+ //Instruction *stInst=new StoreInst(addIn, Idx, InsertPos);
+ Instruction *stInst=new StoreInst(newCount, Idx);//, InsertPos);
+ BB->getInstList().push_back(stInst);
+#endif
- here=instList.insert(here,ldInst)+1;
- here=instList.insert(here,addIn)+1;
- here=instList.insert(here,stInst)+1;
+ Value *trAddIndex = ConstantSInt::get(Type::IntTy,inc);
+
+ retVec.push_back(newCount);
+ retVec.push_back(trAddIndex);
+ //insert trigger
+ //getTriggerCode(M->getParent(), BB, MethNo,
+ // ConstantSInt::get(Type::IntTy,inc), newCount, triggerInst);
+ //end trigger code
+
+ assert(inc>=0 && "IT MUST BE POSITIVE NOW");
break;
}
//case: count[r+inc]++
case 5:{
+
//ti1=inc+r
- Instruction *ldIndex=new LoadInst(rInst, "ti1");
+ Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
+ BB->getInstList().push_back(ldIndex);
+
Value *val=ConstantSInt::get(Type::IntTy,inc);
Instruction *addIndex=BinaryOperator::
- create(Instruction::Add, ldIndex, val,"ti2");
+ create(Instruction::Add, ldIndex, val,"ti2");//, InsertPos);
+ BB->getInstList().push_back(addIndex);
//now load count[addIndex]
Instruction *castInst=new CastInst(addIndex,
- Type::UIntTy,"ctin");
- Instruction *ldInst=new
- LoadInst(countInst, vector<Value *>(1,castInst), "ti3");
+ Type::LongTy,"ctin");//, InsertPos);
+ BB->getInstList().push_back(castInst);
+
+ Instruction *Idx = new GetElementPtrInst(countInst,
+ vector<Value*>(1,castInst), "");//,
+ // InsertPos);
+ BB->getInstList().push_back(Idx);
+
+ Instruction *ldInst=new LoadInst(Idx, "ti3");//, InsertPos);
+ BB->getInstList().push_back(ldInst);
+
Value *cons=ConstantSInt::get(Type::IntTy,1);
-
//count[addIndex]++
- Instruction *addIn=BinaryOperator::
- create(Instruction::Add, ldInst, cons,"ti4");
- Instruction *stInst=new
- StoreInst(addIn, countInst,
- vector<Value *>(1,castInst));
+ Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
+ cons,"");
+ BB->getInstList().push_back(newCount);
- here=instList.insert(here,ldIndex)+1;
- here=instList.insert(here,addIndex)+1;
- here=instList.insert(here,castInst)+1;
- here=instList.insert(here,ldInst)+1;
- here=instList.insert(here,addIn)+1;
- here=instList.insert(here,stInst)+1;
+#ifdef INSERT_STORE
+ Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
+ BB->getInstList().push_back(stInst);
+#endif
+
+ retVec.push_back(newCount);
+ retVec.push_back(addIndex);
+ //insert trigger
+ //getTriggerCode(M->getParent(), BB, MethNo, addIndex, newCount, triggerInst);
+ //end trigger code
+
break;
}
//case: count[r]+
case 6:{
//ti1=inc+r
- Instruction *ldIndex=new LoadInst(rInst, "ti1");
+ Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
+ BB->getInstList().push_back(ldIndex);
//now load count[addIndex]
- Instruction *castInst2=new
- CastInst(ldIndex, Type::UIntTy,"ctin");
- Instruction *ldInst=new
- LoadInst(countInst, vector<Value *>(1,castInst2), "ti2");
+ Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin");
+ BB->getInstList().push_back(castInst2);
+
+ Instruction *Idx = new GetElementPtrInst(countInst,
+ vector<Value*>(1,castInst2), "");
+ BB->getInstList().push_back(Idx);
+
+ Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos);
+ BB->getInstList().push_back(ldInst);
+
Value *cons=ConstantSInt::get(Type::IntTy,1);
//count[addIndex]++
- Instruction *addIn=BinaryOperator::
- create(Instruction::Add, ldInst, cons,"ti3");
- Instruction *stInst=new
- StoreInst(addIn, countInst, vector<Value *>(1,castInst2));
-
- here=instList.insert(here,ldIndex)+1;
- here=instList.insert(here,castInst2)+1;
- here=instList.insert(here,ldInst)+1;
- here=instList.insert(here,addIn)+1;
- here=instList.insert(here,stInst)+1;
+ Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
+ cons,"ti3");
+ BB->getInstList().push_back(newCount);
+
+#ifdef INSERT_STORE
+ Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
+ BB->getInstList().push_back(stInst);
+#endif
+
+ retVec.push_back(newCount);
+ retVec.push_back(ldIndex);
break;
}
}
- //now check for cdIn and cdOut
- //first put cdOut
- if(cdOut!=NULL){
- cdOut->getCode(rInst, countInst, M, BB);
- }
- if(cdIn!=NULL){
- cdIn->getCode(rInst, countInst, M, BB);
- }
-
}
void insertInTopBB(BasicBlock *front,
int k,
Instruction *rVar,
- Instruction *countVar){
+ Instruction *countVar, Value *threshold){
//rVar is variable r,
//countVar is array Count, and these are allocatted outside
-
- //store uint 0, uint *%R, uint 0
- vector<Value *> idx;
- idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
- Instruction *stInstr=new StoreInst(ConstantInt::get(Type::IntTy, 0), rVar,
- idx);
+ Value *Int0 = ConstantInt::get(Type::IntTy, 0);
+
//now push all instructions in front of the BB
- BasicBlock::InstListType& instList=front->getInstList();
- BasicBlock::iterator here=instList.begin();
- here=front->getInstList().insert(here, rVar)+1;
- here=front->getInstList().insert(here,countVar)+1;
+ BasicBlock::iterator here=front->begin();
+ front->getInstList().insert(here, rVar);
+ front->getInstList().insert(here,countVar);
//Initialize Count[...] with 0
- for(int i=0;i<k; i++){
- Instruction *stInstrC=new
- StoreInst(ConstantInt::get(Type::IntTy, 0),
- countVar, std::vector<Value *>
- (1,ConstantUInt::get(Type::UIntTy, i)));
- here=front->getInstList().insert(here,stInstrC)+1;
- }
+
+ //for (int i=0;i<k; i++){
+ //Value *GEP2 = new GetElementPtrInst(countVar,
+ // vector<Value *>(1,ConstantSInt::get(Type::LongTy, i)),
+ // "", here);
+ //new StoreInst(Int0, GEP2, here);
+ //}
+
+ //store uint 0, uint *%R
+ 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);
- here=front->getInstList().insert(here,stInstr)+1;
+
+ if(front->getParent()->getName() == "main"){
+ //intialize threshold
+ vector<const Type*> initialize_args;
+ initialize_args.push_back(PointerType::get(Type::IntTy));
+
+ const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
+ false);
+ Function *initialMeth = front->getParent()->getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
+ assert(initialMeth && "Initialize method could not be inserted!");
+
+ vector<Value *> trargs;
+ trargs.push_back(threshold);
+
+ new CallInst(initialMeth, trargs, "", here);
+ }
}
void insertBB(Edge ed,
getEdgeCode *edgeCode,
Instruction *rInst,
- Instruction *countInst){
+ Instruction *countInst,
+ int numPaths, int Methno, Value *threshold){
BasicBlock* BB1=ed.getFirst()->getElement();
BasicBlock* BB2=ed.getSecond()->getElement();
cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n";
cerr<<"########################\n";
#endif
-
+
//We need to insert a BB between BB1 and BB2
TerminatorInst *TI=BB1->getTerminator();
BasicBlock *newBB=new BasicBlock("counter", BB1->getParent());
//get code for the new BB
- edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB);
-
+ vector<Value *> retVec;
+
+ edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, retVec);
+
+ BranchInst *BI = cast<BranchInst>(TI);
+
//Is terminator a branch instruction?
//then we need to change branch destinations to include new BB
- BranchInst *BI=cast<BranchInst>(TI);
-
if(BI->isUnconditional()){
BI->setUnconditionalDest(newBB);
- Instruction *newBI2=new BranchInst(BB2);
+ }
+ else{
+ if(BI->getSuccessor(0)==BB2)
+ BI->setSuccessor(0, newBB);
+
+ if(BI->getSuccessor(1)==BB2)
+ BI->setSuccessor(1, newBB);
+ }
+
+ BasicBlock *triggerBB = NULL;
+ if(retVec.size()>0){
+ triggerBB = new BasicBlock("trigger", BB1->getParent());
+ getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno,
+ retVec[1], countInst);//retVec[0]);
+
+ //Instruction *castInst = new CastInst(retVec[0], Type::IntTy, "");
+ Instruction *etr = new LoadInst(threshold, "threshold");
+
+ //std::cerr<<"type1: "<<etr->getType()<<" type2: "<<retVec[0]->getType()<<"\n";
+ Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr,
+ retVec[0], "");
+ Instruction *newBI2 = new BranchInst(triggerBB, BB2, cmpInst);
+ //newBB->getInstList().push_back(castInst);
+ newBB->getInstList().push_back(etr);
+ newBB->getInstList().push_back(cmpInst);
newBB->getInstList().push_back(newBI2);
+
+ //triggerBB->getInstList().push_back(triggerInst);
+ Instruction *triggerBranch = new BranchInst(BB2);
+ triggerBB->getInstList().push_back(triggerBranch);
}
else{
- Value *cond=BI->getCondition();
- BasicBlock *fB, *tB;
-
- if(BI->getSuccessor(0)==BB2){
- tB=newBB;
- fB=BI->getSuccessor(1);
- }
- else{
- fB=newBB;
- tB=BI->getSuccessor(0);
- }
-
- delete BB1->getInstList().pop_back();
- Instruction *newBI=new BranchInst(tB,fB,cond);
Instruction *newBI2=new BranchInst(BB2);
- BB1->getInstList().push_back(newBI);
newBB->getInstList().push_back(newBI2);
}
-
+
//now iterate over BB2, and set its Phi nodes right
- for(BasicBlock::iterator BB2Inst=BB2->begin(), BBend=BB2->end();
- BB2Inst!=BBend; ++BB2Inst){
+ for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end();
+ BB2Inst != BBend; ++BB2Inst){
- if(PHINode *phiInst=dyn_cast<PHINode>(*BB2Inst)){
-#ifdef DEBUG_PATH_PROFILES
- cerr<<"YYYYYYYYYYYYYYYYY\n";
-#endif
-
+ if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){
int bbIndex=phiInst->getBasicBlockIndex(BB1);
- if(bbIndex>=0)
- phiInst->setIncomingBlock(bbIndex, newBB);
+ assert(bbIndex>=0);
+ phiInst->setIncomingBlock(bbIndex, newBB);
+
+ ///check if trigger!=null, then add value corresponding to it too!
+ if(retVec.size()>0){
+ assert(triggerBB && "BasicBlock with trigger should not be null!");
+ Value *vl = phiInst->getIncomingValue((unsigned int)bbIndex);
+ phiInst->addIncoming(vl, triggerBB);
+ }
}
}
}
+