[llvm-commits] [poolalloc] r130415 - in /poolalloc/trunk/lib/AssistDS: GEPExprArg.cpp LoadArgs.cpp

Arushi Aggarwal aggarwa4 at illinois.edu
Thu Apr 28 11:11:37 PDT 2011


Author: aggarwa4
Date: Thu Apr 28 13:11:37 2011
New Revision: 130415

URL: http://llvm.org/viewvc/llvm-project?rev=130415&view=rev
Log:
Repeat in a loop, to find more cases.

Modified:
    poolalloc/trunk/lib/AssistDS/GEPExprArg.cpp
    poolalloc/trunk/lib/AssistDS/LoadArgs.cpp

Modified: poolalloc/trunk/lib/AssistDS/GEPExprArg.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/GEPExprArg.cpp?rev=130415&r1=130414&r2=130415&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/GEPExprArg.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/GEPExprArg.cpp Thu Apr 28 13:11:37 2011
@@ -35,112 +35,117 @@
     static char ID;
     GEPExprArg() : ModulePass(&ID) {}
     bool runOnModule(Module& M) {
-      for (Module::iterator F = M.begin(); F != M.end(); ++F){
-        for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
-          for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
-            CallInst *CI = dyn_cast<CallInst>(I++);
-            if(!CI)
-              continue;
-
-            if(CI->hasByValArgument())
-              continue;
-            // if the GEP calls a function, that is externally defined,
-            // or might be changed, ignore this call site.
-            Function *F = CI->getCalledFunction();
-
-            if (!F || (F->isDeclaration() || F->mayBeOverridden())) 
-              continue;
-            if(F->hasStructRetAttr())
-              continue;
-            if(F->isVarArg())
-              continue;
-
-            // find the argument we must replace
-            Function::arg_iterator ai = F->arg_begin(), ae = F->arg_end();
-            unsigned argNum = 1;
-            for(; argNum < CI->getNumOperands();argNum++, ++ai) {
-              if(ai->use_empty())
+      bool changed;
+      do {
+        changed = false;
+        for (Module::iterator F = M.begin(); F != M.end(); ++F){
+          for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
+            for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
+              CallInst *CI = dyn_cast<CallInst>(I++);
+              if(!CI)
                 continue;
-              if (isa<GEPOperator>(CI->getOperand(argNum)))
-                break;
-            }
 
-            // if no argument was a GEP operator to be changed 
-            if(ai == ae)
-              continue;
-
-            GEPOperator *GEP = dyn_cast<GEPOperator>(CI->getOperand(argNum));
-            if(!GEP->hasAllConstantIndices())
-              continue;
-
-            // Construct the new Type
-            // Appends the struct Type at the beginning
-            std::vector<const Type*>TP;
-            TP.push_back(GEP->getPointerOperand()->getType());
-            for(unsigned c = 1; c < CI->getNumOperands();c++) {
-              TP.push_back(CI->getOperand(c)->getType());
-            }
+              if(CI->hasByValArgument())
+                continue;
+              // if the GEP calls a function, that is externally defined,
+              // or might be changed, ignore this call site.
+              Function *F = CI->getCalledFunction();
 
-            //return type is same as that of original instruction
-            const FunctionType *NewFTy = FunctionType::get(CI->getType(), TP, false);
-            Function *NewF;
-            numSimplified++;
-            if(numSimplified > 1000) 
-              return true;
-
-            NewF = Function::Create(NewFTy,
-                                    GlobalValue::InternalLinkage,
-                                    F->getNameStr() + ".TEST",
-                                    &M);
-
-            Function::arg_iterator NI = NewF->arg_begin();
-            NI->setName("Sarg");
-            ++NI;
-
-            DenseMap<const Value*, Value*> ValueMap;
-
-            for (Function::arg_iterator II = F->arg_begin(); NI != NewF->arg_end(); ++II, ++NI) {
-              ValueMap[II] = NI;
-              NI->setName(II->getName());
-            }
-            // Perform the cloning.
-            SmallVector<ReturnInst*,100> Returns;
-            CloneFunctionInto(NewF, F, ValueMap, Returns);
-            std::vector<Value*> fargs;
-            for(Function::arg_iterator ai = NewF->arg_begin(), 
-                ae= NewF->arg_end(); ai != ae; ++ai) {
-              fargs.push_back(ai);
-            }
+              if (!F || (F->isDeclaration() || F->mayBeOverridden())) 
+                continue;
+              if(F->hasStructRetAttr())
+                continue;
+              if(F->isVarArg())
+                continue;
 
-            NewF->setAlignment(F->getAlignment());
-            //Get the point to insert the GEP instr.
-            NI = NewF->arg_begin();
-            SmallVector<Value*, 8> Ops(CI->op_begin()+1, CI->op_end());
-            Instruction *InsertPoint;
-            for (BasicBlock::iterator insrt = NewF->front().begin(); isa<AllocaInst>(InsertPoint = insrt); ++insrt) {;}
-
-            SmallVector<Value*, 8> Indices;
-            Indices.append(GEP->op_begin()+1, GEP->op_end());
-            GetElementPtrInst *GEP_new = GetElementPtrInst::Create(cast<Value>(NI), Indices.begin(), Indices.end(), "", InsertPoint);
-            fargs.at(argNum)->replaceAllUsesWith(GEP_new);
-            unsigned j = argNum + 1;
-            for(; j < CI->getNumOperands();j++) {
-              if(CI->getOperand(j) == GEP)
-                fargs.at(j)->replaceAllUsesWith(GEP_new);
-            }
+              // find the argument we must replace
+              Function::arg_iterator ai = F->arg_begin(), ae = F->arg_end();
+              unsigned argNum = 1;
+              for(; argNum < CI->getNumOperands();argNum++, ++ai) {
+                if(ai->use_empty())
+                  continue;
+                if (isa<GEPOperator>(CI->getOperand(argNum)))
+                  break;
+              }
+
+              // if no argument was a GEP operator to be changed 
+              if(ai == ae)
+                continue;
+
+              GEPOperator *GEP = dyn_cast<GEPOperator>(CI->getOperand(argNum));
+              if(!GEP->hasAllConstantIndices())
+                continue;
 
-            SmallVector<Value*, 8> Args;
-            Args.push_back(GEP->getPointerOperand());
-            for(unsigned j =1;j<CI->getNumOperands();j++) {
-              Args.push_back(CI->getOperand(j));
+              // Construct the new Type
+              // Appends the struct Type at the beginning
+              std::vector<const Type*>TP;
+              TP.push_back(GEP->getPointerOperand()->getType());
+              for(unsigned c = 1; c < CI->getNumOperands();c++) {
+                TP.push_back(CI->getOperand(c)->getType());
+              }
+
+              //return type is same as that of original instruction
+              const FunctionType *NewFTy = FunctionType::get(CI->getType(), TP, false);
+              Function *NewF;
+              numSimplified++;
+              if(numSimplified > 1000) 
+                return true;
+
+              NewF = Function::Create(NewFTy,
+                                      GlobalValue::InternalLinkage,
+                                      F->getNameStr() + ".TEST",
+                                      &M);
+
+              Function::arg_iterator NI = NewF->arg_begin();
+              NI->setName("Sarg");
+              ++NI;
+
+              DenseMap<const Value*, Value*> ValueMap;
+
+              for (Function::arg_iterator II = F->arg_begin(); NI != NewF->arg_end(); ++II, ++NI) {
+                ValueMap[II] = NI;
+                NI->setName(II->getName());
+              }
+              // Perform the cloning.
+              SmallVector<ReturnInst*,100> Returns;
+              CloneFunctionInto(NewF, F, ValueMap, Returns);
+              std::vector<Value*> fargs;
+              for(Function::arg_iterator ai = NewF->arg_begin(), 
+                  ae= NewF->arg_end(); ai != ae; ++ai) {
+                fargs.push_back(ai);
+              }
+
+              NewF->setAlignment(F->getAlignment());
+              //Get the point to insert the GEP instr.
+              NI = NewF->arg_begin();
+              SmallVector<Value*, 8> Ops(CI->op_begin()+1, CI->op_end());
+              Instruction *InsertPoint;
+              for (BasicBlock::iterator insrt = NewF->front().begin(); isa<AllocaInst>(InsertPoint = insrt); ++insrt) {;}
+
+              SmallVector<Value*, 8> Indices;
+              Indices.append(GEP->op_begin()+1, GEP->op_end());
+              GetElementPtrInst *GEP_new = GetElementPtrInst::Create(cast<Value>(NI), Indices.begin(), Indices.end(), "", InsertPoint);
+              fargs.at(argNum)->replaceAllUsesWith(GEP_new);
+              unsigned j = argNum + 1;
+              for(; j < CI->getNumOperands();j++) {
+                if(CI->getOperand(j) == GEP)
+                  fargs.at(j)->replaceAllUsesWith(GEP_new);
+              }
+
+              SmallVector<Value*, 8> Args;
+              Args.push_back(GEP->getPointerOperand());
+              for(unsigned j =1;j<CI->getNumOperands();j++) {
+                Args.push_back(CI->getOperand(j));
+              }
+              CallInst *CallI = CallInst::Create(NewF,Args.begin(), Args.end(),"", CI);
+              CallI->setCallingConv(CI->getCallingConv());
+              CI->replaceAllUsesWith(CallI);
+              CI->eraseFromParent();
+              changed = true;
             }
-            CallInst *CallI = CallInst::Create(NewF,Args.begin(), Args.end(),"", CI);
-            CallI->setCallingConv(CI->getCallingConv());
-            CI->replaceAllUsesWith(CallI);
-            CI->eraseFromParent();
           }
         }
-      }
+      } while(changed);
       return true;
     }
   };

