[llvm-commits] [poolalloc] r133445 - in /poolalloc/trunk: include/assistDS/TypeChecks.h lib/AssistDS/TypeChecks.cpp
Arushi Aggarwal
aggarwa4 at illinois.edu
Mon Jun 20 09:56:57 PDT 2011
Author: aggarwa4
Date: Mon Jun 20 11:56:57 2011
New Revision: 133445
URL: http://llvm.org/viewvc/llvm-project?rev=133445&view=rev
Log:
Handle calls to va_copy, and add a new counter for the
values in the copied va_list.
Modified:
poolalloc/trunk/include/assistDS/TypeChecks.h
poolalloc/trunk/lib/AssistDS/TypeChecks.cpp
Modified: poolalloc/trunk/include/assistDS/TypeChecks.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/assistDS/TypeChecks.h?rev=133445&r1=133444&r2=133445&view=diff
==============================================================================
--- poolalloc/trunk/include/assistDS/TypeChecks.h (original)
+++ poolalloc/trunk/include/assistDS/TypeChecks.h Mon Jun 20 11:56:57 2011
@@ -42,6 +42,8 @@
std::list<Function *> ByValFunctions;
std::list<Function *> AddressTakenFunctions;
std::set<Instruction*> IndCalls;
+ // Map of VAList to current count
+ std::map<Value*, Value*> CounterMap;
// Analysis from other passes.
TargetData *TD;
Modified: poolalloc/trunk/lib/AssistDS/TypeChecks.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/TypeChecks.cpp?rev=133445&r1=133444&r2=133445&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/TypeChecks.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/TypeChecks.cpp Mon Jun 20 11:56:57 2011
@@ -260,6 +260,7 @@
ByValFunctions.pop_back();
modified |= visitByValFunction(M, *F);
}
+
// NOTE:must visit before VAArgFunctions, to populate the map with the
// correct cloned functions.
@@ -468,10 +469,24 @@
Function *CalledF = dyn_cast<Function>(CI->getCalledFunction());
if(VAListFunctionsMap.find(CalledF) == VAListFunctionsMap.end())
continue;
+ const Type *ListType = F->getParent()->getTypeByName("struct.__va_list_tag");
+ const Type *ListPtrType = ListType->getPointerTo();
+ unsigned VAListArgNum = 0;
+ for (Function::arg_iterator I = CalledF->arg_begin();
+ I != CalledF->arg_end(); ++I) {
+ VAListArgNum ++;
+ if(I->getType() == ListPtrType) {
+ break;
+ }
+ }
+
Function::arg_iterator NII = F->arg_begin();
std::vector<Value *>Args;
Args.push_back(NII++); // total count
- Args.push_back(NII++); // current count
+ Value *CounterSrc = CounterMap[CI->getOperand(VAListArgNum)->stripPointerCasts()];
+ LoadInst *CountValue = new LoadInst(CounterSrc, "count", CI);
+ Args.push_back(CountValue); // current count
+ NII++;
Args.push_back(NII); // MD
for(unsigned i = 1 ;i < CI->getNumOperands(); i++) {
// Add the original argument
@@ -559,6 +574,7 @@
VAListFunctionsMap[&F_orig] = F;
inst_iterator InsPt = inst_begin(F);
+
// Store the information
Function::arg_iterator NII = F->arg_begin();
AllocaInst *VASizeLoc = new AllocaInst(Int64Ty, "", &*InsPt);
@@ -566,9 +582,33 @@
NII++;
AllocaInst *Counter = new AllocaInst(Int64Ty, "",&*InsPt);
new StoreInst(NII, Counter, &*InsPt);
+ CounterMap[NII] = Counter;
NII++;
AllocaInst *VAMDLoc = new AllocaInst(VoidPtrTy, "", &*InsPt);
new StoreInst(NII, VAMDLoc, &*InsPt);
+
+ // Find all va_copy calls
+ for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
+ for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;I++) {
+ CallInst *CI = dyn_cast<CallInst>(I);
+ if(!CI)
+ continue;
+ Function *CalledF = dyn_cast<Function>(CI->getCalledFunction());
+ if(!CalledF)
+ continue;
+ if(!CalledF->isIntrinsic())
+ continue;
+ if(CalledF->getIntrinsicID() != Intrinsic::vacopy)
+ continue;
+ // Reinitialize the counter
+ AllocaInst *CounterDest = new AllocaInst(Int64Ty, "",&*InsPt);
+ Value *CounterSource = CounterMap[CI->getOperand(2)->stripPointerCasts()];
+ LoadInst *CurrentValue = new LoadInst(CounterSource, "count", CI);
+ new StoreInst(CurrentValue, CounterDest, CI);
+ CounterMap[CI->getOperand(1)->stripPointerCasts()] = CounterDest;
+ }
+ }
+
// instrument va_arg to increment the counter
for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
@@ -577,13 +617,14 @@
if(!VI)
continue;
Constant *One = ConstantInt::get(Int64Ty, 1);
- LoadInst *OldValue = new LoadInst(Counter, "count", VI);
+ Value *CounterSrc = CounterMap[VI->getOperand(0)->stripPointerCasts()];
+ LoadInst *OldValue = new LoadInst(CounterSrc, "count", VI);
Instruction *NewValue = BinaryOperator::Create(BinaryOperator::Add,
OldValue,
One,
"count",
VI);
- new StoreInst(NewValue, Counter, VI);
+ new StoreInst(NewValue, CounterSrc, VI);
std::vector<Value *> Args;
Instruction *VASize = new LoadInst(VASizeLoc, "", VI);
Instruction *VAMetaData = new LoadInst(VAMDLoc, "", VI);
@@ -804,6 +845,49 @@
AllocaInst *Counter = new AllocaInst(Int64Ty, "",&*InsPt);
new StoreInst(ConstantInt::get(Int64Ty, 0), Counter, &*InsPt);
+
+ // visit all VAStarts and initialize the counter
+ for (Function::iterator B = NewF->begin(), FE = NewF->end(); B != FE; ++B) {
+ for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;I++) {
+ CallInst *CI = dyn_cast<CallInst>(I);
+ if(!CI)
+ continue;
+ Function *CalledF = dyn_cast<Function>(CI->getCalledFunction());
+ if(!CalledF)
+ continue;
+ if(!CalledF->isIntrinsic())
+ continue;
+ if(CalledF->getIntrinsicID() != Intrinsic::vastart)
+ continue;
+ // Reinitialize the counter
+ StoreInst *SI3 = new StoreInst(ConstantInt::get(Int64Ty, 0), Counter);
+ SI3->insertAfter(CI);
+ CounterMap[CI->getOperand(1)->stripPointerCasts()] = Counter;
+ }
+ }
+
+ // Find all va_copy calls
+ for (Function::iterator B = NewF->begin(), FE = NewF->end(); B != FE; ++B) {
+ for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;I++) {
+ CallInst *CI = dyn_cast<CallInst>(I);
+ if(!CI)
+ continue;
+ Function *CalledF = dyn_cast<Function>(CI->getCalledFunction());
+ if(!CalledF)
+ continue;
+ if(!CalledF->isIntrinsic())
+ continue;
+ if(CalledF->getIntrinsicID() != Intrinsic::vacopy)
+ continue;
+ // Reinitialize the counter
+ AllocaInst *CounterDest = new AllocaInst(Int64Ty, "",&*InsPt);
+ Value *CounterSource = CounterMap[CI->getOperand(2)->stripPointerCasts()];
+ LoadInst *CurrentValue = new LoadInst(CounterSource, "count", CI);
+ new StoreInst(CurrentValue, CounterDest, CI);
+ CounterMap[CI->getOperand(1)->stripPointerCasts()] = CounterDest;
+ }
+ }
+
// Modify function to add checks on every var_arg call to ensure that we
// are not accessing more arguments than we passed in.
@@ -814,13 +898,14 @@
if(!VI)
continue;
Constant *One = ConstantInt::get(Int64Ty, 1);
- LoadInst *OldValue = new LoadInst(Counter, "count", VI);
+ Value *CounterSrc = CounterMap[VI->getOperand(0)->stripPointerCasts()];
+ LoadInst *OldValue = new LoadInst(CounterSrc, "count", VI);
Instruction *NewValue = BinaryOperator::Create(BinaryOperator::Add,
OldValue,
One,
"count",
VI);
- new StoreInst(NewValue, Counter, VI);
+ new StoreInst(NewValue, CounterSrc, VI);
std::vector<Value *> Args;
Instruction *VASize = new LoadInst(VASizeLoc, "", VI);
Instruction *VAMetaData = new LoadInst(VAMDLoc, "", VI);
@@ -836,26 +921,6 @@
}
}
- // visit all VAStarts and initialize the counter
- CallInst *VAStart = NULL;
- for (Function::iterator B = NewF->begin(), FE = NewF->end(); B != FE; ++B) {
- for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
- CallInst *CI = dyn_cast<CallInst>(I++);
- if(!CI)
- continue;
- Function *CalledF = dyn_cast<Function>(CI->getCalledFunction());
- if(!CalledF)
- continue;
- if(!CalledF->isIntrinsic())
- continue;
- if(CalledF->getIntrinsicID() != Intrinsic::vastart)
- continue;
- VAStart = CI;
- // Reinitialize the counter
- StoreInst *SI3 = new StoreInst(ConstantInt::get(Int64Ty, 0), Counter);
- SI3->insertAfter(CI);
- }
- }
// modify calls to va list functions to pass the metadata
for (Function::iterator B = NewF->begin(), FE = NewF->end(); B != FE; ++B) {
@@ -864,11 +929,24 @@
if(!CI)
continue;
Function *CalledF = dyn_cast<Function>(CI->getCalledFunction());
+
if(VAListFunctionsMap.find(CalledF) == VAListFunctionsMap.end())
continue;
+ const Type *ListType = M.getTypeByName("struct.__va_list_tag");
+ const Type *ListPtrType = ListType->getPointerTo();
+ unsigned VAListArgNum = 0;
+ for (Function::arg_iterator I = CalledF->arg_begin();
+ I != CalledF->arg_end(); ++I) {
+ VAListArgNum ++;
+ if(I->getType() == ListPtrType) {
+ break;
+ }
+ }
std::vector<Value *>Args;
+
+ Value *CounterSrc = CounterMap[CI->getOperand(VAListArgNum)->stripPointerCasts()];
Instruction *VASize = new LoadInst(VASizeLoc, "", CI);
- Instruction *VACounter = new LoadInst(Counter, "", CI);
+ Instruction *VACounter = new LoadInst(CounterSrc, "", CI);
Instruction *VAMetaData = new LoadInst(VAMDLoc, "", CI);
Args.push_back(VASize); // toatl count
Args.push_back(VACounter); // current count
More information about the llvm-commits
mailing list