* We were forgetting to pass varargs arguments through a call
[oota-llvm.git] / lib / Transforms / Scalar / TailRecursionElimination.cpp
index f0c5ba73b917f4bca185133131e3746b73849f4e..d07f4e857eee7a16bf5b08353842ef9f6262fcef 100644 (file)
@@ -1,4 +1,11 @@
 //===- TailRecursionElimination.cpp - Eliminate Tail Calls ----------------===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file implements tail recursion elimination.
 //
@@ -19,6 +26,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Transforms/Scalar.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
 #include "llvm/Instructions.h"
@@ -34,6 +42,8 @@ namespace {
   RegisterOpt<TailCallElim> X("tailcallelim", "Tail Call Elimination");
 }
 
+FunctionPass *createTailCallEliminationPass() { return new TailCallElim(); }
+
 
 bool TailCallElim::runOnFunction(Function &F) {
   // If this function is a varargs function, we won't be able to PHI the args
@@ -47,7 +57,7 @@ bool TailCallElim::runOnFunction(Function &F) {
   // Loop over the function, looking for any returning blocks...
   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
     if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB->getTerminator()))
-      if (Ret != BB->begin())
+      if (Ret != BB->begin())  // Make sure there is something before the ret...
         if (CallInst *CI = dyn_cast<CallInst>(Ret->getPrev()))
           // Make sure the tail call is to the current function, and that the
           // return either returns void or returns the value computed by the
@@ -59,7 +69,7 @@ bool TailCallElim::runOnFunction(Function &F) {
               // Ok, so this is the first tail call we have found in this
               // function.  Insert a new entry block into the function, allowing
               // us to branch back to the old entry block.
-              OldEntry = &F.getEntryNode();
+              OldEntry = &F.getEntryBlock();
               BasicBlock *NewEntry = new BasicBlock("tailrecurse", OldEntry);
               NewEntry->getInstList().push_back(new BranchInst(OldEntry));
               
@@ -71,6 +81,7 @@ bool TailCallElim::runOnFunction(Function &F) {
               for (Function::aiterator I = F.abegin(), E = F.aend(); I!=E; ++I){
                 PHINode *PN = new PHINode(I->getType(), I->getName()+".tr",
                                           InsertPos);
+                I->replaceAllUsesWith(PN); // Everyone use the PHI node now!
                 PN->addIncoming(I, NewEntry);
                 ArgumentPHIs.push_back(PN);
               }