Simplify code (somtimes dramatically), by using the new "auto-insert" feature

of instruction constructors.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3655 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2002-09-10 17:03:06 +00:00
parent 3bc06b33da
commit 3ea5cb0df1
2 changed files with 65 additions and 125 deletions

View File

@ -60,21 +60,16 @@ static void ConvertCallTo(CallInst *CI, Function *Dest) {
for (unsigned i = 1; i < CI->getNumOperands(); ++i) { for (unsigned i = 1; i < CI->getNumOperands(); ++i) {
Value *V = CI->getOperand(i); Value *V = CI->getOperand(i);
if (V->getType() != ParamTys[i-1]) { // Must insert a cast... if (V->getType() != ParamTys[i-1]) // Must insert a cast...
Instruction *Cast = new CastInst(V, ParamTys[i-1]); V = new CastInst(V, ParamTys[i-1], "argcast", BBI);
BBI = ++BB->getInstList().insert(BBI, Cast);
V = Cast;
}
Params.push_back(V); Params.push_back(V);
} }
Instruction *NewCall = new CallInst(Dest, Params);
// Replace the old call instruction with a new call instruction that calls // Replace the old call instruction with a new call instruction that calls
// the real function. // the real function.
// //
BBI = ++BB->getInstList().insert(BBI, NewCall); Instruction *NewCall = new CallInst(Dest, Params, "", BBI);
// Remove the old call instruction from the program... // Remove the old call instruction from the program...
BB->getInstList().remove(BBI); BB->getInstList().remove(BBI);
@ -104,11 +99,10 @@ static void ConvertCallTo(CallInst *CI, Function *Dest) {
// value of the function is actually USED. // value of the function is actually USED.
// //
if (!CI->use_empty()) { if (!CI->use_empty()) {
CastInst *NewCast = new CastInst(NewCall, CI->getType(),
NewCall->getName());
CI->replaceAllUsesWith(NewCast);
// Insert the new cast instruction... // Insert the new cast instruction...
BB->getInstList().insert(BBI, NewCast); CastInst *NewCast = new CastInst(NewCall, CI->getType(),
NewCall->getName(), BBI);
CI->replaceAllUsesWith(NewCast);
} }
} }

View File

