InlineFunction.cpp revision bf229f488a7541abd979cc3fbe9c3ce1c100e5c0
1ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===- InlineFunction.cpp - Code to perform function inlining -------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
5b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// This file was developed by the LLVM research group and is distributed under
6b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
10ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// This file implements inlining of a function into a call site, resolving
11ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// parameters and the return value as appropriate.
12ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
13ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===----------------------------------------------------------------------===//
14ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
15ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/Transforms/Utils/Cloning.h"
163787e765facfad5ea62753922d940bcdd52afd57Chris Lattner#include "llvm/Constants.h"
177152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner#include "llvm/DerivedTypes.h"
18ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/Module.h"
1980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Instructions.h"
2080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Intrinsics.h"
2180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Support/CallSite.h"
22f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm;
23ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
24f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerbool llvm::InlineFunction(CallInst *CI) { return InlineFunction(CallSite(CI)); }
25f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerbool llvm::InlineFunction(InvokeInst *II) {return InlineFunction(CallSite(II));}
2680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
27cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
28cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// in the body of the inlined function into invokes and turn unwind
29cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// instructions into branches to the invoke unwind dest.
30cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner///
31cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// II is the invoke instruction begin inlined.  FirstNewBlock is the first
32cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// block of the inlined code (the last block is the end of the function),
33cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// and InlineCodeInfo is information about the code that got inlined.
34cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattnerstatic void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
35cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner                                ClonedCodeInfo &InlinedCodeInfo) {
36cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  BasicBlock *InvokeDest = II->getUnwindDest();
37cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  std::vector<Value*> InvokeDestPHIValues;
38cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
39cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // If there are PHI nodes in the unwind destination block, we need to
40cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // keep track of which values came into them from this invoke, then remove
41cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // the entry for this block.
42cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  BasicBlock *InvokeBlock = II->getParent();
43cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) {
44cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    PHINode *PN = cast<PHINode>(I);
45cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    // Save the value to use for this edge.
46cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock));
47cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  }
48cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
49cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  Function *Caller = FirstNewBlock->getParent();
50cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
51cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // The inlined code is currently at the end of the function, scan from the
52cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // start of the inlined code to its end, checking for stuff we need to
53cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // rewrite.
54727d1dd58793447e83ade712f0e58172f156edcfChris Lattner  if (InlinedCodeInfo.ContainsCalls || InlinedCodeInfo.ContainsUnwinds) {
55727d1dd58793447e83ade712f0e58172f156edcfChris Lattner    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
56727d1dd58793447e83ade712f0e58172f156edcfChris Lattner         BB != E; ++BB) {
57727d1dd58793447e83ade712f0e58172f156edcfChris Lattner      if (InlinedCodeInfo.ContainsCalls) {
58727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ){
59727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          Instruction *I = BBI++;
60727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
61727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // We only need to check for function calls: inlined invoke
62727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // instructions require no special handling.
63727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          if (!isa<CallInst>(I)) continue;
64727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          CallInst *CI = cast<CallInst>(I);
65cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
66727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // If this is an intrinsic function call, don't convert it to an
67727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // invoke.
68727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          if (CI->getCalledFunction() &&
69727d1dd58793447e83ade712f0e58172f156edcfChris Lattner              CI->getCalledFunction()->getIntrinsicID())
70727d1dd58793447e83ade712f0e58172f156edcfChris Lattner            continue;
71727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
72727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Convert this function call into an invoke instruction.
73727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // First, split the basic block.
74727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
75727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
76727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Next, create the new invoke instruction, inserting it at the end
77727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // of the old basic block.
78727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          InvokeInst *II =
79727d1dd58793447e83ade712f0e58172f156edcfChris Lattner            new InvokeInst(CI->getCalledValue(), Split, InvokeDest,
80727d1dd58793447e83ade712f0e58172f156edcfChris Lattner                           std::vector<Value*>(CI->op_begin()+1, CI->op_end()),
81727d1dd58793447e83ade712f0e58172f156edcfChris Lattner                           CI->getName(), BB->getTerminator());
82727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          II->setCallingConv(CI->getCallingConv());
83727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
84727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Make sure that anything using the call now uses the invoke!
85727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          CI->replaceAllUsesWith(II);
86727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
87727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Delete the unconditional branch inserted by splitBasicBlock
88727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          BB->getInstList().pop_back();
89727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          Split->getInstList().pop_front();  // Delete the original call
90727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
91727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Update any PHI nodes in the exceptional block to indicate that
92727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // there is now a new entry in them.
93727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          unsigned i = 0;
94727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          for (BasicBlock::iterator I = InvokeDest->begin();
95727d1dd58793447e83ade712f0e58172f156edcfChris Lattner               isa<PHINode>(I); ++I, ++i) {
96727d1dd58793447e83ade712f0e58172f156edcfChris Lattner            PHINode *PN = cast<PHINode>(I);
97727d1dd58793447e83ade712f0e58172f156edcfChris Lattner            PN->addIncoming(InvokeDestPHIValues[i], BB);
98727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          }
99727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
100727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // This basic block is now complete, start scanning the next one.
101727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          break;
102727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        }
103cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner      }
104cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
105727d1dd58793447e83ade712f0e58172f156edcfChris Lattner      if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
106727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // An UnwindInst requires special handling when it gets inlined into an
107727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // invoke site.  Once this happens, we know that the unwind would cause
108727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // a control transfer to the invoke exception destination, so we can
109727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // transform it into a direct branch to the exception destination.
110727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        new BranchInst(InvokeDest, UI);
111727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
112727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // Delete the unwind instruction!
113727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        UI->getParent()->getInstList().pop_back();
114727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
115727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // Update any PHI nodes in the exceptional block to indicate that
116727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // there is now a new entry in them.
117727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        unsigned i = 0;
118727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        for (BasicBlock::iterator I = InvokeDest->begin();
119727d1dd58793447e83ade712f0e58172f156edcfChris Lattner             isa<PHINode>(I); ++I, ++i) {
120727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          PHINode *PN = cast<PHINode>(I);
121727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          PN->addIncoming(InvokeDestPHIValues[i], BB);
122727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        }
123cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner      }
124cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    }
125cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  }
126cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
127cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // Now that everything is happy, we have one final detail.  The PHI nodes in
128cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // the exception destination block still have entries due to the original
129cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // invoke instruction.  Eliminate these entries (which might even delete the
130cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // PHI node) now.
131cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  InvokeDest->removePredecessor(II->getParent());
132cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner}
133cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
134cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
135ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// InlineFunction - This function inlines the called function into the basic
136ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// block of the caller.  This returns false if it is not possible to inline this
137ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// call.  The program is still in a well defined state if this occurs though.
138ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
139fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// Note that this only does one level of inlining.  For example, if the
140fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
141ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// exists in the instruction stream.  Similiarly this will inline a recursive
142ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// function by one level.
143ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
144f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerbool llvm::InlineFunction(CallSite CS) {
14580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  Instruction *TheCall = CS.getInstruction();
14680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
14780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner         "Instruction not in function!");
148ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
14980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  const Function *CalledFunc = CS.getCalledFunction();
150ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  if (CalledFunc == 0 ||          // Can't inline external function or indirect
151ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner      CalledFunc->isExternal() || // call, or call to a vararg function!
152ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner      CalledFunc->getFunctionType()->isVarArg()) return false;
153ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
1541b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
1551b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  // If the call to the callee is a non-tail call, we must clear the 'tail'
1561b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  // flags on any calls that we inline.
1571b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  bool MustClearTailCallFlags =
1583799ed83b4cb80695a81294da6a7d18cf0884f5eChris Lattner    isa<CallInst>(TheCall) && !cast<CallInst>(TheCall)->isTailCall();
1591b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
16080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  BasicBlock *OrigBB = TheCall->getParent();
161ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  Function *Caller = OrigBB->getParent();
162ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
1635052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // Get an iterator to the last basic block in the function, which will have
1645052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // the new function inlined after it.
1655052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  //
1665052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  Function::iterator LastBlock = &Caller->back();
1675052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner
1685e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Make sure to capture all of the return instructions from the cloned
1695e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // function.
1705052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  std::vector<ReturnInst*> Returns;
171cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  ClonedCodeInfo InlinedFunctionInfo;
1725e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  { // Scope to destroy ValueMap after cloning.
1735e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Calculate the vector of arguments to pass into the function cloner...
1745e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    std::map<const Value*, Value*> ValueMap;
175fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman    assert(std::distance(CalledFunc->arg_begin(), CalledFunc->arg_end()) ==
1765e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           std::distance(CS.arg_begin(), CS.arg_end()) &&
1775e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           "No varargs calls can be inlined!");
178fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
1795e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    CallSite::arg_iterator AI = CS.arg_begin();
180e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner    for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
181e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner           E = CalledFunc->arg_end(); I != E; ++I, ++AI)
1825e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      ValueMap[I] = *AI;
183fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
184fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman    // Clone the entire body of the callee into the caller.
185cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    CloneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i",
186cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner                      &InlinedFunctionInfo);
187fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  }
188c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner
1895e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Remember the first block that is newly cloned over.
1905e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  Function::iterator FirstNewBlock = LastBlock; ++FirstNewBlock;
191ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
192ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // If there are any alloca instructions in the block that used to be the entry
193ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // block for the callee, move them to the entry block of the caller.  First
194ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // calculate which instruction they should be inserted before.  We insert the
195ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // instructions at the end of the current alloca list.
196ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  //
19721f20558d629f7ff8f64c20746d890d29328a544Chris Lattner  {
19880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner    BasicBlock::iterator InsertPoint = Caller->begin()->begin();
1995e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    for (BasicBlock::iterator I = FirstNewBlock->begin(),
2005e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           E = FirstNewBlock->end(); I != E; )
201f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner      if (AllocaInst *AI = dyn_cast<AllocaInst>(I++))
202f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner        if (isa<Constant>(AI->getArraySize())) {
20321f20558d629f7ff8f64c20746d890d29328a544Chris Lattner          // Scan for the block of allocas that we can move over, and move them
20421f20558d629f7ff8f64c20746d890d29328a544Chris Lattner          // all at once.
205c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          while (isa<AllocaInst>(I) &&
206c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner                 isa<Constant>(cast<AllocaInst>(I)->getArraySize()))
207c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner            ++I;
208c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner
209c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          // Transfer all of the allocas over in a block.  Using splice means
210c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          // that they instructions aren't removed from the symbol table, then
211c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          // reinserted.
212c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          Caller->front().getInstList().splice(InsertPoint,
213c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner                                               FirstNewBlock->getInstList(),
214c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner                                               AI, I);
215f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner        }
21680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  }
21780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
218bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // If the inlined code contained dynamic alloca instructions, wrap the inlined
219bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // code with llvm.stacksave/llvm.stackrestore intrinsics.
220bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  if (InlinedFunctionInfo.ContainsDynamicAllocas) {
221bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    Module *M = Caller->getParent();
222bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    const Type *SBytePtr = PointerType::get(Type::SByteTy);
223bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Get the two intrinsics we care about.
224bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    Function *StackSave, *StackRestore;
225bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    StackSave    = M->getOrInsertFunction("llvm.stacksave", SBytePtr, NULL);
226bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    StackRestore = M->getOrInsertFunction("llvm.stackrestore", Type::VoidTy,
227bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner                                          SBytePtr, NULL);
228bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
229bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert the llvm.stacksave.
230bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    Value *SavedPtr = new CallInst(StackSave, "savedstack",
231bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner                                   FirstNewBlock->begin());
232bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
233bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert a call to llvm.stackrestore before any return instructions in the
234bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // inlined function.
235bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    for (unsigned i = 0, e = Returns.size(); i != e; ++i)
236bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner      new CallInst(StackRestore, SavedPtr, "", Returns[i]);
237bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
238bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // If we are inlining an invoke instruction, insert restores before each
239bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // unwind.  These unwinds will be rewritten into branches later.
240bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
241bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner      for (Function::iterator BB = FirstNewBlock, E = Caller->end();
242bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner           BB != E; ++BB)
243bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner        if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator()))
244bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner          new CallInst(StackRestore, SavedPtr, "", UI);
245bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    }
246bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  }
247bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
2481fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // If we are inlining tail call instruction through a call site that isn't
2491fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // marked 'tail', we must remove the tail marker for any calls in the inlined
2501fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // code.
2511fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  if (MustClearTailCallFlags && InlinedFunctionInfo.ContainsCalls) {
2521b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
2531b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner         BB != E; ++BB)
2541b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
2551b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner        if (CallInst *CI = dyn_cast<CallInst>(I))
2561b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner          CI->setTailCall(false);
2571b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  }
2581b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
2595e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // If we are inlining for an invoke instruction, we must make sure to rewrite
2605e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any inlined 'unwind' instructions into branches to the invoke exception
2615e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // destination, and call instructions into invoke instructions.
262cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
263cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
2645e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
26544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // If we cloned in _exactly one_ basic block, and if that block ends in a
26644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // return instruction, we splice the body of the inlined callee directly into
26744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // the calling basic block.
26844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
26944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Move all of the instructions right before the call.
27044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
27144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                 FirstNewBlock->begin(), FirstNewBlock->end());
27244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Remove the cloned basic block.
27344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    Caller->getBasicBlockList().pop_back();
274fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
27544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the call site was an invoke instruction, add a branch to the normal
27644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // destination.
27744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
27844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner      new BranchInst(II->getNormalDest(), TheCall);
27944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
28044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the return instruction returned a value, replace uses of the call with
28144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // uses of the returned value.
28244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    if (!TheCall->use_empty())
28344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner      TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
28444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
28544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the Call/Invoke, we can delete it.
28644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    TheCall->getParent()->getInstList().erase(TheCall);
28744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
28844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the return instruction, delete it also.
28944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    Returns[0]->getParent()->getInstList().erase(Returns[0]);
29044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
29144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // We are now done with the inlining.
29244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    return true;
29344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  }
29444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
29544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Otherwise, we have the normal case, of more than one block to inline or
29644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // multiple return sites.
29744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
2985e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // We want to clone the entire callee function into the hole between the
2995e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // "starter" and "ender" blocks.  How we accomplish this depends on whether
3005e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // this is an invoke instruction or a call instruction.
3015e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  BasicBlock *AfterCallBB;
3025e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
303fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3045e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Add an unconditional branch to make this look like the CallInst case...
3055e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall);
306fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3075e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Split the basic block.  This guarantees that no PHI nodes will have to be
3085e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // updated due to new incoming edges, and make the invoke case more
3095e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // symmetric to the call case.
3105e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(NewBr,
311284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
312fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3135e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  } else {  // It's a call
31444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If this is a call instruction, we need to split the basic block that
31544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // the call lives in.
3165e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    //
3175e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(TheCall,
318284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
3195e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
3205e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
32144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Change the branch that used to go to AfterCallBB to branch to the first
32244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // basic block of the inlined function.
32344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  //
32444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  TerminatorInst *Br = OrigBB->getTerminator();
325fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  assert(Br && Br->getOpcode() == Instruction::Br &&
32644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner         "splitBasicBlock broken!");
32744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Br->setOperand(0, FirstNewBlock);
32844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
32944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
33044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Now that the function is correct, make it a little bit nicer.  In
33144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // particular, move the basic blocks inserted from the end of the function
33244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // into the space made by splitting the source basic block.
33344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  //
33444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
33544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                     FirstNewBlock, Caller->end());
33644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
3375e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Handle all of the return instructions that we just cloned in, and eliminate
3385e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any users of the original call/invoke instruction.
3395e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  if (Returns.size() > 1) {
3405e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // The PHI node should go at the front of the new basic block to merge all
3415e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // possible incoming values.
3425e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    //
3435e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    PHINode *PHI = 0;
3445e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    if (!TheCall->use_empty()) {
3455e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      PHI = new PHINode(CalledFunc->getReturnType(),
3465e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner                        TheCall->getName(), AfterCallBB->begin());
347fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3485e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      // Anything that used the result of the function call should now use the
3495e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      // PHI node as their operand.
3505e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      //
3515e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      TheCall->replaceAllUsesWith(PHI);
3525e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
353fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3545e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Loop over all of the return instructions, turning them into unconditional
3555e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // branches to the merge point now, and adding entries to the PHI node as
3565e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // appropriate.
3575e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
3585e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      ReturnInst *RI = Returns[i];
359fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3605e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      if (PHI) {
3615e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner        assert(RI->getReturnValue() && "Ret should have value!");
362fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman        assert(RI->getReturnValue()->getType() == PHI->getType() &&
3635e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner               "Ret value not consistent in function!");
3645e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner        PHI->addIncoming(RI->getReturnValue(), RI->getParent());
3655e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      }
366fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3675e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      // Add a branch to the merge point where the PHI node lives if it exists.
3685e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      new BranchInst(AfterCallBB, RI);
369fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3705e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      // Delete the return instruction now
3715e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      RI->getParent()->getInstList().erase(RI);
3725e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
373fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3745e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  } else if (!Returns.empty()) {
3755e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Otherwise, if there is exactly one return value, just replace anything
3765e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // using the return value of the call with the computed value.
3775e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    if (!TheCall->use_empty())
3785e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
379fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
380cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner    // Splice the code from the return block into the block that it will return
381cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner    // to, which contains the code that was after the call.
382cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner    BasicBlock *ReturnBB = Returns[0]->getParent();
383adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner    AfterCallBB->getInstList().splice(AfterCallBB->begin(),
384adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner                                      ReturnBB->getInstList());
385cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
386adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner    // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
387adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner    ReturnBB->replaceAllUsesWith(AfterCallBB);
388fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
389adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner    // Delete the return instruction now and empty ReturnBB now.
3903787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    Returns[0]->eraseFromParent();
3913787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    ReturnBB->eraseFromParent();
3923787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  } else if (!TheCall->use_empty()) {
3933787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // No returns, but something is using the return value of the call.  Just
3943787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // nuke the result.
3953787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
3965e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
397fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3985e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Since we are now done with the Call/Invoke, we can delete it.
3993787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  TheCall->eraseFromParent();
400ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
4017152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // We should always be able to fold the entry block of the function into the
4027152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // single predecessor of the block...
403cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
4047152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
40544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
406cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Splice the code entry block into calling block, right before the
407cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // unconditional branch.
408cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
409cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  CalleeEntry->replaceAllUsesWith(OrigBB);  // Update PHI nodes
410cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
411cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Remove the unconditional branch.
412cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  OrigBB->getInstList().erase(Br);
413cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
414cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Now we can remove the CalleeEntry block, which is now empty.
415cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  Caller->getBasicBlockList().erase(CalleeEntry);
416ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  return true;
417ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner}
418