Drop 'const'
[oota-llvm.git] / lib / Transforms / IPO / DeadArgumentElimination.cpp
index d3326ac71f8d3f9389cd53416e11748ade20b77b..019949c858fc759e1137051a71f7b90495ecd015 100644 (file)
 #include "llvm/Support/CallSite.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Support/Compiler.h"
 #include <set>
 using namespace llvm;
 
-namespace {
-  Statistic NumArgumentsEliminated("deadargelim",
-                                     "Number of unread args removed");
-  Statistic NumRetValsEliminated("deadargelim",
-                                   "Number of unused return values removed");
+STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
+STATISTIC(NumRetValsEliminated  , "Number of unused return values removed");
 
+namespace {
   /// DAE - The dead argument elimination pass.
   ///
-  class DAE : public ModulePass {
+  class VISIBILITY_HIDDEN DAE : public ModulePass {
     /// Liveness enum - During our initial pass over the program, we determine
     /// that things are either definately alive, definately dead, or in need of
     /// interprocedural analysis (MaybeLive).
@@ -77,6 +76,8 @@ namespace {
     std::multimap<Function*, CallSite> CallSites;
 
   public:
+    static char ID; // Pass identifcation, replacement for typeid
+    DAE() : ModulePass((intptr_t)&ID) {}
     bool runOnModule(Module &M);
 
     virtual bool ShouldHackArguments() const { return false; }
@@ -94,14 +95,17 @@ namespace {
 
     void RemoveDeadArgumentsFromFunction(Function *F);
   };
+  char DAE::ID = 0;
   RegisterPass<DAE> X("deadargelim", "Dead Argument Elimination");
 
   /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
   /// deletes arguments to functions which are external.  This is only for use
   /// by bugpoint.
   struct DAH : public DAE {
+    static char ID;
     virtual bool ShouldHackArguments() const { return true; }
   };
+  char DAH::ID = 0;
   RegisterPass<DAH> Y("deadarghaX0r",
                       "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)");
 }
@@ -116,7 +120,7 @@ ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); }
 /// llvm.vastart is never called, the varargs list is dead for the function.
 bool DAE::DeleteDeadVarargs(Function &Fn) {
   assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!");
-  if (Fn.isExternal() || !Fn.hasInternalLinkage()) return false;
+  if (Fn.isDeclaration() || !Fn.hasInternalLinkage()) return false;
   
   // Ensure that the function is only directly called.
   for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ++I) {
@@ -151,9 +155,10 @@ bool DAE::DeleteDeadVarargs(Function &Fn) {
   unsigned NumArgs = Params.size();
   
   // Create the new function body and insert it into the module...
-  Function *NF = new Function(NFTy, Fn.getLinkage(), Fn.getName());
+  Function *NF = new Function(NFTy, Fn.getLinkage());
   NF->setCallingConv(Fn.getCallingConv());
   Fn.getParent()->getFunctionList().insert(&Fn, NF);
+  NF->takeName(&Fn);
   
   // Loop over all of the callers of the function, transforming the call sites
   // to pass in a smaller number of arguments into the new function.
@@ -169,10 +174,10 @@ bool DAE::DeleteDeadVarargs(Function &Fn) {
     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();
@@ -182,11 +187,7 @@ bool DAE::DeleteDeadVarargs(Function &Fn) {
     if (!Call->use_empty())
       Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
     
-    if (Call->hasName()) {
-      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
     // F.
@@ -206,7 +207,7 @@ bool DAE::DeleteDeadVarargs(Function &Fn) {
        I2 = NF->arg_begin(); I != E; ++I, ++I2) {
     // Move the name and users over to the new version.
     I->replaceAllUsesWith(I2);
-    I2->setName(I->getName());
+    I2->takeName(I);
   }
   
   // Finally, nuke the old function.
@@ -232,9 +233,10 @@ static inline bool CallPassesValueThoughVararg(Instruction *Call,
 // (used in a computation), MaybeLive (only passed as an argument to a call), or
 // Dead (not used).
 DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
-  // If this is the return value of a csret function, it's not really dead.
-  if (A.getParent()->getCallingConv() == CallingConv::CSRet &&
-      &*A.getParent()->arg_begin() == &A)
+  const FunctionType *FTy = A.getParent()->getFunctionType();
+  
+  // If this is the return value of a struct function, it's not really dead.
+  if (FTy->isStructReturn() && &*A.getParent()->arg_begin() == &A)
     return Live;
   
   if (A.use_empty())  // First check, directly dead?
@@ -499,20 +501,19 @@ void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
   // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
   // have zero fixed arguments.
   //
-  // FIXME: once this bug is fixed in the CWriter, this hack should be removed.
-  //
   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());
 
   // Create the new function body and insert it into the module...
-  Function *NF = new Function(NFTy, F->getLinkage(), F->getName());
+  Function *NF = new Function(NFTy, F->getLinkage());
   NF->setCallingConv(F->getCallingConv());
   F->getParent()->getFunctionList().insert(F, NF);
+  NF->takeName(F);
 
   // Loop over all of the callers of the function, transforming the call sites
   // to pass in a smaller number of arguments into the new function.
@@ -530,7 +531,7 @@ void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
         Args.push_back(*AI);
 
     if (ExtraArgHack)
-      Args.push_back(Constant::getNullValue(Type::IntTy));
+      Args.push_back(UndefValue::get(Type::Int32Ty));
 
     // Push any varargs arguments on the list
     for (; AI != CS.arg_end(); ++AI)
@@ -539,10 +540,10 @@ void DAE::RemoveDeadArgumentsFromFunction(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();
@@ -554,9 +555,7 @@ void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
         Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
       else {
         Call->replaceAllUsesWith(New);
-        std::string Name = Call->getName();
-        Call->setName("");
-        New->setName(Name);
+        New->takeName(Call);
       }
     }
 
@@ -581,7 +580,7 @@ void DAE::RemoveDeadArgumentsFromFunction(Function *F) {
       // If this is a live argument, move the name and users over to the new
       // version.
       I->replaceAllUsesWith(I2);
-      I2->setName(I->getName());
+      I2->takeName(I);
       ++I2;
     } else {
       // If this argument is dead, replace any uses of it with null constants