Start using the nicer terminator auto-insertion API
[oota-llvm.git] / lib / Transforms / Utils / InlineFunction.cpp
index 8f69c1ee161ab6e215aac9a0294dbda42499309a..a0fc9bf1cff128169fba0ebb35408afcc6c133ce 100644 (file)
@@ -1,4 +1,11 @@
 //===- InlineFunction.cpp - Code to perform function inlining -------------===//
+// 
+//                     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 inlining of a function into a call site, resolving
 // parameters and the return value as appropriate.
@@ -17,6 +24,8 @@
 #include "llvm/Support/CallSite.h"
 #include "llvm/Transforms/Utils/Local.h"
 
+namespace llvm {
+
 bool InlineFunction(CallInst *CI) { return InlineFunction(CallSite(CI)); }
 bool InlineFunction(InvokeInst *II) { return InlineFunction(CallSite(II)); }
 
@@ -53,15 +62,6 @@ bool InlineFunction(CallSite CS) {
   if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
     InvokeDest = II->getExceptionalDest();
 
-    // Add an unconditional branch to make this look like the CallInst case...
-    BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall);
-
-    // Split the basic block.  This guarantees that no PHI nodes will have to be
-    // updated due to new incoming edges, and make the invoke case more
-    // symmetric to the call case.
-    AfterCallBB = OrigBB->splitBasicBlock(NewBr,
-                                          CalledFunc->getName()+".entry");
-
     // If there are PHI nodes in the exceptional destination block, we need to
     // keep track of which values came into them from this invoke, then remove
     // the entry for this block.
@@ -71,6 +71,15 @@ bool InlineFunction(CallSite CS) {
       InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(OrigBB));
     }
 
+    // Add an unconditional branch to make this look like the CallInst case...
+    BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall);
+
+    // Split the basic block.  This guarantees that no PHI nodes will have to be
+    // updated due to new incoming edges, and make the invoke case more
+    // symmetric to the call case.
+    AfterCallBB = OrigBB->splitBasicBlock(NewBr,
+                                          CalledFunc->getName()+".entry");
+
     // Remove (unlink) the InvokeInst from the function...
     OrigBB->getInstList().remove(TheCall);
 
@@ -124,14 +133,6 @@ bool InlineFunction(CallSite CS) {
   // Make a vector to capture the return instructions in the cloned function...
   std::vector<ReturnInst*> Returns;
 
-  // Populate the value map with all of the globals in the program.
-  // FIXME: This should be the default for CloneFunctionInto!
-  Module &M = *Caller->getParent();
-  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
-    ValueMap[I] = I;
-  for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
-    ValueMap[I] = I;
-
   // Do all of the hard part of cloning the callee into the caller...
   CloneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i");
 
@@ -183,13 +184,11 @@ bool InlineFunction(CallSite CS) {
     
     for (BasicBlock::iterator I = LastBlock->begin(), E = LastBlock->end();
          I != E; )
-      if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
-        ++I;  // Move to the next instruction
-        LastBlock->getInstList().remove(AI);
-        Caller->front().getInstList().insert(InsertPoint, AI);      
-      } else {
-        ++I;
-      }
+      if (AllocaInst *AI = dyn_cast<AllocaInst>(I++))
+        if (isa<Constant>(AI->getArraySize())) {
+          LastBlock->getInstList().remove(AI);
+          Caller->front().getInstList().insert(InsertPoint, AI);      
+        }
   }
 
   // If we just inlined a call due to an invoke instruction, scan the inlined
@@ -239,10 +238,17 @@ bool InlineFunction(CallSite CS) {
         // invoke site.  Once this happens, we know that the unwind would cause
         // a control transfer to the invoke exception destination, so we can
         // transform it into a direct branch to the exception destination.
-        BranchInst *BI = new BranchInst(InvokeDest, UI);
+        new BranchInst(InvokeDest, UI);
 
         // Delete the unwind instruction!
         UI->getParent()->getInstList().pop_back();
+
+        // Update any PHI nodes in the exceptional block to indicate that
+        // there is now a new entry in them.
+        unsigned i = 0;
+        for (BasicBlock::iterator I = InvokeDest->begin();
+             PHINode *PN = dyn_cast<PHINode>(I); ++I, ++i)
+          PN->addIncoming(InvokeDestPHIValues[i], BB);
       }
     }
 
@@ -252,7 +258,7 @@ bool InlineFunction(CallSite CS) {
     // PHI node) now.
     for (BasicBlock::iterator I = InvokeDest->begin();
          PHINode *PN = dyn_cast<PHINode>(I); ++I)
-      PN->removeIncomingValue(OrigBB);
+      PN->removeIncomingValue(AfterCallBB);
   }
   // Now that the function is correct, make it a little bit nicer.  In
   // particular, move the basic blocks inserted from the end of the function
@@ -274,3 +280,5 @@ bool InlineFunction(CallSite CS) {
   SimplifyCFG(AfterCallBB);
   return true;
 }
+
+} // End llvm namespace