InlineFunction.cpp revision f7703df4968084c18c248c1feea9961c19a32e6a
1ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===- InlineFunction.cpp - Code to perform function inlining -------------===// 2b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 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. 7b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 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// FIXME: This pass should transform alloca instructions in the called function 14ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// into malloc/free pairs! Or perhaps it should refuse to inline them! 15ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// 16ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===----------------------------------------------------------------------===// 17ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 18ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/Transforms/Utils/Cloning.h" 1980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Constant.h" 207152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner#include "llvm/DerivedTypes.h" 21ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/Module.h" 2280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Instructions.h" 2380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Intrinsics.h" 2480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Support/CallSite.h" 257152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner#include "llvm/Transforms/Utils/Local.h" 26f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm; 27ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 28f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerbool llvm::InlineFunction(CallInst *CI) { return InlineFunction(CallSite(CI)); } 29f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerbool llvm::InlineFunction(InvokeInst *II) {return InlineFunction(CallSite(II));} 3080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 31ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// InlineFunction - This function inlines the called function into the basic 32ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// block of the caller. This returns false if it is not possible to inline this 33ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// call. The program is still in a well defined state if this occurs though. 34ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// 35ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// Note that this only does one level of inlining. For example, if the 36ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 37ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// exists in the instruction stream. Similiarly this will inline a recursive 38ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// function by one level. 39ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// 40f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerbool llvm::InlineFunction(CallSite CS) { 4180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner Instruction *TheCall = CS.getInstruction(); 4280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner assert(TheCall->getParent() && TheCall->getParent()->getParent() && 4380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner "Instruction not in function!"); 44ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 4580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner const Function *CalledFunc = CS.getCalledFunction(); 46ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner if (CalledFunc == 0 || // Can't inline external function or indirect 47ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner CalledFunc->isExternal() || // call, or call to a vararg function! 48ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner CalledFunc->getFunctionType()->isVarArg()) return false; 49ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 5080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock *OrigBB = TheCall->getParent(); 51ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner Function *Caller = OrigBB->getParent(); 52ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 5380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // We want to clone the entire callee function into the whole between the 5480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // "starter" and "ender" blocks. How we accomplish this depends on whether 5580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // this is an invoke instruction or a call instruction. 5680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 5780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock *InvokeDest = 0; // Exception handling destination 5851d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner std::vector<Value*> InvokeDestPHIValues; // Values for PHI nodes in InvokeDest 5980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock *AfterCallBB; 6051d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner 6180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { 6280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner InvokeDest = II->getExceptionalDest(); 63ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 64198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner // If there are PHI nodes in the exceptional destination block, we need to 65198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner // keep track of which values came into them from this invoke, then remove 66198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner // the entry for this block. 67198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); 68198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner PHINode *PN = dyn_cast<PHINode>(I); ++I) { 69198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner // Save the value to use for this edge... 70198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(OrigBB)); 71198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner } 72198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner 7380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // Add an unconditional branch to make this look like the CallInst case... 74f98a08490801b8bc6e2006627d34095f5b8567c8Chris Lattner BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall); 75f98a08490801b8bc6e2006627d34095f5b8567c8Chris Lattner 76f98a08490801b8bc6e2006627d34095f5b8567c8Chris Lattner // Split the basic block. This guarantees that no PHI nodes will have to be 77f98a08490801b8bc6e2006627d34095f5b8567c8Chris Lattner // updated due to new incoming edges, and make the invoke case more 78f98a08490801b8bc6e2006627d34095f5b8567c8Chris Lattner // symmetric to the call case. 79f98a08490801b8bc6e2006627d34095f5b8567c8Chris Lattner AfterCallBB = OrigBB->splitBasicBlock(NewBr, 80f98a08490801b8bc6e2006627d34095f5b8567c8Chris Lattner CalledFunc->getName()+".entry"); 8180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 8280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // Remove (unlink) the InvokeInst from the function... 8380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner OrigBB->getInstList().remove(TheCall); 8451d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner 8580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } else { // It's a call 8680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // If this is a call instruction, we need to split the basic block that the 8780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // call lives in. 8880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // 8980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner AfterCallBB = OrigBB->splitBasicBlock(TheCall, 9080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner CalledFunc->getName()+".entry"); 9180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // Remove (unlink) the CallInst from the function... 9280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner AfterCallBB->getInstList().remove(TheCall); 9380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } 94ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 95ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // If we have a return value generated by this call, convert it into a PHI 96ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // node that gets values from each of the old RET instructions in the original 97ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // function. 98ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 99ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner PHINode *PHI = 0; 10080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner if (!TheCall->use_empty()) { 101ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // The PHI node should go at the front of the new basic block to merge all 102ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // possible incoming values. 103ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 10480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner PHI = new PHINode(CalledFunc->getReturnType(), TheCall->getName(), 10580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner AfterCallBB->begin()); 106ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 107ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Anything that used the result of the function call should now use the PHI 108ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // node as their operand. 109ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 11080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner TheCall->replaceAllUsesWith(PHI); 111ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner } 112ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 113ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Get an iterator to the last basic block in the function, which will have 114ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // the new function inlined after it. 115ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 116ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner Function::iterator LastBlock = &Caller->back(); 117ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 118ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Calculate the vector of arguments to pass into the function cloner... 119ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner std::map<const Value*, Value*> ValueMap; 12080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner assert(std::distance(CalledFunc->abegin(), CalledFunc->aend()) == 12180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner std::distance(CS.arg_begin(), CS.arg_end()) && 12280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner "No varargs calls can be inlined!"); 123ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 12480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner CallSite::arg_iterator AI = CS.arg_begin(); 125ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend(); 12680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner I != E; ++I, ++AI) 12780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner ValueMap[I] = *AI; 128ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 12980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // Since we are now done with the Call/Invoke, we can delete it. 13080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner delete TheCall; 131ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 132ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Make a vector to capture the return instructions in the cloned function... 133ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner std::vector<ReturnInst*> Returns; 134ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 135ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Do all of the hard part of cloning the callee into the caller... 136ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner CloneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i"); 137ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 138ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Loop over all of the return instructions, turning them into unconditional 139ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // branches to the merge point now... 140ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 141ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner ReturnInst *RI = Returns[i]; 142ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner BasicBlock *BB = RI->getParent(); 143ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 14480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // Add a branch to the merge point where the PHI node lives if it exists. 14580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner new BranchInst(AfterCallBB, RI); 146ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 147ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner if (PHI) { // The PHI node should include this value! 148ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner assert(RI->getReturnValue() && "Ret should have value!"); 149ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner assert(RI->getReturnValue()->getType() == PHI->getType() && 150ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner "Ret value not consistent in function!"); 151ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner PHI->addIncoming(RI->getReturnValue(), BB); 152ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner } 153ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 154ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Delete the return instruction now 155ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner BB->getInstList().erase(RI); 156ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner } 157ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 158ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Check to see if the PHI node only has one argument. This is a common 159ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // case resulting from there only being a single return instruction in the 160ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // function call. Because this is so common, eliminate the PHI node. 161ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 162ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner if (PHI && PHI->getNumIncomingValues() == 1) { 163ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner PHI->replaceAllUsesWith(PHI->getIncomingValue(0)); 164ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner PHI->getParent()->getInstList().erase(PHI); 165ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner } 166ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 16780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // Change the branch that used to go to AfterCallBB to branch to the first 16880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // basic block of the inlined function. 169ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 170ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner TerminatorInst *Br = OrigBB->getTerminator(); 171ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner assert(Br && Br->getOpcode() == Instruction::Br && 172ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner "splitBasicBlock broken!"); 173ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner Br->setOperand(0, ++LastBlock); 174ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 175ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // If there are any alloca instructions in the block that used to be the entry 176ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // block for the callee, move them to the entry block of the caller. First 177ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // calculate which instruction they should be inserted before. We insert the 178ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // instructions at the end of the current alloca list. 179ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 18080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner if (isa<AllocaInst>(LastBlock->begin())) { 18180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock::iterator InsertPoint = Caller->begin()->begin(); 18280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner while (isa<AllocaInst>(InsertPoint)) ++InsertPoint; 18380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 18480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner for (BasicBlock::iterator I = LastBlock->begin(), E = LastBlock->end(); 18580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner I != E; ) 186f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner if (AllocaInst *AI = dyn_cast<AllocaInst>(I++)) 187f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner if (isa<Constant>(AI->getArraySize())) { 188f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner LastBlock->getInstList().remove(AI); 189f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner Caller->front().getInstList().insert(InsertPoint, AI); 190f775f95369ba7d88a5cb8b6f1900241699d79351Chris Lattner } 19180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } 19280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 19380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // If we just inlined a call due to an invoke instruction, scan the inlined 19480a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // function checking for function calls that should now be made into invoke 195ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner // instructions, and for unwind's which should be turned into branches. 19651d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner if (InvokeDest) { 197ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner for (Function::iterator BB = LastBlock, E = Caller->end(); BB != E; ++BB) { 19880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 19980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // We only need to check for function calls: inlined invoke instructions 20080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // require no special handling... 20180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner if (CallInst *CI = dyn_cast<CallInst>(I)) { 202e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner // Convert this function call into an invoke instruction... 203e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner 204e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner // First, split the basic block... 205e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); 206e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner 207e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner // Next, create the new invoke instruction, inserting it at the end 208e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner // of the old basic block. 209e07007c2bd0dcb775ff3eecabb4c31ce8dc5fab0Chris Lattner InvokeInst *II = 210e07007c2bd0dcb775ff3eecabb4c31ce8dc5fab0Chris Lattner new InvokeInst(CI->getCalledValue(), Split, InvokeDest, 211e07007c2bd0dcb775ff3eecabb4c31ce8dc5fab0Chris Lattner std::vector<Value*>(CI->op_begin()+1, CI->op_end()), 212e07007c2bd0dcb775ff3eecabb4c31ce8dc5fab0Chris Lattner CI->getName(), BB->getTerminator()); 213e07007c2bd0dcb775ff3eecabb4c31ce8dc5fab0Chris Lattner 214e07007c2bd0dcb775ff3eecabb4c31ce8dc5fab0Chris Lattner // Make sure that anything using the call now uses the invoke! 215e07007c2bd0dcb775ff3eecabb4c31ce8dc5fab0Chris Lattner CI->replaceAllUsesWith(II); 216e07007c2bd0dcb775ff3eecabb4c31ce8dc5fab0Chris Lattner 217e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner // Delete the unconditional branch inserted by splitBasicBlock 218e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner BB->getInstList().pop_back(); 219e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner Split->getInstList().pop_front(); // Delete the original call 220e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner 22151d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner // Update any PHI nodes in the exceptional block to indicate that 22251d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner // there is now a new entry in them. 22351d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner unsigned i = 0; 22451d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); 22551d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner PHINode *PN = dyn_cast<PHINode>(I); ++I, ++i) 22651d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner PN->addIncoming(InvokeDestPHIValues[i], BB); 22751d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner 228e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner // This basic block is now complete, start scanning the next one. 229e4d90964096c240c9e29ae414792ed1246db4ee8Chris Lattner break; 23080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } else { 23180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner ++I; 23280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } 23380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } 234ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 235ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { 236ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner // An UnwindInst requires special handling when it gets inlined into an 237ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner // invoke site. Once this happens, we know that the unwind would cause 238ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner // a control transfer to the invoke exception destination, so we can 239ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner // transform it into a direct branch to the exception destination. 240f8485c643412dbff46fe87ea2867445169a5c28eChris Lattner new BranchInst(InvokeDest, UI); 241ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner 242ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner // Delete the unwind instruction! 243ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner UI->getParent()->getInstList().pop_back(); 244198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner 245198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner // Update any PHI nodes in the exceptional block to indicate that 246198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner // there is now a new entry in them. 247198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner unsigned i = 0; 248198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); 249198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner PHINode *PN = dyn_cast<PHINode>(I); ++I, ++i) 250198f4507e2ff934a8ac3aa0a82526fa6a153b70bChris Lattner PN->addIncoming(InvokeDestPHIValues[i], BB); 251ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner } 252ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner } 253ee5457cbe88b7f691f774de8515d9a94226d1b00Chris Lattner 25451d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner // Now that everything is happy, we have one final detail. The PHI nodes in 25551d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner // the exception destination block still have entries due to the original 25651d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner // invoke instruction. Eliminate these entries (which might even delete the 25751d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner // PHI node) now. 25851d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); 25951d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner PHINode *PN = dyn_cast<PHINode>(I); ++I) 260dd7036d19a3bfd3dff2531c7fdcd7901b2442de9Chris Lattner PN->removeIncomingValue(AfterCallBB); 26151d6816089a66c171dc23b50d62989ac6bb5c491Chris Lattner } 262ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Now that the function is correct, make it a little bit nicer. In 263ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // particular, move the basic blocks inserted from the end of the function 264ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // into the space made by splitting the source basic block. 265ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 26680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), 267ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner LastBlock, Caller->end()); 268ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 2697152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // We should always be able to fold the entry block of the function into the 2707152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // single predecessor of the block... 2717152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); 2727152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); 2737152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner SimplifyCFG(CalleeEntry); 2747152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner 2757152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // Okay, continue the CFG cleanup. It's often the case that there is only a 2767152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // single return instruction in the callee function. If this is the case, 27780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // then we have an unconditional branch from the return block to the 27880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner // 'AfterCallBB'. Check for this case, and eliminate the branch is possible. 27980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner SimplifyCFG(AfterCallBB); 280ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner return true; 281ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner} 282