[llvm-commits] [poolalloc] r126409 - /poolalloc/trunk/lib/AssistDS/ArgSimplify.cpp
Arushi Aggarwal
aggarwa4 at illinois.edu
Thu Feb 24 11:30:13 PST 2011
Author: aggarwa4
Date: Thu Feb 24 13:30:13 2011
New Revision: 126409
URL: http://llvm.org/viewvc/llvm-project?rev=126409&view=rev
Log:
Added Comments.
Minor fixes.
Modified:
poolalloc/trunk/lib/AssistDS/ArgSimplify.cpp
Modified: poolalloc/trunk/lib/AssistDS/ArgSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/ArgSimplify.cpp?rev=126409&r1=126408&r2=126409&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/ArgSimplify.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/ArgSimplify.cpp Thu Feb 24 13:30:13 2011
@@ -24,8 +24,11 @@
using namespace llvm;
STATISTIC(numTransformable, "Number of Args changeable");
+
namespace {
+
static void simplify(Function *I, unsigned arg_count, const Type* type) {
+
for(Value::use_iterator ui = I->use_begin(), ue = I->use_end();
ui != ue; ++ui) {
if (Constant *C = dyn_cast<Constant>(ui)) {
@@ -34,28 +37,41 @@
if(CE->getOperand(0) == I) {
for(Value::use_iterator uii = CE->use_begin(), uee = CE->use_end();
uii != uee; ) {
- if (CallInst* CI = dyn_cast<CallInst>(uii)) {
- ++uii;
+ if (CallInst* CI = dyn_cast<CallInst>(uii++)) {
if(CI->getCalledValue() == CE) {
+ // if I is ever called as a bitcasted function
if(I->getReturnType() == CI->getType()){
+ // if the return types match.
if(I->arg_size() == (CI->getNumOperands()-1)){
+ // and the numeber of args match too
unsigned arg_count1 = 1;
bool change = true;
- for (Function::arg_iterator ii1 = I->arg_begin(), ee1 = I->arg_end();ii1 != ee1; ++ii1,arg_count1++) {
- if(ii1->getType() != CI->getOperand(arg_count1)->getType()) {
- if(arg_count1 == (arg_count + 1)) {
- continue;
+ for (Function::arg_iterator ii1 = I->arg_begin(), ee1 = I->arg_end();
+ ii1 != ee1; ++ii1,arg_count1++) {
+ if(arg_count1 == (arg_count + 1)) {
+ if(ii1->getType() == CI->getOperand(arg_count1)->getType()){
+ change = false;
+ break;
}
+ else
+ continue;
+ }
+ if(ii1->getType() != CI->getOperand(arg_count1)->getType()) {
change = false;
break;
}
}
+ // if all types match except the argument we are interested in
+
if(change){
+ // create a new function, to do the cast from ptr to int,
+ // and call the original function, with the casted value
std::vector<const Type*>TP;
for(unsigned c = 1; c<CI->getNumOperands();c++) {
TP.push_back(CI->getOperand(c)->getType());
}
- const FunctionType *NewFTy = FunctionType::get(CI->getType(), TP, false);
+ const FunctionType *NewFTy = FunctionType::
+ get(CI->getType(), TP, false);
Module *M = I->getParent();
Function *NewF = Function::Create(NewFTy,
@@ -63,32 +79,37 @@
"argbounce",
M);
std::vector<Value*> fargs;
- for(Function::arg_iterator ai = NewF->arg_begin(), ae= NewF->arg_end(); ai != ae; ++ai) {
+ for(Function::arg_iterator ai = NewF->arg_begin(),
+ ae= NewF->arg_end(); ai != ae; ++ai) {
fargs.push_back(ai);
ai->setName("arg");
}
Value *CastedVal;
- BasicBlock* entryBB = BasicBlock::Create (M->getContext(), "entry", NewF);
+ BasicBlock* entryBB = BasicBlock::
+ Create (M->getContext(), "entry", NewF);
+
if(type->isIntegerTy()){
- CastedVal = new PtrToIntInst(fargs.at(arg_count), type, "castd", entryBB);
+ CastedVal = new PtrToIntInst(fargs.at(arg_count),
+ type, "castd", entryBB);
} else {
- CastedVal = new BitCastInst(fargs.at(arg_count), type, "castd", entryBB);
+ CastedVal = new BitCastInst(fargs.at(arg_count),
+ type, "castd", entryBB);
}
SmallVector<Value*, 8> Args;
- for(Function::arg_iterator ai = NewF->arg_begin(), ae= NewF->arg_end(); ai != ae; ++ai) {
+ for(Function::arg_iterator ai = NewF->arg_begin(),
+ ae= NewF->arg_end(); ai != ae; ++ai) {
if(ai->getArgNo() == arg_count)
Args.push_back(CastedVal);
else
Args.push_back(ai);
}
- CallInst * CallI = CallInst::Create(I,Args.begin(), Args.end(),"", entryBB);
+ CallInst * CallI = CallInst::Create(I,Args.begin(),
+ Args.end(),"", entryBB);
if(CallI->getType()->isVoidTy())
ReturnInst::Create(M->getContext(), entryBB);
else
ReturnInst::Create(M->getContext(), CallI, entryBB);
- //new BitCastInst(fargs.at(ii->getArgNo()), ii->getType(), "test", entryBB);
- //new UnreachableInst (M.getContext(), entryBB);
CI->setCalledFunction(NewF);
numTransformable++;
@@ -96,8 +117,6 @@
}
}
}
- } else {
- ++uii;
}
}
}
@@ -111,8 +130,10 @@
public:
static char ID;
ArgSimplify() : ModulePass(&ID) {}
+
bool runOnModule(Module& M) {
- for (Module::iterator I = M.begin(); I != M.end(); ++I) {
+
+ for (Module::iterator I = M.begin(); I != M.end(); ++I)
if (!I->isDeclaration() && !I->mayBeOverridden()) {
if(I->getNameStr() == "main")
continue;
@@ -127,12 +148,14 @@
break;
}
}
- if(change){
+ // if this argument is only used in CMP instructions, we can
+ // replace it.
+ if(change) {
simplify(I, ii->getArgNo(), ii->getType());
}
}
}
- }
+
return true;
}
More information about the llvm-commits
mailing list