Modified: poolalloc/trunk/lib/AssistDS/LoadArgs.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/LoadArgs.cpp?rev=130415&r1=130414&r2=130415&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/LoadArgs.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/LoadArgs.cpp Thu Apr 28 13:11:37 2011
@@ -35,125 +35,130 @@
     static char ID;
     LoadArgs() : ModulePass(&ID) {}
     bool runOnModule(Module& M) {
-      for (Module::iterator F = M.begin(); F != M.end(); ++F){
-        for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
-          for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
-            CallInst *CI = dyn_cast<CallInst>(I++);
-            if(!CI)
-              continue;
-
-            if(CI->hasByValArgument())
-              continue;
-            // if the GEP calls a function, that is externally defined,
-            // or might be changed, ignore this call site.
-            Function *F = CI->getCalledFunction();
-
-            if (!F || (F->isDeclaration() || F->mayBeOverridden())) 
-              continue;
-            if(F->hasStructRetAttr())
-              continue;
-            if(F->isVarArg())
-              continue;
-
-            // find the argument we must replace
-            Function::arg_iterator ai = F->arg_begin(), ae = F->arg_end();
-            unsigned argNum = 1;
-            for(; argNum < CI->getNumOperands();argNum++, ++ai) {
-              if(ai->use_empty())
+      bool changed;
+      do { 
+        changed = false;
+        for (Module::iterator F = M.begin(); F != M.end(); ++F){
+          for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
+            for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
+              CallInst *CI = dyn_cast<CallInst>(I++);
+              if(!CI)
                 continue;
-              if(F->paramHasAttr(argNum, Attribute::SExt) ||
-                 F->paramHasAttr(argNum, Attribute::ZExt)) 
+
+              if(CI->hasByValArgument())
                 continue;
-              if (isa<LoadInst>(CI->getOperand(argNum)))
-                break;
-            }
+              // if the GEP calls a function, that is externally defined,
+              // or might be changed, ignore this call site.
+              Function *F = CI->getCalledFunction();
 
-            // if no argument was a GEP operator to be changed 
-            if(ai == ae)
-              continue;
-
-            LoadInst *LI = dyn_cast<LoadInst>(CI->getOperand(argNum));
-            if(LI->getParent() != CI->getParent())
-              continue;
-            // Also check that there is no store after the load.
-            // TODO: Check if the load/store do not alias.
-            BasicBlock::iterator bii = LI->getParent()->begin();
-            Instruction *BII = bii;
-            while(BII != LI) {
-              ++bii;
-              BII = bii;
-            }
-            while(BII != CI) {
-              if(isa<StoreInst>(BII))
-                break;
-              ++bii;
-              BII = bii;
-            }
-            if(isa<StoreInst>(bii)){
-              continue;
-            }
+              if (!F || (F->isDeclaration() || F->mayBeOverridden())) 
+                continue;
+              if(F->hasStructRetAttr())
+                continue;
+              if(F->isVarArg())
+                continue;
 
-            // Construct the new Type
-            // Appends the struct Type at the beginning
-            std::vector<const Type*>TP;
-            TP.push_back(LI->getOperand(0)->getType());
-            for(unsigned c = 1; c < CI->getNumOperands();c++) {
-              TP.push_back(CI->getOperand(c)->getType());
-            }
+              // find the argument we must replace
+              Function::arg_iterator ai = F->arg_begin(), ae = F->arg_end();
+              unsigned argNum = 1;
+              for(; argNum < CI->getNumOperands();argNum++, ++ai) {
+                if(ai->use_empty())
+                  continue;
+                if(F->paramHasAttr(argNum, Attribute::SExt) ||
+                   F->paramHasAttr(argNum, Attribute::ZExt)) 
+                  continue;
+                if (isa<LoadInst>(CI->getOperand(argNum)))
+                  break;
+              }
 
-            //return type is same as that of original instruction
-            const FunctionType *NewFTy = FunctionType::get(CI->getType(), TP, false);
-            Function *NewF;
-            numSimplified++;
-            if(numSimplified > 500) //26
-              return true;
-
-            NewF = Function::Create(NewFTy,
-                                    GlobalValue::InternalLinkage,
-                                    F->getNameStr() + ".TEST",
-                                    &M);
-
-            Function::arg_iterator NI = NewF->arg_begin();
-            NI->setName("Sarg");
-            ++NI;
-
-            DenseMap<const Value*, Value*> ValueMap;
-
-            for (Function::arg_iterator II = F->arg_begin(); NI != NewF->arg_end(); ++II, ++NI) {
-              ValueMap[II] = NI;
-              NI->setName(II->getName());
-            }
-            // Perform the cloning.
-            SmallVector<ReturnInst*,100> Returns;
-            CloneFunctionInto(NewF, F, ValueMap, Returns);
-            std::vector<Value*> fargs;
-            for(Function::arg_iterator ai = NewF->arg_begin(), 
-                ae= NewF->arg_end(); ai != ae; ++ai) {
-              fargs.push_back(ai);
-            }
+              // if no argument was a GEP operator to be changed 
+              if(ai == ae)
+                continue;
+
+              LoadInst *LI = dyn_cast<LoadInst>(CI->getOperand(argNum));
+              if(LI->getParent() != CI->getParent())
+                continue;
+              // Also check that there is no store after the load.
+              // TODO: Check if the load/store do not alias.
+              BasicBlock::iterator bii = LI->getParent()->begin();
+              Instruction *BII = bii;
+              while(BII != LI) {
+                ++bii;
+                BII = bii;
+              }
+              while(BII != CI) {
+                if(isa<StoreInst>(BII))
+                  break;
+                ++bii;
+                BII = bii;
+              }
+              if(isa<StoreInst>(bii)){
+                continue;
+              }
 
-            NewF->setAlignment(F->getAlignment());
-            //Get the point to insert the GEP instr.
-            NI = NewF->arg_begin();
-            SmallVector<Value*, 8> Ops(CI->op_begin()+1, CI->op_end());
-            Instruction *InsertPoint;
-            for (BasicBlock::iterator insrt = NewF->front().begin(); isa<AllocaInst>(InsertPoint = insrt); ++insrt) {;}
-
-            LoadInst *LI_new = new LoadInst(cast<Value>(NI), "", InsertPoint);
-            fargs.at(argNum)->replaceAllUsesWith(LI_new);
-
-            SmallVector<Value*, 8> Args;
-            Args.push_back(LI->getOperand(0));
-            for(unsigned j =1;j<CI->getNumOperands();j++) {
-              Args.push_back(CI->getOperand(j));
+              // Construct the new Type
+              // Appends the struct Type at the beginning
+              std::vector<const Type*>TP;
+              TP.push_back(LI->getOperand(0)->getType());
+              for(unsigned c = 1; c < CI->getNumOperands();c++) {
+                TP.push_back(CI->getOperand(c)->getType());
+              }
+
+              //return type is same as that of original instruction
+              const FunctionType *NewFTy = FunctionType::get(CI->getType(), TP, false);
+              Function *NewF;
+              numSimplified++;
+              if(numSimplified > 500) //26
+                return true;
+
+              NewF = Function::Create(NewFTy,
+                                      GlobalValue::InternalLinkage,
+                                      F->getNameStr() + ".TEST",
+                                      &M);
+
+              Function::arg_iterator NI = NewF->arg_begin();
+              NI->setName("Sarg");
+              ++NI;
+
+              DenseMap<const Value*, Value*> ValueMap;
+
+              for (Function::arg_iterator II = F->arg_begin(); NI != NewF->arg_end(); ++II, ++NI) {
+                ValueMap[II] = NI;
+                NI->setName(II->getName());
+              }
+              // Perform the cloning.
+              SmallVector<ReturnInst*,100> Returns;
+              CloneFunctionInto(NewF, F, ValueMap, Returns);
+              std::vector<Value*> fargs;
+              for(Function::arg_iterator ai = NewF->arg_begin(), 
+                  ae= NewF->arg_end(); ai != ae; ++ai) {
+                fargs.push_back(ai);
+              }
+
+              NewF->setAlignment(F->getAlignment());
+              //Get the point to insert the GEP instr.
+              NI = NewF->arg_begin();
+              SmallVector<Value*, 8> Ops(CI->op_begin()+1, CI->op_end());
+              Instruction *InsertPoint;
+              for (BasicBlock::iterator insrt = NewF->front().begin(); isa<AllocaInst>(InsertPoint = insrt); ++insrt) {;}
+
+              LoadInst *LI_new = new LoadInst(cast<Value>(NI), "", InsertPoint);
+              fargs.at(argNum)->replaceAllUsesWith(LI_new);
+
+              SmallVector<Value*, 8> Args;
+              Args.push_back(LI->getOperand(0));
+              for(unsigned j =1;j<CI->getNumOperands();j++) {
+                Args.push_back(CI->getOperand(j));
+              }
+              CallInst *CallI = CallInst::Create(NewF,Args.begin(), Args.end(),"", CI);
+              CallI->setCallingConv(CI->getCallingConv());
+              CI->replaceAllUsesWith(CallI);
+              CI->eraseFromParent();
+              changed = true;
             }
-            CallInst *CallI = CallInst::Create(NewF,Args.begin(), Args.end(),"", CI);
-            CallI->setCallingConv(CI->getCallingConv());
-            CI->replaceAllUsesWith(CallI);
-            CI->eraseFromParent();
           }
         }
-      }
+      } while(changed);
       return true;
     }
   };





More information about the llvm-commits mailing list