InlineFunction.cpp revision 981418bf1562d0b5b470ddc7d0034c9f3297b893
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"
21468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner#include "llvm/Analysis/CallGraph.h"
2280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Support/CallSite.h"
23f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm;
24ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
25468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattnerbool llvm::InlineFunction(CallInst *CI, CallGraph *CG) {
26468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  return InlineFunction(CallSite(CI), CG);
27468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
28468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattnerbool llvm::InlineFunction(InvokeInst *II, CallGraph *CG) {
29468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  return InlineFunction(CallSite(II), CG);
30468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
3180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
32cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
33cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// in the body of the inlined function into invokes and turn unwind
34cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// instructions into branches to the invoke unwind dest.
35cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner///
36cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// II is the invoke instruction begin inlined.  FirstNewBlock is the first
37cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// block of the inlined code (the last block is the end of the function),
38cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// and InlineCodeInfo is information about the code that got inlined.
39cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattnerstatic void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
40cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner                                ClonedCodeInfo &InlinedCodeInfo) {
41cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  BasicBlock *InvokeDest = II->getUnwindDest();
42cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  std::vector<Value*> InvokeDestPHIValues;
43cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
44cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // If there are PHI nodes in the unwind destination block, we need to
45cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // keep track of which values came into them from this invoke, then remove
46cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // the entry for this block.
47cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  BasicBlock *InvokeBlock = II->getParent();
48cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) {
49cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    PHINode *PN = cast<PHINode>(I);
50cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    // Save the value to use for this edge.
51cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock));
52cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  }
53cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
54cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  Function *Caller = FirstNewBlock->getParent();
55cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
56cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // The inlined code is currently at the end of the function, scan from the
57cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // start of the inlined code to its end, checking for stuff we need to
58cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // rewrite.
59727d1dd58793447e83ade712f0e58172f156edcfChris Lattner  if (InlinedCodeInfo.ContainsCalls || InlinedCodeInfo.ContainsUnwinds) {
60727d1dd58793447e83ade712f0e58172f156edcfChris Lattner    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
61727d1dd58793447e83ade712f0e58172f156edcfChris Lattner         BB != E; ++BB) {
62727d1dd58793447e83ade712f0e58172f156edcfChris Lattner      if (InlinedCodeInfo.ContainsCalls) {
63727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ){
64727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          Instruction *I = BBI++;
65727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
66727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // We only need to check for function calls: inlined invoke
67727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // instructions require no special handling.
68727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          if (!isa<CallInst>(I)) continue;
69727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          CallInst *CI = cast<CallInst>(I);
70cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
71727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // If this is an intrinsic function call, don't convert it to an
72727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // invoke.
73727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          if (CI->getCalledFunction() &&
74727d1dd58793447e83ade712f0e58172f156edcfChris Lattner              CI->getCalledFunction()->getIntrinsicID())
75727d1dd58793447e83ade712f0e58172f156edcfChris Lattner            continue;
76727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
77727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Convert this function call into an invoke instruction.
78727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // First, split the basic block.
79727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
80727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
81727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Next, create the new invoke instruction, inserting it at the end
82727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // of the old basic block.
83727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          InvokeInst *II =
84727d1dd58793447e83ade712f0e58172f156edcfChris Lattner            new InvokeInst(CI->getCalledValue(), Split, InvokeDest,
85727d1dd58793447e83ade712f0e58172f156edcfChris Lattner                           std::vector<Value*>(CI->op_begin()+1, CI->op_end()),
86727d1dd58793447e83ade712f0e58172f156edcfChris Lattner                           CI->getName(), BB->getTerminator());
87727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          II->setCallingConv(CI->getCallingConv());
88727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
89727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Make sure that anything using the call now uses the invoke!
90727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          CI->replaceAllUsesWith(II);
91727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
92727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Delete the unconditional branch inserted by splitBasicBlock
93727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          BB->getInstList().pop_back();
94727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          Split->getInstList().pop_front();  // Delete the original call
95727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
96727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // Update any PHI nodes in the exceptional block to indicate that
97727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // there is now a new entry in them.
98727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          unsigned i = 0;
99727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          for (BasicBlock::iterator I = InvokeDest->begin();
100727d1dd58793447e83ade712f0e58172f156edcfChris Lattner               isa<PHINode>(I); ++I, ++i) {
101727d1dd58793447e83ade712f0e58172f156edcfChris Lattner            PHINode *PN = cast<PHINode>(I);
102727d1dd58793447e83ade712f0e58172f156edcfChris Lattner            PN->addIncoming(InvokeDestPHIValues[i], BB);
103727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          }
104727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
105727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          // This basic block is now complete, start scanning the next one.
106727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          break;
107727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        }
108cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner      }
109cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
110727d1dd58793447e83ade712f0e58172f156edcfChris Lattner      if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
111727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // An UnwindInst requires special handling when it gets inlined into an
112727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // invoke site.  Once this happens, we know that the unwind would cause
113727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // a control transfer to the invoke exception destination, so we can
114727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // transform it into a direct branch to the exception destination.
115727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        new BranchInst(InvokeDest, UI);
116727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
117727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // Delete the unwind instruction!
118727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        UI->getParent()->getInstList().pop_back();
119727d1dd58793447e83ade712f0e58172f156edcfChris Lattner
120727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // Update any PHI nodes in the exceptional block to indicate that
121727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        // there is now a new entry in them.
122727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        unsigned i = 0;
123727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        for (BasicBlock::iterator I = InvokeDest->begin();
124727d1dd58793447e83ade712f0e58172f156edcfChris Lattner             isa<PHINode>(I); ++I, ++i) {
125727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          PHINode *PN = cast<PHINode>(I);
126727d1dd58793447e83ade712f0e58172f156edcfChris Lattner          PN->addIncoming(InvokeDestPHIValues[i], BB);
127727d1dd58793447e83ade712f0e58172f156edcfChris Lattner        }
128cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner      }
129cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    }
130cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  }
131cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
132cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // Now that everything is happy, we have one final detail.  The PHI nodes in
133cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // the exception destination block still have entries due to the original
134cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // invoke instruction.  Eliminate these entries (which might even delete the
135cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // PHI node) now.
136cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  InvokeDest->removePredecessor(II->getParent());
137cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner}
138cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
139d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee
140d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// into the caller, update the specified callgraph to reflect the changes we
141d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// made.  Note that it's possible that not all code was copied over, so only
142d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// some edges of the callgraph will be remain.
143d85340f4ec587e22b0239617f3b747a6df113894Chris Lattnerstatic void UpdateCallGraphAfterInlining(const Function *Caller,
144468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner                                         const Function *Callee,
145d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner                                         Function::iterator FirstNewBlock,
146d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner                                       std::map<const Value*, Value*> &ValueMap,
147468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner                                         CallGraph &CG) {
148468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  // Update the call graph by deleting the edge from Callee to Caller
149468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallGraphNode *CalleeNode = CG[Callee];
150468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallGraphNode *CallerNode = CG[Caller];
151468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallerNode->removeCallEdgeTo(CalleeNode);
152468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
153d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  // Since we inlined some uninlined call sites in the callee into the caller,
154468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  // add edges from the caller to all of the callees of the callee.
155468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  for (CallGraphNode::iterator I = CalleeNode->begin(),
156d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner       E = CalleeNode->end(); I != E; ++I) {
157d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    const Instruction *OrigCall = I->first.getInstruction();
158d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
159d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    std::map<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall);
160981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner    // Only copy the edge if the call was inlined!
161981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner    if (VMI != ValueMap.end() && VMI->second) {
1621bb3a402574557cb228f8a96030776c229e282e5Chris Lattner      // If the call was inlined, but then constant folded, there is no edge to
1631bb3a402574557cb228f8a96030776c229e282e5Chris Lattner      // add.  Check for this case.
1641bb3a402574557cb228f8a96030776c229e282e5Chris Lattner      if (Instruction *NewCall = dyn_cast<Instruction>(VMI->second))
1651bb3a402574557cb228f8a96030776c229e282e5Chris Lattner        CallerNode->addCalledFunction(CallSite::get(NewCall), I->second);
166d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    }
167d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  }
168468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
169468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
170cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
171ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// InlineFunction - This function inlines the called function into the basic
172ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// block of the caller.  This returns false if it is not possible to inline this
173ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// call.  The program is still in a well defined state if this occurs though.
174ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
175fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// Note that this only does one level of inlining.  For example, if the
176fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
177ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// exists in the instruction stream.  Similiarly this will inline a recursive
178ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// function by one level.
179ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
180468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattnerbool llvm::InlineFunction(CallSite CS, CallGraph *CG) {
18180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  Instruction *TheCall = CS.getInstruction();
18280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
18380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner         "Instruction not in function!");
184ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
18580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  const Function *CalledFunc = CS.getCalledFunction();
186ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  if (CalledFunc == 0 ||          // Can't inline external function or indirect
187ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner      CalledFunc->isExternal() || // call, or call to a vararg function!
188ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner      CalledFunc->getFunctionType()->isVarArg()) return false;
189ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
1901b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
1911b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  // If the call to the callee is a non-tail call, we must clear the 'tail'
1921b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  // flags on any calls that we inline.
1931b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  bool MustClearTailCallFlags =
1943799ed83b4cb80695a81294da6a7d18cf0884f5eChris Lattner    isa<CallInst>(TheCall) && !cast<CallInst>(TheCall)->isTailCall();
1951b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
19680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  BasicBlock *OrigBB = TheCall->getParent();
197ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  Function *Caller = OrigBB->getParent();
198ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
1995052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // Get an iterator to the last basic block in the function, which will have
2005052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // the new function inlined after it.
2015052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  //
2025052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  Function::iterator LastBlock = &Caller->back();
2035052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner
2045e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Make sure to capture all of the return instructions from the cloned
2055e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // function.
2065052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  std::vector<ReturnInst*> Returns;
207cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  ClonedCodeInfo InlinedFunctionInfo;
208d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  Function::iterator FirstNewBlock;
209d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
2105e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  { // Scope to destroy ValueMap after cloning.
2115e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    std::map<const Value*, Value*> ValueMap;
2125b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner
2135b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // Calculate the vector of arguments to pass into the function cloner, which
2145b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // matches up the formal to the actual argument values.
215fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman    assert(std::distance(CalledFunc->arg_begin(), CalledFunc->arg_end()) ==
2165e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           std::distance(CS.arg_begin(), CS.arg_end()) &&
2175e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           "No varargs calls can be inlined!");
2185e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    CallSite::arg_iterator AI = CS.arg_begin();
219e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner    for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
220e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner           E = CalledFunc->arg_end(); I != E; ++I, ++AI)
2215e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      ValueMap[I] = *AI;
222fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
2235b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // We want the inliner to prune the code as it copies.  We would LOVE to
2245b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // have no dead or constant instructions leftover after inlining occurs
2255b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // (which can happen, e.g., because an argument was constant), but we'll be
2265b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // happy with whatever the cloner can do.
2275b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    CloneAndPruneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i",
2285b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner                              &InlinedFunctionInfo);
229d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
230d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // Remember the first block that is newly cloned over.
231d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    FirstNewBlock = LastBlock; ++FirstNewBlock;
232d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
233d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // Update the callgraph if requested.
234d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    if (CG)
235d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner      UpdateCallGraphAfterInlining(Caller, CalledFunc, FirstNewBlock, ValueMap,
236d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner                                   *CG);
237fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  }
238d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
239ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // If there are any alloca instructions in the block that used to be the entry
240ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // block for the callee, move them to the entry block of the caller.  First
241ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // calculate which instruction they should be inserted before.  We insert the
242ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // instructions at the end of the current alloca list.
243ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  //
24421f20558d629f7ff8f64c20746d890d29328a544Chris Lattner  {
24580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner    BasicBlock::iterator InsertPoint = Caller->begin()->begin();
2465e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    for (BasicBlock::iterator I = FirstNewBlock->begin(),
2475e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           E = FirstNewBlock->end(); I != E; )
248f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner      if (AllocaInst *AI = dyn_cast<AllocaInst>(I++))
249f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner        if (isa<Constant>(AI->getArraySize())) {
25021f20558d629f7ff8f64c20746d890d29328a544Chris Lattner          // Scan for the block of allocas that we can move over, and move them
25121f20558d629f7ff8f64c20746d890d29328a544Chris Lattner          // all at once.
252c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          while (isa<AllocaInst>(I) &&
253c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner                 isa<Constant>(cast<AllocaInst>(I)->getArraySize()))
254c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner            ++I;
255c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner
256c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          // Transfer all of the allocas over in a block.  Using splice means
257c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          // that they instructions aren't removed from the symbol table, then
258c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          // reinserted.
259c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner          Caller->front().getInstList().splice(InsertPoint,
260c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner                                               FirstNewBlock->getInstList(),
261c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner                                               AI, I);
262f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner        }
26380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  }
26480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
265bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // If the inlined code contained dynamic alloca instructions, wrap the inlined
266bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // code with llvm.stacksave/llvm.stackrestore intrinsics.
267bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  if (InlinedFunctionInfo.ContainsDynamicAllocas) {
268bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    Module *M = Caller->getParent();
269bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    const Type *SBytePtr = PointerType::get(Type::SByteTy);
270bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Get the two intrinsics we care about.
271bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    Function *StackSave, *StackRestore;
272bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    StackSave    = M->getOrInsertFunction("llvm.stacksave", SBytePtr, NULL);
273bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    StackRestore = M->getOrInsertFunction("llvm.stackrestore", Type::VoidTy,
274bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner                                          SBytePtr, NULL);
275d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
276d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // If we are preserving the callgraph, add edges to the stacksave/restore
277d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // functions for the calls we insert.
278d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    CallGraphNode *StackSaveCGN, *StackRestoreCGN, *CallerNode;
279d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    if (CG) {
280d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner      StackSaveCGN    = CG->getOrInsertFunction(StackSave);
281d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner      StackRestoreCGN = CG->getOrInsertFunction(StackRestore);
282d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner      CallerNode = (*CG)[Caller];
283d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    }
284d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
285bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert the llvm.stacksave.
286d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    CallInst *SavedPtr = new CallInst(StackSave, "savedstack",
287d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner                                      FirstNewBlock->begin());
288d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    if (CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN);
289d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
290bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert a call to llvm.stackrestore before any return instructions in the
291bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // inlined function.
292d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
293d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner      CallInst *CI = new CallInst(StackRestore, SavedPtr, "", Returns[i]);
294d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner      if (CG) CallerNode->addCalledFunction(CI, StackRestoreCGN);
295d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    }
296468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
297468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    // Count the number of StackRestore calls we insert.
298468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    unsigned NumStackRestores = Returns.size();
299bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
300bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // If we are inlining an invoke instruction, insert restores before each
301bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // unwind.  These unwinds will be rewritten into branches later.
302bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
303bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner      for (Function::iterator BB = FirstNewBlock, E = Caller->end();
304bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner           BB != E; ++BB)
305468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner        if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
306bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner          new CallInst(StackRestore, SavedPtr, "", UI);
307468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner          ++NumStackRestores;
308468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner        }
309468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    }
310bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  }
311bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
3121fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // If we are inlining tail call instruction through a call site that isn't
3131fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // marked 'tail', we must remove the tail marker for any calls in the inlined
3141fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // code.
3151fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  if (MustClearTailCallFlags && InlinedFunctionInfo.ContainsCalls) {
3161b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
3171b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner         BB != E; ++BB)
3181b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
3191b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner        if (CallInst *CI = dyn_cast<CallInst>(I))
3201b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner          CI->setTailCall(false);
3211b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  }
3221b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
3235e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // If we are inlining for an invoke instruction, we must make sure to rewrite
3245e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any inlined 'unwind' instructions into branches to the invoke exception
3255e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // destination, and call instructions into invoke instructions.
326cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
327cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
3285e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
32944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // If we cloned in _exactly one_ basic block, and if that block ends in a
33044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // return instruction, we splice the body of the inlined callee directly into
33144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // the calling basic block.
33244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
33344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Move all of the instructions right before the call.
33444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
33544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                 FirstNewBlock->begin(), FirstNewBlock->end());
33644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Remove the cloned basic block.
33744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    Caller->getBasicBlockList().pop_back();
338fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
33944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the call site was an invoke instruction, add a branch to the normal
34044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // destination.
34144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
34244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner      new BranchInst(II->getNormalDest(), TheCall);
34344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
34444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the return instruction returned a value, replace uses of the call with
34544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // uses of the returned value.
34644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    if (!TheCall->use_empty())
34744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner      TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
34844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
34944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the Call/Invoke, we can delete it.
35044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    TheCall->getParent()->getInstList().erase(TheCall);
35144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
35244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the return instruction, delete it also.
35344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    Returns[0]->getParent()->getInstList().erase(Returns[0]);
35444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
35544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // We are now done with the inlining.
35644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    return true;
35744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  }
35844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
35944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Otherwise, we have the normal case, of more than one block to inline or
36044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // multiple return sites.
36144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
3625e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // We want to clone the entire callee function into the hole between the
3635e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // "starter" and "ender" blocks.  How we accomplish this depends on whether
3645e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // this is an invoke instruction or a call instruction.
3655e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  BasicBlock *AfterCallBB;
3665e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
367fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3685e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Add an unconditional branch to make this look like the CallInst case...
3695e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall);
370fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3715e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Split the basic block.  This guarantees that no PHI nodes will have to be
3725e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // updated due to new incoming edges, and make the invoke case more
3735e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // symmetric to the call case.
3745e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(NewBr,
375284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
376fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3775e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  } else {  // It's a call
37844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If this is a call instruction, we need to split the basic block that
37944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // the call lives in.
3805e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    //
3815e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(TheCall,
382284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
3835e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
3845e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
38544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Change the branch that used to go to AfterCallBB to branch to the first
38644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // basic block of the inlined function.
38744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  //
38844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  TerminatorInst *Br = OrigBB->getTerminator();
389fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  assert(Br && Br->getOpcode() == Instruction::Br &&
39044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner         "splitBasicBlock broken!");
39144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Br->setOperand(0, FirstNewBlock);
39244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
39344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
39444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Now that the function is correct, make it a little bit nicer.  In
39544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // particular, move the basic blocks inserted from the end of the function
39644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // into the space made by splitting the source basic block.
39744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  //
39844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
39944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                     FirstNewBlock, Caller->end());
40044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
4015e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Handle all of the return instructions that we just cloned in, and eliminate
4025e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any users of the original call/invoke instruction.
4035e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  if (Returns.size() > 1) {
4045e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // The PHI node should go at the front of the new basic block to merge all
4055e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // possible incoming values.
4065e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    //
4075e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    PHINode *PHI = 0;
4085e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    if (!TheCall->use_empty()) {
4095e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      PHI = new PHINode(CalledFunc->getReturnType(),
4105e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner                        TheCall->getName(), AfterCallBB->begin());
411fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4125e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      // Anything that used the result of the function call should now use the
4135e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      // PHI node as their operand.
4145e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      //
4155e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      TheCall->replaceAllUsesWith(PHI);
4165e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
417fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4185e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Loop over all of the return instructions, turning them into unconditional
4195e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // branches to the merge point now, and adding entries to the PHI node as
4205e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // appropriate.
4215e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
4225e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      ReturnInst *RI = Returns[i];
423fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4245e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      if (PHI) {
4255e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner        assert(RI->getReturnValue() && "Ret should have value!");
426fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman        assert(RI->getReturnValue()->getType() == PHI->getType() &&
4275e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner               "Ret value not consistent in function!");
4285e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner        PHI->addIncoming(RI->getReturnValue(), RI->getParent());
4295e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      }
430fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4315e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      // Add a branch to the merge point where the PHI node lives if it exists.
4325e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      new BranchInst(AfterCallBB, RI);
433fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4345e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      // Delete the return instruction now
4355e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      RI->getParent()->getInstList().erase(RI);
4365e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
437fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4385e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  } else if (!Returns.empty()) {
4395e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Otherwise, if there is exactly one return value, just replace anything
4405e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // using the return value of the call with the computed value.
4415e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    if (!TheCall->use_empty())
4425e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
443fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
444cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner    // Splice the code from the return block into the block that it will return
445cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner    // to, which contains the code that was after the call.
446cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner    BasicBlock *ReturnBB = Returns[0]->getParent();
447adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner    AfterCallBB->getInstList().splice(AfterCallBB->begin(),
448adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner                                      ReturnBB->getInstList());
449cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
450adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner    // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
451adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner    ReturnBB->replaceAllUsesWith(AfterCallBB);
452fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
453adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner    // Delete the return instruction now and empty ReturnBB now.
4543787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    Returns[0]->eraseFromParent();
4553787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    ReturnBB->eraseFromParent();
4563787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  } else if (!TheCall->use_empty()) {
4573787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // No returns, but something is using the return value of the call.  Just
4583787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // nuke the result.
4593787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
4605e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
461fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4625e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Since we are now done with the Call/Invoke, we can delete it.
4633787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  TheCall->eraseFromParent();
464ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
4657152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // We should always be able to fold the entry block of the function into the
4667152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // single predecessor of the block...
467cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
4687152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
46944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
470cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Splice the code entry block into calling block, right before the
471cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // unconditional branch.
472cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
473cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  CalleeEntry->replaceAllUsesWith(OrigBB);  // Update PHI nodes
474cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
475cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Remove the unconditional branch.
476cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  OrigBB->getInstList().erase(Br);
477cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
478cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Now we can remove the CalleeEntry block, which is now empty.
479cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  Caller->getBasicBlockList().erase(CalleeEntry);
480468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
481ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  return true;
482ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner}
483