InlineFunction.cpp revision 93e985f1b17aef62d58e3198a4604f9f6cfe8d19
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" 2293e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner#include "llvm/ADT/SmallVector.h" 2380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Support/CallSite.h" 24f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm; 25ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 261dfdf8255e803d6376f5fe94a113f892e796ae6cChris Lattnerbool llvm::InlineFunction(CallInst *CI, CallGraph *CG, const TargetData *TD) { 271dfdf8255e803d6376f5fe94a113f892e796ae6cChris Lattner return InlineFunction(CallSite(CI), CG, TD); 28468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 291dfdf8255e803d6376f5fe94a113f892e796ae6cChris Lattnerbool llvm::InlineFunction(InvokeInst *II, CallGraph *CG, const TargetData *TD) { 301dfdf8255e803d6376f5fe94a113f892e796ae6cChris Lattner return InlineFunction(CallSite(II), CG, TD); 31468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 3280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 33cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls 34cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// in the body of the inlined function into invokes and turn unwind 35cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// instructions into branches to the invoke unwind dest. 36cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// 37cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// II is the invoke instruction begin inlined. FirstNewBlock is the first 38cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// block of the inlined code (the last block is the end of the function), 39cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// and InlineCodeInfo is information about the code that got inlined. 40cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattnerstatic void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, 41cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner ClonedCodeInfo &InlinedCodeInfo) { 42cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner BasicBlock *InvokeDest = II->getUnwindDest(); 43cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner std::vector<Value*> InvokeDestPHIValues; 44cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 45cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // If there are PHI nodes in the unwind destination block, we need to 46cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // keep track of which values came into them from this invoke, then remove 47cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // the entry for this block. 48cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner BasicBlock *InvokeBlock = II->getParent(); 49cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) { 50cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner PHINode *PN = cast<PHINode>(I); 51cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // Save the value to use for this edge. 52cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock)); 53cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 54cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 55cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner Function *Caller = FirstNewBlock->getParent(); 56cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 57cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // The inlined code is currently at the end of the function, scan from the 58cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // start of the inlined code to its end, checking for stuff we need to 59cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // rewrite. 60727d1dd58793447e83ade712f0e58172f156edcfChris Lattner if (InlinedCodeInfo.ContainsCalls || InlinedCodeInfo.ContainsUnwinds) { 61727d1dd58793447e83ade712f0e58172f156edcfChris Lattner for (Function::iterator BB = FirstNewBlock, E = Caller->end(); 62727d1dd58793447e83ade712f0e58172f156edcfChris Lattner BB != E; ++BB) { 63727d1dd58793447e83ade712f0e58172f156edcfChris Lattner if (InlinedCodeInfo.ContainsCalls) { 64727d1dd58793447e83ade712f0e58172f156edcfChris Lattner for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ){ 65727d1dd58793447e83ade712f0e58172f156edcfChris Lattner Instruction *I = BBI++; 66727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 67727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // We only need to check for function calls: inlined invoke 68727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // instructions require no special handling. 69727d1dd58793447e83ade712f0e58172f156edcfChris Lattner if (!isa<CallInst>(I)) continue; 70727d1dd58793447e83ade712f0e58172f156edcfChris Lattner CallInst *CI = cast<CallInst>(I); 71cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 72727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // If this is an intrinsic function call, don't convert it to an 73727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // invoke. 74727d1dd58793447e83ade712f0e58172f156edcfChris Lattner if (CI->getCalledFunction() && 75727d1dd58793447e83ade712f0e58172f156edcfChris Lattner CI->getCalledFunction()->getIntrinsicID()) 76727d1dd58793447e83ade712f0e58172f156edcfChris Lattner continue; 77727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 78727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // Convert this function call into an invoke instruction. 79727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // First, split the basic block. 80727d1dd58793447e83ade712f0e58172f156edcfChris Lattner BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); 81727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 82727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // Next, create the new invoke instruction, inserting it at the end 83727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // of the old basic block. 8493e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner SmallVector<Value*, 8> InvokeArgs(CI->op_begin()+1, CI->op_end()); 85727d1dd58793447e83ade712f0e58172f156edcfChris Lattner InvokeInst *II = 86727d1dd58793447e83ade712f0e58172f156edcfChris Lattner new InvokeInst(CI->getCalledValue(), Split, InvokeDest, 8793e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner &InvokeArgs[0], InvokeArgs.size(), 88727d1dd58793447e83ade712f0e58172f156edcfChris Lattner CI->getName(), BB->getTerminator()); 89727d1dd58793447e83ade712f0e58172f156edcfChris Lattner II->setCallingConv(CI->getCallingConv()); 90727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 91727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // Make sure that anything using the call now uses the invoke! 92727d1dd58793447e83ade712f0e58172f156edcfChris Lattner CI->replaceAllUsesWith(II); 93727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 94727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // Delete the unconditional branch inserted by splitBasicBlock 95727d1dd58793447e83ade712f0e58172f156edcfChris Lattner BB->getInstList().pop_back(); 96727d1dd58793447e83ade712f0e58172f156edcfChris Lattner Split->getInstList().pop_front(); // Delete the original call 97727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 98727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // Update any PHI nodes in the exceptional block to indicate that 99727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // there is now a new entry in them. 100727d1dd58793447e83ade712f0e58172f156edcfChris Lattner unsigned i = 0; 101727d1dd58793447e83ade712f0e58172f156edcfChris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); 102727d1dd58793447e83ade712f0e58172f156edcfChris Lattner isa<PHINode>(I); ++I, ++i) { 103727d1dd58793447e83ade712f0e58172f156edcfChris Lattner PHINode *PN = cast<PHINode>(I); 104727d1dd58793447e83ade712f0e58172f156edcfChris Lattner PN->addIncoming(InvokeDestPHIValues[i], BB); 105727d1dd58793447e83ade712f0e58172f156edcfChris Lattner } 106727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 107727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // This basic block is now complete, start scanning the next one. 108727d1dd58793447e83ade712f0e58172f156edcfChris Lattner break; 109727d1dd58793447e83ade712f0e58172f156edcfChris Lattner } 110cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 111cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 112727d1dd58793447e83ade712f0e58172f156edcfChris Lattner if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { 113727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // An UnwindInst requires special handling when it gets inlined into an 114727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // invoke site. Once this happens, we know that the unwind would cause 115727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // a control transfer to the invoke exception destination, so we can 116727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // transform it into a direct branch to the exception destination. 117727d1dd58793447e83ade712f0e58172f156edcfChris Lattner new BranchInst(InvokeDest, UI); 118727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 119727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // Delete the unwind instruction! 120727d1dd58793447e83ade712f0e58172f156edcfChris Lattner UI->getParent()->getInstList().pop_back(); 121727d1dd58793447e83ade712f0e58172f156edcfChris Lattner 122727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // Update any PHI nodes in the exceptional block to indicate that 123727d1dd58793447e83ade712f0e58172f156edcfChris Lattner // there is now a new entry in them. 124727d1dd58793447e83ade712f0e58172f156edcfChris Lattner unsigned i = 0; 125727d1dd58793447e83ade712f0e58172f156edcfChris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); 126727d1dd58793447e83ade712f0e58172f156edcfChris Lattner isa<PHINode>(I); ++I, ++i) { 127727d1dd58793447e83ade712f0e58172f156edcfChris Lattner PHINode *PN = cast<PHINode>(I); 128727d1dd58793447e83ade712f0e58172f156edcfChris Lattner PN->addIncoming(InvokeDestPHIValues[i], BB); 129727d1dd58793447e83ade712f0e58172f156edcfChris Lattner } 130cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 131cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 132cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 133cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 134cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // Now that everything is happy, we have one final detail. The PHI nodes in 135cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // the exception destination block still have entries due to the original 136cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // invoke instruction. Eliminate these entries (which might even delete the 137cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // PHI node) now. 138cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner InvokeDest->removePredecessor(II->getParent()); 139cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner} 140cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 141d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee 142d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// into the caller, update the specified callgraph to reflect the changes we 143d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// made. Note that it's possible that not all code was copied over, so only 144d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// some edges of the callgraph will be remain. 145d85340f4ec587e22b0239617f3b747a6df113894Chris Lattnerstatic void UpdateCallGraphAfterInlining(const Function *Caller, 146468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner const Function *Callee, 147d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner Function::iterator FirstNewBlock, 1485e665f559419c7f58a4fd9360cd488f065505c44Chris Lattner DenseMap<const Value*, Value*> &ValueMap, 149468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner CallGraph &CG) { 150468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner // Update the call graph by deleting the edge from Callee to Caller 151468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner CallGraphNode *CalleeNode = CG[Callee]; 152468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner CallGraphNode *CallerNode = CG[Caller]; 153468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner CallerNode->removeCallEdgeTo(CalleeNode); 154468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner 155d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Since we inlined some uninlined call sites in the callee into the caller, 156468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner // add edges from the caller to all of the callees of the callee. 157468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner for (CallGraphNode::iterator I = CalleeNode->begin(), 158d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner E = CalleeNode->end(); I != E; ++I) { 159d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner const Instruction *OrigCall = I->first.getInstruction(); 160d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 1615e665f559419c7f58a4fd9360cd488f065505c44Chris Lattner DenseMap<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall); 162981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner // Only copy the edge if the call was inlined! 163981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner if (VMI != ValueMap.end() && VMI->second) { 1641bb3a402574557cb228f8a96030776c229e282e5Chris Lattner // If the call was inlined, but then constant folded, there is no edge to 1651bb3a402574557cb228f8a96030776c229e282e5Chris Lattner // add. Check for this case. 1661bb3a402574557cb228f8a96030776c229e282e5Chris Lattner if (Instruction *NewCall = dyn_cast<Instruction>(VMI->second)) 1671bb3a402574557cb228f8a96030776c229e282e5Chris Lattner CallerNode->addCalledFunction(CallSite::get(NewCall), I->second); 168d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 169d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 170468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 171468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner 172cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 173ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// InlineFunction - This function inlines the called function into the basic 174ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// block of the caller. This returns false if it is not possible to inline this 175ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// call. The program is still in a well defined state if this occurs though. 176ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// 177fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// Note that this only does one level of inlining. For example, if the 178fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 179ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// exists in the instruction stream. Similiarly this will inline a recursive 180ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// function by one level. 181ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// 1821dfdf8255e803d6376f5fe94a113f892e796ae6cChris Lattnerbool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) { 18380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner Instruction *TheCall = CS.getInstruction(); 18480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner assert(TheCall->getParent() && TheCall->getParent()->getParent() && 18580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner "Instruction not in function!"); 186ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 18780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner const Function *CalledFunc = CS.getCalledFunction(); 188ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner if (CalledFunc == 0 || // Can't inline external function or indirect 1895cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer CalledFunc->isDeclaration() || // call, or call to a vararg function! 190ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner CalledFunc->getFunctionType()->isVarArg()) return false; 191ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 1921b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner 1931b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner // If the call to the callee is a non-tail call, we must clear the 'tail' 1941b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner // flags on any calls that we inline. 1951b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner bool MustClearTailCallFlags = 1963799ed83b4cb80695a81294da6a7d18cf0884f5eChris Lattner isa<CallInst>(TheCall) && !cast<CallInst>(TheCall)->isTailCall(); 1971b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner 19880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock *OrigBB = TheCall->getParent(); 199ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner Function *Caller = OrigBB->getParent(); 200ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 2015052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner // Get an iterator to the last basic block in the function, which will have 2025052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner // the new function inlined after it. 2035052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner // 2045052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner Function::iterator LastBlock = &Caller->back(); 2055052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner 2065e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Make sure to capture all of the return instructions from the cloned 2075e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // function. 2085052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner std::vector<ReturnInst*> Returns; 209cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner ClonedCodeInfo InlinedFunctionInfo; 210d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner Function::iterator FirstNewBlock; 211d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 2125e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner { // Scope to destroy ValueMap after cloning. 2135e665f559419c7f58a4fd9360cd488f065505c44Chris Lattner DenseMap<const Value*, Value*> ValueMap; 2145b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner 2155b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // Calculate the vector of arguments to pass into the function cloner, which 2165b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // matches up the formal to the actual argument values. 217fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman assert(std::distance(CalledFunc->arg_begin(), CalledFunc->arg_end()) == 2185e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner std::distance(CS.arg_begin(), CS.arg_end()) && 2195e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner "No varargs calls can be inlined!"); 2205e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner CallSite::arg_iterator AI = CS.arg_begin(); 221e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::const_arg_iterator I = CalledFunc->arg_begin(), 222e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner E = CalledFunc->arg_end(); I != E; ++I, ++AI) 2235e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner ValueMap[I] = *AI; 224fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 2255b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // We want the inliner to prune the code as it copies. We would LOVE to 2265b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // have no dead or constant instructions leftover after inlining occurs 2275b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // (which can happen, e.g., because an argument was constant), but we'll be 2285b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // happy with whatever the cloner can do. 2295b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner CloneAndPruneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i", 2301dfdf8255e803d6376f5fe94a113f892e796ae6cChris Lattner &InlinedFunctionInfo, TD); 231d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 232d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Remember the first block that is newly cloned over. 233d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner FirstNewBlock = LastBlock; ++FirstNewBlock; 234d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 235d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Update the callgraph if requested. 236d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner if (CG) 237d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner UpdateCallGraphAfterInlining(Caller, CalledFunc, FirstNewBlock, ValueMap, 238d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner *CG); 239fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman } 240d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 241ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // If there are any alloca instructions in the block that used to be the entry 242ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // block for the callee, move them to the entry block of the caller. First 243ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // calculate which instruction they should be inserted before. We insert the 244ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // instructions at the end of the current alloca list. 245ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 24621f20558d629f7ff8f64c20746d890d29328a544Chris Lattner { 24780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock::iterator InsertPoint = Caller->begin()->begin(); 2485e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner for (BasicBlock::iterator I = FirstNewBlock->begin(), 2495e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner E = FirstNewBlock->end(); I != E; ) 25033bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner if (AllocaInst *AI = dyn_cast<AllocaInst>(I++)) { 25133bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner // If the alloca is now dead, remove it. This often occurs due to code 25233bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner // specialization. 25333bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner if (AI->use_empty()) { 25433bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner AI->eraseFromParent(); 25533bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner continue; 25633bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner } 25733bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner 258f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner if (isa<Constant>(AI->getArraySize())) { 25921f20558d629f7ff8f64c20746d890d29328a544Chris Lattner // Scan for the block of allocas that we can move over, and move them 26021f20558d629f7ff8f64c20746d890d29328a544Chris Lattner // all at once. 261c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner while (isa<AllocaInst>(I) && 262c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner isa<Constant>(cast<AllocaInst>(I)->getArraySize())) 263c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner ++I; 264c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner 265c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner // Transfer all of the allocas over in a block. Using splice means 266c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner // that they instructions aren't removed from the symbol table, then 267c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner // reinserted. 268c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner Caller->front().getInstList().splice(InsertPoint, 269c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner FirstNewBlock->getInstList(), 270c1df7e1799a7a97602bdedec818f7ab1dc514b4eChris Lattner AI, I); 271f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner } 27233bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner } 27380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } 27480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 275bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // If the inlined code contained dynamic alloca instructions, wrap the inlined 276bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // code with llvm.stacksave/llvm.stackrestore intrinsics. 277bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner if (InlinedFunctionInfo.ContainsDynamicAllocas) { 278bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner Module *M = Caller->getParent(); 279a121fddf3c34938d866ac4bb67247a479dedcd1bChris Lattner const Type *BytePtr = PointerType::get(Type::Int8Ty); 280bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Get the two intrinsics we care about. 281a121fddf3c34938d866ac4bb67247a479dedcd1bChris Lattner Constant *StackSave, *StackRestore; 282a121fddf3c34938d866ac4bb67247a479dedcd1bChris Lattner StackSave = M->getOrInsertFunction("llvm.stacksave", BytePtr, NULL); 283bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner StackRestore = M->getOrInsertFunction("llvm.stackrestore", Type::VoidTy, 284a121fddf3c34938d866ac4bb67247a479dedcd1bChris Lattner BytePtr, NULL); 285d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 286d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // If we are preserving the callgraph, add edges to the stacksave/restore 287d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // functions for the calls we insert. 28821ba23d0d88cff57dcdf104cd6914dabcbc68131Chris Lattner CallGraphNode *StackSaveCGN = 0, *StackRestoreCGN = 0, *CallerNode = 0; 289d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner if (CG) { 290a121fddf3c34938d866ac4bb67247a479dedcd1bChris Lattner // We know that StackSave/StackRestore are Function*'s, because they are 291a121fddf3c34938d866ac4bb67247a479dedcd1bChris Lattner // intrinsics which must have the right types. 292a121fddf3c34938d866ac4bb67247a479dedcd1bChris Lattner StackSaveCGN = CG->getOrInsertFunction(cast<Function>(StackSave)); 293a121fddf3c34938d866ac4bb67247a479dedcd1bChris Lattner StackRestoreCGN = CG->getOrInsertFunction(cast<Function>(StackRestore)); 294d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner CallerNode = (*CG)[Caller]; 295d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 296d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 297bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Insert the llvm.stacksave. 298d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner CallInst *SavedPtr = new CallInst(StackSave, "savedstack", 299d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner FirstNewBlock->begin()); 300d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner if (CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN); 301d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 302bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Insert a call to llvm.stackrestore before any return instructions in the 303bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // inlined function. 304d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 305d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner CallInst *CI = new CallInst(StackRestore, SavedPtr, "", Returns[i]); 306d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner if (CG) CallerNode->addCalledFunction(CI, StackRestoreCGN); 307d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 308468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner 309468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner // Count the number of StackRestore calls we insert. 310468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner unsigned NumStackRestores = Returns.size(); 311bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner 312bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // If we are inlining an invoke instruction, insert restores before each 313bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // unwind. These unwinds will be rewritten into branches later. 314bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) { 315bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner for (Function::iterator BB = FirstNewBlock, E = Caller->end(); 316bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner BB != E; ++BB) 317468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { 318bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner new CallInst(StackRestore, SavedPtr, "", UI); 319468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner ++NumStackRestores; 320468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner } 321468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner } 322bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner } 323bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner 3241fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner // If we are inlining tail call instruction through a call site that isn't 3251fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner // marked 'tail', we must remove the tail marker for any calls in the inlined 3261fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner // code. 3271fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner if (MustClearTailCallFlags && InlinedFunctionInfo.ContainsCalls) { 3281b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner for (Function::iterator BB = FirstNewBlock, E = Caller->end(); 3291b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner BB != E; ++BB) 3301b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 3311b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner if (CallInst *CI = dyn_cast<CallInst>(I)) 3321b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner CI->setTailCall(false); 3331b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner } 3341b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner 3355e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // If we are inlining for an invoke instruction, we must make sure to rewrite 3365e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // any inlined 'unwind' instructions into branches to the invoke exception 3375e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // destination, and call instructions into invoke instructions. 338cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) 339cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo); 3405e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner 34144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If we cloned in _exactly one_ basic block, and if that block ends in a 34244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // return instruction, we splice the body of the inlined callee directly into 34344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // the calling basic block. 34444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) { 34544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Move all of the instructions right before the call. 34644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(), 34744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner FirstNewBlock->begin(), FirstNewBlock->end()); 34844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Remove the cloned basic block. 34944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Caller->getBasicBlockList().pop_back(); 350fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 35144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If the call site was an invoke instruction, add a branch to the normal 35244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // destination. 35344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) 35444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner new BranchInst(II->getNormalDest(), TheCall); 35544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 35644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If the return instruction returned a value, replace uses of the call with 35744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // uses of the returned value. 35844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner if (!TheCall->use_empty()) 35944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); 36044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 36144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Since we are now done with the Call/Invoke, we can delete it. 36244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner TheCall->getParent()->getInstList().erase(TheCall); 36344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 36444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Since we are now done with the return instruction, delete it also. 36544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Returns[0]->getParent()->getInstList().erase(Returns[0]); 36644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 36744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // We are now done with the inlining. 36844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner return true; 36944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner } 37044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 37144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Otherwise, we have the normal case, of more than one block to inline or 37244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // multiple return sites. 37344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 3745e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // We want to clone the entire callee function into the hole between the 3755e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // "starter" and "ender" blocks. How we accomplish this depends on whether 3765e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // this is an invoke instruction or a call instruction. 3775e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner BasicBlock *AfterCallBB; 3785e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { 379fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 3805e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Add an unconditional branch to make this look like the CallInst case... 3815e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall); 382fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 3835e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Split the basic block. This guarantees that no PHI nodes will have to be 3845e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // updated due to new incoming edges, and make the invoke case more 3855e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // symmetric to the call case. 3865e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner AfterCallBB = OrigBB->splitBasicBlock(NewBr, 387284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner CalledFunc->getName()+".exit"); 388fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 3895e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } else { // It's a call 39044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If this is a call instruction, we need to split the basic block that 39144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // the call lives in. 3925e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // 3935e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner AfterCallBB = OrigBB->splitBasicBlock(TheCall, 394284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner CalledFunc->getName()+".exit"); 3955e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 3965e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner 39744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Change the branch that used to go to AfterCallBB to branch to the first 39844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // basic block of the inlined function. 39944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // 40044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner TerminatorInst *Br = OrigBB->getTerminator(); 401fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman assert(Br && Br->getOpcode() == Instruction::Br && 40244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner "splitBasicBlock broken!"); 40344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Br->setOperand(0, FirstNewBlock); 40444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 40544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 40644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Now that the function is correct, make it a little bit nicer. In 40744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // particular, move the basic blocks inserted from the end of the function 40844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // into the space made by splitting the source basic block. 40944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // 41044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), 41144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner FirstNewBlock, Caller->end()); 41244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 4135e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Handle all of the return instructions that we just cloned in, and eliminate 4145e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // any users of the original call/invoke instruction. 4155e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (Returns.size() > 1) { 4165e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // The PHI node should go at the front of the new basic block to merge all 4175e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // possible incoming values. 4185e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // 4195e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner PHINode *PHI = 0; 4205e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (!TheCall->use_empty()) { 4215e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner PHI = new PHINode(CalledFunc->getReturnType(), 4225e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner TheCall->getName(), AfterCallBB->begin()); 423fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4245e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Anything that used the result of the function call should now use the 4255e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // PHI node as their operand. 4265e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // 4275e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner TheCall->replaceAllUsesWith(PHI); 4285e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 429fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4305e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Loop over all of the return instructions, turning them into unconditional 4315e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // branches to the merge point now, and adding entries to the PHI node as 4325e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // appropriate. 4335e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 4345e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner ReturnInst *RI = Returns[i]; 435fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4365e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (PHI) { 4375e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner assert(RI->getReturnValue() && "Ret should have value!"); 438fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman assert(RI->getReturnValue()->getType() == PHI->getType() && 4395e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner "Ret value not consistent in function!"); 4405e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner PHI->addIncoming(RI->getReturnValue(), RI->getParent()); 4415e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 442fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4435e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Add a branch to the merge point where the PHI node lives if it exists. 4445e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner new BranchInst(AfterCallBB, RI); 445fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4465e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Delete the return instruction now 4475e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner RI->getParent()->getInstList().erase(RI); 4485e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 449fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4505e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } else if (!Returns.empty()) { 4515e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Otherwise, if there is exactly one return value, just replace anything 4525e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // using the return value of the call with the computed value. 4535e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (!TheCall->use_empty()) 4545e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); 455fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 456cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Splice the code from the return block into the block that it will return 457cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // to, which contains the code that was after the call. 458cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner BasicBlock *ReturnBB = Returns[0]->getParent(); 459adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner AfterCallBB->getInstList().splice(AfterCallBB->begin(), 460adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner ReturnBB->getInstList()); 461cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner 462adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner // Update PHI nodes that use the ReturnBB to use the AfterCallBB. 463adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner ReturnBB->replaceAllUsesWith(AfterCallBB); 464fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 465adfd32f8eefc804dce24d14450c10f592e643e2dChris Lattner // Delete the return instruction now and empty ReturnBB now. 4663787e765facfad5ea62753922d940bcdd52afd57Chris Lattner Returns[0]->eraseFromParent(); 4673787e765facfad5ea62753922d940bcdd52afd57Chris Lattner ReturnBB->eraseFromParent(); 4683787e765facfad5ea62753922d940bcdd52afd57Chris Lattner } else if (!TheCall->use_empty()) { 4693787e765facfad5ea62753922d940bcdd52afd57Chris Lattner // No returns, but something is using the return value of the call. Just 4703787e765facfad5ea62753922d940bcdd52afd57Chris Lattner // nuke the result. 4713787e765facfad5ea62753922d940bcdd52afd57Chris Lattner TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); 4725e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 473fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4745e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Since we are now done with the Call/Invoke, we can delete it. 4753787e765facfad5ea62753922d940bcdd52afd57Chris Lattner TheCall->eraseFromParent(); 476ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 4777152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // We should always be able to fold the entry block of the function into the 4787152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // single predecessor of the block... 479cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); 4807152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); 48144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 482cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Splice the code entry block into calling block, right before the 483cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // unconditional branch. 484cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); 485cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes 486cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner 487cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Remove the unconditional branch. 488cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner OrigBB->getInstList().erase(Br); 489cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner 490cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Now we can remove the CalleeEntry block, which is now empty. 491cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner Caller->getBasicBlockList().erase(CalleeEntry); 492468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner 493ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner return true; 494ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner} 495