Fix batch of converting RegisterPass<> to INTIALIZE_PASS().
[oota-llvm.git] / lib / Transforms / IPO / DeadArgumentElimination.cpp
index 0da14d8b29bae4cd87ff16e41bb435de5ecdcf27..c06d688b3c4816555238a453b0252fc0062a7348 100644 (file)
@@ -30,7 +30,6 @@
 #include "llvm/Support/CallSite.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Transforms/Utils/Local.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
@@ -38,9 +37,8 @@
 #include <set>
 using namespace llvm;
 
-STATISTIC(NumArgumentsEliminated , "Number of unread args removed");
-STATISTIC(NumRetValsEliminated   , "Number of unused return values removed");
-STATISTIC(NumParametersEliminated, "Number of parameters replaced with undef");
+STATISTIC(NumArgumentsEliminated, "Number of unread args removed");
+STATISTIC(NumRetValsEliminated  , "Number of unused return values removed");
 
 namespace {
   /// DAE - The dead argument elimination pass.
@@ -122,9 +120,14 @@ namespace {
 
     typedef SmallVector<RetOrArg, 5> UseVector;
 
+  protected:
+    // DAH uses this to specify a different ID.
+    explicit DAE(void *ID) : ModulePass(ID) {}
+
   public:
     static char ID; // Pass identification, replacement for typeid
     DAE() : ModulePass(&ID) {}
+
     bool runOnModule(Module &M);
 
     virtual bool ShouldHackArguments() const { return false; }
@@ -142,15 +145,13 @@ namespace {
     void MarkLive(const Function &F);
     void PropagateLiveness(const RetOrArg &RA);
     bool RemoveDeadStuffFromFunction(Function *F);
-    bool RemoveDeadParamsFromCallersOf(Function *F);
     bool DeleteDeadVarargs(Function &Fn);
   };
 }
 
 
 char DAE::ID = 0;
-static RegisterPass<DAE>
-X("deadargelim", "Dead Argument Elimination");
+INITIALIZE_PASS(DAE, "deadargelim", "Dead Argument Elimination", false, false);
 
 namespace {
   /// DAH - DeadArgumentHacking pass - Same as dead argument elimination, but
@@ -158,13 +159,16 @@ namespace {
   /// by bugpoint.
   struct DAH : public DAE {
     static char ID;
+    DAH() : DAE(&ID) {}
+
     virtual bool ShouldHackArguments() const { return true; }
   };
 }
 
 char DAH::ID = 0;
-static RegisterPass<DAH>
-Y("deadarghaX0r", "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)");
+INITIALIZE_PASS(DAH, "deadarghaX0r", 
+                "Dead Argument Hacking (BUGPOINT USE ONLY; DO NOT USE)",
+                false, false);
 
 /// createDeadArgEliminationPass - This pass removes arguments from functions
 /// which are not used by the body of the function.
@@ -246,6 +250,8 @@ bool DAE::DeleteDeadVarargs(Function &Fn) {
       if (cast<CallInst>(Call)->isTailCall())
         cast<CallInst>(New)->setTailCall();
     }
+    New->setDebugLoc(Call->getDebugLoc());
+
     Args.clear();
 
     if (!Call->use_empty())
@@ -400,9 +406,7 @@ DAE::Liveness DAE::SurveyUses(const Value *V, UseVector &MaybeLiveUses) {
 // map.
 //
 // We consider arguments of non-internal functions to be intrinsically alive as
-// well as arguments to functions which have their "address taken". Externally
-// visible functions are assumed to only have their return values intrinsically
-// alive, permitting removal of parameters to unused arguments in callers.
+// well as arguments to functions which have their "address taken".
 //
 void DAE::SurveyFunction(const Function &F) {
   unsigned RetCount = NumRetVals(&F);
@@ -425,14 +429,7 @@ void DAE::SurveyFunction(const Function &F) {
         return;
       }
 
-  if (F.hasExternalLinkage() && !F.isDeclaration()) {
-    DEBUG(dbgs() << "DAE - Intrinsically live return from " << F.getName()
-                 << "\n");
-    // Mark the return values alive.
-    for (unsigned i = 0, e = NumRetVals(&F); i != e; ++i)
-      MarkLive(CreateRet(&F, i));
-  } else if (!F.hasLocalLinkage() &&
-             (!ShouldHackArguments() || F.isIntrinsic())) {
+  if (!F.hasLocalLinkage() && (!ShouldHackArguments() || F.isIntrinsic())) {
     MarkLive(F);
     return;
   }
@@ -782,6 +779,8 @@ bool DAE::RemoveDeadStuffFromFunction(Function *F) {
       if (cast<CallInst>(Call)->isTailCall())
         cast<CallInst>(New)->setTailCall();
     }
+    New->setDebugLoc(Call->getDebugLoc());
+
     Args.clear();
 
     if (!Call->use_empty()) {
@@ -906,43 +905,6 @@ bool DAE::RemoveDeadStuffFromFunction(Function *F) {
   return true;
 }
 
-bool DAE::RemoveDeadParamsFromCallersOf(Function *F) {
-  // Don't modify fully live functions
-  if (LiveFunctions.count(F))
-    return false;
-
-  // Make a list of the dead arguments.
-  SmallVector<int, 10> ArgDead;
-  unsigned i = 0;
-  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
-       I != E; ++I, ++i) {
-    RetOrArg Arg = CreateArg(F, i);
-    if (!LiveValues.count(Arg))
-      ArgDead.push_back(i);
-  }
-  if (ArgDead.empty())
-    return false;
-
-  bool MadeChange = false;
-  for (Function::use_iterator I = F->use_begin(), E = F->use_end();
-       I != E; ++I) {
-    CallSite CS = CallSite::get(*I);
-    if (CS.getInstruction() && CS.isCallee(I)) {
-      for (unsigned i = 0, e = ArgDead.size(); i != e; ++i) {
-        Value *A = CS.getArgument(ArgDead[i]);
-        if (!isa<UndefValue>(A)) {
-         ++NumParametersEliminated;
-          MadeChange = true;
-          CS.setArgument(ArgDead[i], UndefValue::get(A->getType()));
-          RecursivelyDeleteTriviallyDeadInstructions(A);
-        }
-      }
-    }
-  }
-
-  return MadeChange;
-}
-
 bool DAE::runOnModule(Module &M) {
   bool Changed = false;
 
@@ -971,10 +933,7 @@ bool DAE::runOnModule(Module &M) {
     // Increment now, because the function will probably get removed (ie.
     // replaced by a new one).
     Function *F = I++;
-    if (F->hasExternalLinkage() && !F->isDeclaration())
-      Changed |= RemoveDeadParamsFromCallersOf(F);
-    else
-      Changed |= RemoveDeadStuffFromFunction(F);
+    Changed |= RemoveDeadStuffFromFunction(F);
   }
   return Changed;
 }