@ -1,4 +1,4 @@
//===-- EdgeCode.cpp - generate LLVM instrumentation code --------*- C++ -*--=// //===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===//
//It implements the class EdgeCode: which provides //It implements the class EdgeCode: which provides
//support for inserting "appropriate" instrumentation at //support for inserting "appropriate" instrumentation at
//designated points in the graph //designated points in the graph
@ -8,7 +8,6 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Transforms/Instrumentation/Graph.h" #include "llvm/Transforms/Instrumentation/Graph.h"
#include "llvm/BasicBlock.h"
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/iMemory.h" #include "llvm/iMemory.h"
@ -17,14 +16,8 @@
#include "llvm/iOperators.h" #include "llvm/iOperators.h"
#include "llvm/iPHINode.h" #include "llvm/iPHINode.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/SymbolTable.h"
#include "llvm/GlobalVariable.h"
#include "llvm/Constants.h"
#include "llvm/BasicBlock.h"
#include "llvm/Function.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <iostream>
#define INSERT_LOAD_COUNT #define INSERT_LOAD_COUNT
#define INSERT_STORE #define INSERT_STORE
@ -32,21 +25,19 @@
using std::vector; using std::vector;
void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
Value *cnt){ Value *cnt, Instruction *InsertPos){
static int i=-1; static int i=-1;
i++; i++;
char gstr[100]; char gstr[100];
sprintf(gstr,"globalVar%d",i); sprintf(gstr,"globalVar%d",i);
std::string globalVarName=gstr; std::string globalVarName=gstr;
SymbolTable *ST = M->getSymbolTable();
vector<const Type*> args; vector<const Type*> args;
//args.push_back(PointerType::get(Type::SByteTy)); //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(Type::IntTy);
args.push_back(Type::IntTy); args.push_back(Type::IntTy);
const FunctionType *MTy = const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false);
FunctionType::get(Type::VoidTy, args, false);
// Function *triggerMeth = M->getOrInsertFunction("trigger", MTy); // Function *triggerMeth = M->getOrInsertFunction("trigger", MTy);
Function *trigMeth = M->getOrInsertFunction("trigger", MTy); Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
@ -81,12 +72,7 @@ void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
//trargs.push_back(ConstantSInt::get(Type::IntTy,-1));//erase this //trargs.push_back(ConstantSInt::get(Type::IntTy,-1));//erase this
trargs.push_back(pathNo); trargs.push_back(pathNo);
trargs.push_back(cnt); trargs.push_back(cnt);
Instruction *callInst=new CallInst(trigMeth,trargs); Instruction *callInst=new CallInst(trigMeth, trargs, "", InsertPos);
BasicBlock::InstListType& instList=BB->getInstList();
BasicBlock::iterator here=instList.begin();
//here = ++instList.insert(here, getElmntInst);
instList.insert(here,callInst);
} }
@ -97,160 +83,126 @@ void getEdgeCode::getCode(Instruction *rInst,
Function *M, Function *M,
BasicBlock *BB, int numPaths, int MethNo){ BasicBlock *BB, int numPaths, int MethNo){
BasicBlock::InstListType& instList=BB->getInstList(); Instruction *InsertPos = BB->begin();
BasicBlock::iterator here=instList.begin();
//case: r=k code to be inserted //case: r=k code to be inserted
switch(cond){ switch(cond){
case 1:{ case 1:{
Value *val=ConstantSInt::get(Type::IntTy,inc); Value *val=ConstantSInt::get(Type::IntTy,inc);
#ifdef INSERT_STORE #ifdef INSERT_STORE
Instruction *stInst=new StoreInst(val, rInst); Instruction *stInst=new StoreInst(val, rInst, InsertPos);
here = ++instList.insert(here,stInst);
#endif #endif
break; break;
} }
//case: r=0 to be inserted //case: r=0 to be inserted
case 2:{ case 2:
Value *val=ConstantSInt::get(Type::IntTy,0);
#ifdef INSERT_STORE #ifdef INSERT_STORE
Instruction *stInst=new StoreInst(val, rInst); new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst, InsertPos);
here = ++instList.insert(here,stInst);
#endif #endif
break; break;
}
//r+=k //r+=k
case 3:{ case 3:{
Instruction *ldInst = new LoadInst(rInst, "ti1", InsertPos);
Instruction *ldInst=new LoadInst(rInst, "ti1"); Value *val = ConstantSInt::get(Type::IntTy,inc);
Value *val=ConstantSInt::get(Type::IntTy,inc); Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, val,
Instruction *addIn=BinaryOperator:: "ti2", InsertPos);
create(Instruction::Add, ldInst, val,"ti2");
#ifdef INSERT_STORE #ifdef INSERT_STORE
Instruction *stInst=new StoreInst(addIn, rInst); new StoreInst(addIn, rInst, InsertPos);
#endif
here = ++instList.insert(here,ldInst);
here = ++instList.insert(here,addIn);
#ifdef INSERT_STORE
here = ++instList.insert(here,stInst);
#endif #endif
break; break;
} }
//count[inc]++ //count[inc]++
case 4:{ case 4:{
assert(inc>=0 && inc<=numPaths && "inc out of bound!"); assert(inc>=0 && inc<=numPaths && "inc out of bound!");
Instruction *Idx = new GetElementPtrInst(countInst, Instruction *Idx = new GetElementPtrInst(countInst,
vector<Value*>(1,ConstantUInt::get(Type::UIntTy, inc))); vector<Value*>(1,ConstantUInt::get(Type::UIntTy, inc)),
"", InsertPos);
Instruction *ldInst=new LoadInst(Idx, "ti1"); Instruction *ldInst=new LoadInst(Idx, "ti1", InsertPos);
Value *val = ConstantSInt::get(Type::IntTy, 1); Value *val = ConstantSInt::get(Type::IntTy, 1);
Instruction *addIn = Instruction *addIn =
BinaryOperator::create(Instruction::Add, ldInst, val,"ti2"); BinaryOperator::create(Instruction::Add, ldInst, val,"ti2", InsertPos);
#ifdef INSERT_STORE
Instruction *stInst=new StoreInst(addIn, Idx, InsertPos);
#endif
//insert trigger //insert trigger
getTriggerCode(M->getParent(), BB, MethNo, getTriggerCode(M->getParent(), BB, MethNo,
ConstantSInt::get(Type::IntTy,inc), addIn); ConstantSInt::get(Type::IntTy,inc), addIn, InsertPos);
here=instList.begin();
//end trigger code //end trigger code
assert(inc>=0 && "IT MUST BE POSITIVE NOW"); assert(inc>=0 && "IT MUST BE POSITIVE NOW");
#ifdef INSERT_STORE
Instruction *stInst=new StoreInst(addIn, Idx);
#endif
here = ++instList.insert(here,Idx);
here = ++instList.insert(here,ldInst);
here = ++instList.insert(here,addIn);
#ifdef INSERT_STORE
here = ++instList.insert(here,stInst);
#endif
break; break;
} }
//case: count[r+inc]++ //case: count[r+inc]++
case 5:{ case 5:{
//ti1=inc+r //ti1=inc+r
Instruction *ldIndex=new LoadInst(rInst, "ti1"); Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos);
Value *val=ConstantSInt::get(Type::IntTy,inc); Value *val=ConstantSInt::get(Type::IntTy,inc);
Instruction *addIndex=BinaryOperator:: Instruction *addIndex=BinaryOperator::
create(Instruction::Add, ldIndex, val,"ti2"); create(Instruction::Add, ldIndex, val,"ti2", InsertPos);
//erase following 1 line //erase following 1 line
//Value *valtemp=ConstantSInt::get(Type::IntTy,999); //Value *valtemp=ConstantSInt::get(Type::IntTy,999);
//now load count[addIndex] //now load count[addIndex]
Instruction *castInst=new CastInst(addIndex, Instruction *castInst=new CastInst(addIndex,
Type::UIntTy,"ctin"); Type::UIntTy,"ctin", InsertPos);
Instruction *Idx = new GetElementPtrInst(countInst, Instruction *Idx = new GetElementPtrInst(countInst,
vector<Value*>(1,castInst)); vector<Value*>(1,castInst), "",
InsertPos);
Instruction *ldInst=new LoadInst(Idx, "ti3"); Instruction *ldInst=new LoadInst(Idx, "ti3", InsertPos);
Value *cons=ConstantSInt::get(Type::IntTy,1); Value *cons=ConstantSInt::get(Type::IntTy,1);
//count[addIndex]++ //count[addIndex]++
Instruction *addIn=BinaryOperator:: Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, cons,
create(Instruction::Add, ldInst, cons,"ti4"); "ti4", InsertPos);
//insert trigger
getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn);
here=instList.begin();
//end trigger code
#ifdef INSERT_STORE #ifdef INSERT_STORE
///* ///*
Instruction *stInst=new StoreInst(addIn, Idx); new StoreInst(addIn, Idx, InsertPos);
//*/ //*/
#endif #endif
here = ++instList.insert(here,ldIndex);
here = ++instList.insert(here,addIndex); //insert trigger
here = ++instList.insert(here,castInst); getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn, InsertPos);
here = ++instList.insert(here,Idx); //end trigger code
here = ++instList.insert(here,ldInst);
here = ++instList.insert(here,addIn);
#ifdef INSERT_STORE
here = ++instList.insert(here,stInst);
#endif
break; break;
} }
//case: count[r]+ //case: count[r]+
case 6:{ case 6:{
//ti1=inc+r //ti1=inc+r
Instruction *ldIndex=new LoadInst(rInst, "ti1"); Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos);
//now load count[addIndex] //now load count[addIndex]
Instruction *castInst2=new CastInst(ldIndex, Type::UIntTy,"ctin"); Instruction *castInst2=new CastInst(ldIndex, Type::UIntTy,"ctin",InsertPos);
Instruction *Idx = new GetElementPtrInst(countInst, Instruction *Idx = new GetElementPtrInst(countInst,
vector<Value*>(1,castInst2)); vector<Value*>(1,castInst2), "",
InsertPos);
Instruction *ldInst=new LoadInst(Idx, "ti2"); Instruction *ldInst=new LoadInst(Idx, "ti2", InsertPos);
Value *cons=ConstantSInt::get(Type::IntTy,1); Value *cons=ConstantSInt::get(Type::IntTy,1);
//count[addIndex]++ //count[addIndex]++
Instruction *addIn=BinaryOperator::create(Instruction::Add, ldInst, Instruction *addIn=BinaryOperator::create(Instruction::Add, ldInst,
cons,"ti3"); cons,"ti3", InsertPos);
#ifdef INSERT_STORE
new StoreInst(addIn, Idx, InsertPos);
#endif
//insert trigger //insert trigger
getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn); getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn, InsertPos);
here=instList.begin();
//end trigger code //end trigger code
#ifdef INSERT_STORE
Instruction *stInst=new StoreInst(addIn, Idx);
#endif
here = ++instList.insert(here,ldIndex);
here = ++instList.insert(here,castInst2);
here = ++instList.insert(here,Idx);
here = instList.insert(here,ldInst);
here = instList.insert(here,addIn);
#ifdef INSERT_STORE
here = instList.insert(here,stInst);
#endif
break; break;
} }
@ -288,28 +240,22 @@ void insertInTopBB(BasicBlock *front,
vector<Value *> idx; vector<Value *> idx;
idx.push_back(ConstantUInt::get(Type::UIntTy, 0)); idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
Instruction *GEP = new GetElementPtrInst(rVar, idx);
Instruction *stInstr=new StoreInst(Int0, GEP);
//now push all instructions in front of the BB //now push all instructions in front of the BB
BasicBlock::InstListType& instList=front->getInstList(); BasicBlock::iterator here=front->begin();
BasicBlock::iterator here=instList.begin(); front->getInstList().insert(here, rVar);
here=++front->getInstList().insert(here, rVar); front->getInstList().insert(here,countVar);
here=++front->getInstList().insert(here,countVar);
//Initialize Count[...] with 0 //Initialize Count[...] with 0
for(int i=0;i<k; i++){ for (int i=0;i<k; i++){
Instruction *GEP2 = new GetElementPtrInst(countVar, Value *GEP2 = new GetElementPtrInst(countVar,
std::vector<Value *>(1,ConstantUInt::get(Type::UIntTy, i))); vector<Value *>(1,ConstantUInt::get(Type::UIntTy, i)),
here=++front->getInstList().insert(here,GEP2); "", here);
new StoreInst(Int0, GEP2, here);
Instruction *stInstrC=new StoreInst(Int0, GEP2);
here=++front->getInstList().insert(here,stInstrC);
} }
here = ++front->getInstList().insert(here,GEP); Instruction *GEP = new GetElementPtrInst(rVar, idx, "", here);
here = ++front->getInstList().insert(here,stInstr); new StoreInst(Int0, GEP, here);
} }