Fix typo in comment.
[oota-llvm.git] / lib / Transforms / IPO / ArgumentPromotion.cpp
index 5183d43bc7c2480567bee8e986f1ec3f7938159a..9a7bcc7c15a5c0c99f47988a26e34f9b14114727 100644 (file)
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
-#include <iostream>
+#include "llvm/Support/Compiler.h"
 #include <set>
 using namespace llvm;
 
-namespace {
-  Statistic<> NumArgumentsPromoted("argpromotion",
-                                   "Number of pointer arguments promoted");
-  Statistic<> NumAggregatesPromoted("argpromotion",
-                                    "Number of aggregate arguments promoted");
-  Statistic<> NumArgumentsDead("argpromotion",
-                               "Number of dead pointer args eliminated");
+STATISTIC(NumArgumentsPromoted , "Number of pointer arguments promoted");
+STATISTIC(NumAggregatesPromoted, "Number of aggregate arguments promoted");
+STATISTIC(NumArgumentsDead     , "Number of dead pointer args eliminated");
 
+namespace {
   /// ArgPromotion - The 'by reference' to 'by value' argument promotion pass.
   ///
-  struct ArgPromotion : public CallGraphSCCPass {
+  struct VISIBILITY_HIDDEN ArgPromotion : public CallGraphSCCPass {
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.addRequired<AliasAnalysis>();
       AU.addRequired<TargetData>();
@@ -66,17 +63,21 @@ namespace {
     }
 
     virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
+    static char ID; // Pass identification, replacement for typeid
+    ArgPromotion() : CallGraphSCCPass((intptr_t)&ID) {}
+
   private:
     bool PromoteArguments(CallGraphNode *CGN);
     bool isSafeToPromoteArgument(Argument *Arg) const;
     Function *DoPromotion(Function *F, std::vector<Argument*> &ArgsToPromote);
   };
 
+  char ArgPromotion::ID = 0;
   RegisterPass<ArgPromotion> X("argpromotion",
                                "Promote 'by reference' arguments to scalars");
 }
 
-ModulePass *llvm::createArgumentPromotionPass() {
+Pass *llvm::createArgumentPromotionPass() {
   return new ArgPromotion();
 }
 
@@ -164,7 +165,8 @@ static bool IsAlwaysValidPointer(Value *V) {
 static bool AllCalleesPassInValidPointerForArgument(Argument *Arg) {
   Function *Callee = Arg->getParent();
 
-  unsigned ArgNo = std::distance(Callee->arg_begin(), Function::arg_iterator(Arg));
+  unsigned ArgNo = std::distance(Callee->arg_begin(),
+                                 Function::arg_iterator(Arg));
 
   // Look at all call sites of the function.  At this pointer we know we only
   // have direct callees.
@@ -231,9 +233,9 @@ bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg) const {
       if (std::find(GEPIndices.begin(), GEPIndices.end(), Operands) ==
           GEPIndices.end()) {
         if (GEPIndices.size() == 3) {
-          DEBUG(std::cerr << "argpromotion disable promoting argument '"
-                << Arg->getName() << "' because it would require adding more "
-                << "than 3 arguments to the function.\n");
+          DOUT << "argpromotion disable promoting argument '"
+               << Arg->getName() << "' because it would require adding more "
+               << "than 3 arguments to the function.\n";
           // We limit aggregate promotion to only promoting up to three elements
           // of the aggregate.
           return false;
@@ -259,7 +261,6 @@ bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg) const {
   // it is safe to unconditionally load the pointer.  Use alias analysis to
   // check to see if the pointer is guaranteed to not be modified from entry of
   // the function to each of the load instructions.
-  Function &F = *Arg->getParent();
 
   // Because there could be several/many load instructions, remember which
   // blocks we know to be transparent to the load.
@@ -307,8 +308,8 @@ namespace {
       unsigned idx = 0;
       for (; idx < LHS.size() && idx < RHS.size(); ++idx) {
         if (LHS[idx] != RHS[idx]) {
-          return cast<ConstantInt>(LHS[idx])->getRawValue() <
-                 cast<ConstantInt>(RHS[idx])->getRawValue();
+          return cast<ConstantInt>(LHS[idx])->getZExtValue() <
+                 cast<ConstantInt>(RHS[idx])->getZExtValue();
         }
       }
 
@@ -374,7 +375,9 @@ Function *ArgPromotion::DoPromotion(Function *F,
       // Add a parameter to the function for each element passed in.
       for (ScalarizeTable::iterator SI = ArgIndices.begin(),
              E = ArgIndices.end(); SI != E; ++SI)
-        Params.push_back(GetElementPtrInst::getIndexedType(I->getType(), *SI));
+        Params.push_back(GetElementPtrInst::getIndexedType(I->getType(),
+                                                           &(*SI)[0],
+                                                           SI->size()));
 
       if (ArgIndices.size() == 1 && ArgIndices.begin()->empty())
         ++NumArgumentsPromoted;
@@ -389,7 +392,7 @@ Function *ArgPromotion::DoPromotion(Function *F,
   bool ExtraArgHack = false;
   if (Params.empty() && FTy->isVarArg()) {
     ExtraArgHack = true;
-    Params.push_back(Type::IntTy);
+    Params.push_back(Type::Int32Ty);
   }
   FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
 
@@ -425,7 +428,8 @@ Function *ArgPromotion::DoPromotion(Function *F,
           Value *V = *AI;
           LoadInst *OrigLoad = OriginalLoads[*SI];
           if (!SI->empty()) {
-            V = new GetElementPtrInst(V, *SI, V->getName()+".idx", Call);
+            V = new GetElementPtrInst(V, &(*SI)[0], SI->size(),
+                                      V->getName()+".idx", Call);
             AA.copyValue(OrigLoad->getOperand(0), V);
           }
           Args.push_back(new LoadInst(V, V->getName()+".val", Call));
@@ -434,7 +438,7 @@ Function *ArgPromotion::DoPromotion(Function *F,
       }
 
     if (ExtraArgHack)
-      Args.push_back(Constant::getNullValue(Type::IntTy));
+      Args.push_back(Constant::getNullValue(Type::Int32Ty));
 
     // Push any varargs arguments on the list
     for (; AI != CS.arg_end(); ++AI)
@@ -443,10 +447,10 @@ Function *ArgPromotion::DoPromotion(Function *F,
     Instruction *New;
     if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
       New = new InvokeInst(NF, II->getNormalDest(), II->getUnwindDest(),
-                           Args, "", Call);
+                           &Args[0], Args.size(), "", Call);
       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
     } else {
-      New = new CallInst(NF, Args, "", Call);
+      New = new CallInst(NF, &Args[0], Args.size(), "", Call);
       cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
       if (cast<CallInst>(Call)->isTailCall())
         cast<CallInst>(New)->setTailCall();
@@ -459,9 +463,7 @@ Function *ArgPromotion::DoPromotion(Function *F,
 
     if (!Call->use_empty()) {
       Call->replaceAllUsesWith(New);
-      std::string Name = Call->getName();
-      Call->setName("");
-      New->setName(Name);
+      New->takeName(Call);
     }
 
     // Finally, remove the old call from the program, reducing the use-count of
@@ -477,13 +479,13 @@ Function *ArgPromotion::DoPromotion(Function *F,
   // Loop over the argument list, transfering uses of the old arguments over to
   // the new arguments, also transfering over the names as well.
   //
-  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(), I2 = NF->arg_begin();
-       I != E; ++I)
+  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(),
+       I2 = NF->arg_begin(); I != E; ++I)
     if (!ArgsToPromote.count(I)) {
       // If this is an unmodified argument, move the name and users over to the
       // new version.
       I->replaceAllUsesWith(I2);
-      I2->setName(I->getName());
+      I2->takeName(I);
       AA.replaceWithNewValue(I, I2);
       ++I2;
     } else if (I->use_empty()) {
@@ -502,13 +504,12 @@ Function *ArgPromotion::DoPromotion(Function *F,
           LI->replaceAllUsesWith(I2);
           AA.replaceWithNewValue(LI, I2);
           LI->getParent()->getInstList().erase(LI);
-          DEBUG(std::cerr << "*** Promoted load of argument '" << I->getName()
-                          << "' in function '" << F->getName() << "'\n");
+          DOUT << "*** Promoted load of argument '" << I->getName()
+               << "' in function '" << F->getName() << "'\n";
         } else {
           GetElementPtrInst *GEP = cast<GetElementPtrInst>(I->use_back());
           std::vector<Value*> Operands(GEP->op_begin()+1, GEP->op_end());
 
-          unsigned ArgNo = 0;
           Function::arg_iterator TheArg = I2;
           for (ScalarizeTable::iterator It = ArgIndices.begin();
                *It != Operands; ++It, ++TheArg) {
@@ -518,13 +519,13 @@ Function *ArgPromotion::DoPromotion(Function *F,
           std::string NewName = I->getName();
           for (unsigned i = 0, e = Operands.size(); i != e; ++i)
             if (ConstantInt *CI = dyn_cast<ConstantInt>(Operands[i]))
-              NewName += "."+itostr((int64_t)CI->getRawValue());
+              NewName += "." + CI->getValue().toString(10);
             else
               NewName += ".x";
           TheArg->setName(NewName+".val");
 
-          DEBUG(std::cerr << "*** Promoted agg argument '" << TheArg->getName()
-                          << "' of function '" << F->getName() << "'\n");
+          DOUT << "*** Promoted agg argument '" << TheArg->getName()
+               << "' of function '" << F->getName() << "'\n";
 
           // All of the uses must be load instructions.  Replace them all with
           // the argument specified by ArgNo.
@@ -546,7 +547,7 @@ Function *ArgPromotion::DoPromotion(Function *F,
 
   // Notify the alias analysis implementation that we inserted a new argument.
   if (ExtraArgHack)
-    AA.copyValue(Constant::getNullValue(Type::IntTy), NF->arg_begin());
+    AA.copyValue(Constant::getNullValue(Type::Int32Ty), NF->arg_begin());
 
 
   // Tell the alias analysis that the old function is about to disappear.