InlineFunction.cpp revision 3ecfc861b4365f341c5c969b40e1afccde676e6f
1ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===- InlineFunction.cpp - Code to perform function inlining -------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// The LLVM Compiler Infrastructure 4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// 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" 20517576d6f96a0acde9bab79553d89f4ceba20cf6Devang Patel#include "llvm/IntrinsicInst.h" 2180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Intrinsics.h" 22eaf42abab6d465c38891345d999255871cf03943Devang Patel#include "llvm/Attributes.h" 23468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner#include "llvm/Analysis/CallGraph.h" 24517576d6f96a0acde9bab79553d89f4ceba20cf6Devang Patel#include "llvm/Analysis/DebugInfo.h" 256fb881c036c32728c4a128d81b6083457e534e09Duncan Sands#include "llvm/Analysis/InstructionSimplify.h" 26c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner#include "llvm/Target/TargetData.h" 277569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner#include "llvm/Transforms/Utils/Local.h" 2893e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner#include "llvm/ADT/SmallVector.h" 29641ca93cff0f957fc5fb9dfb05d2a4a340aa8af7Devang Patel#include "llvm/ADT/StringExtras.h" 3080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Support/CallSite.h" 31f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm; 32ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 3360915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI) { 3460915146f4d35e12f10dcdaa155596fac79184daChris Lattner return InlineFunction(CallSite(CI), IFI); 35468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 3660915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI) { 3760915146f4d35e12f10dcdaa155596fac79184daChris Lattner return InlineFunction(CallSite(II), IFI); 38468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 3980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 40135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 41135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into 42f61f89ae14cf332a014a598153137113af34002fEric Christopher/// an invoke, we have to turn all of the calls that can throw into 43135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// invokes. This function analyze BB to see if there are any calls, and if so, 44135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI 4581dfb3885252fbf621b080827a080099864415f8Chris Lattner/// nodes in that block with the values specified in InvokeDestPHIValues. 46135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// 47135755dae4c3fa8003b76150689d5064aa4612eeChris Lattnerstatic void HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB, 48135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner BasicBlock *InvokeDest, 4981dfb3885252fbf621b080827a080099864415f8Chris Lattner const SmallVectorImpl<Value*> &InvokeDestPHIValues) { 50135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { 51135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Instruction *I = BBI++; 52135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 53135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // We only need to check for function calls: inlined invoke 54135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // instructions require no special handling. 55135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallInst *CI = dyn_cast<CallInst>(I); 56135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (CI == 0) continue; 57135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 58135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If this call cannot unwind, don't convert it to an invoke. 59135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (CI->doesNotThrow()) 60135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 61135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 62135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Convert this function call into an invoke instruction. 63135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // First, split the basic block. 64135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); 65135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 66135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Next, create the new invoke instruction, inserting it at the end 67135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // of the old basic block. 68aeff385e3e21a3cde564ef49da1bdd7b745aa44cGabor Greif ImmutableCallSite CS(CI); 69aeff385e3e21a3cde564ef49da1bdd7b745aa44cGabor Greif SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end()); 70135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner InvokeInst *II = 71135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner InvokeInst::Create(CI->getCalledValue(), Split, InvokeDest, 72135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner InvokeArgs.begin(), InvokeArgs.end(), 73135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CI->getName(), BB->getTerminator()); 74135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner II->setCallingConv(CI->getCallingConv()); 75135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner II->setAttributes(CI->getAttributes()); 76135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 7781dfb3885252fbf621b080827a080099864415f8Chris Lattner // Make sure that anything using the call now uses the invoke! This also 78076863225ce070345ff7048f48b3550e00598a10Chris Lattner // updates the CallGraph if present, because it uses a WeakVH. 79135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CI->replaceAllUsesWith(II); 80135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 81135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Delete the unconditional branch inserted by splitBasicBlock 82135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner BB->getInstList().pop_back(); 83135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Split->getInstList().pop_front(); // Delete the original call 84135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 85135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Update any PHI nodes in the exceptional block to indicate that 86135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // there is now a new entry in them. 87135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner unsigned i = 0; 88135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); 89135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner isa<PHINode>(I); ++I, ++i) 90135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner cast<PHINode>(I)->addIncoming(InvokeDestPHIValues[i], BB); 91135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 92135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // This basic block is now complete, the caller will continue scanning the 93135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // next one. 94135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner return; 95135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 96135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner} 97135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 98135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 99cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls 100cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// in the body of the inlined function into invokes and turn unwind 101cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// instructions into branches to the invoke unwind dest. 102cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// 103dac5c4b10b387b55c2394cd98a64f3f1394df2e8Nick Lewycky/// II is the invoke instruction being inlined. FirstNewBlock is the first 104cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// block of the inlined code (the last block is the end of the function), 105cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// and InlineCodeInfo is information about the code that got inlined. 106cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattnerstatic void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, 10781dfb3885252fbf621b080827a080099864415f8Chris Lattner ClonedCodeInfo &InlinedCodeInfo) { 108cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner BasicBlock *InvokeDest = II->getUnwindDest(); 109135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner SmallVector<Value*, 8> InvokeDestPHIValues; 110cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 111cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // If there are PHI nodes in the unwind destination block, we need to 112cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // keep track of which values came into them from this invoke, then remove 113cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // the entry for this block. 114cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner BasicBlock *InvokeBlock = II->getParent(); 115cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) { 116cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner PHINode *PN = cast<PHINode>(I); 117cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // Save the value to use for this edge. 118cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock)); 119cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 120cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 121cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner Function *Caller = FirstNewBlock->getParent(); 122a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 123cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // The inlined code is currently at the end of the function, scan from the 124cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // start of the inlined code to its end, checking for stuff we need to 125135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // rewrite. If the code doesn't have calls or unwinds, we know there is 126135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // nothing to rewrite. 127135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) { 128135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Now that everything is happy, we have one final detail. The PHI nodes in 129135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // the exception destination block still have entries due to the original 130135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // invoke instruction. Eliminate these entries (which might even delete the 131135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // PHI node) now. 132135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner InvokeDest->removePredecessor(II->getParent()); 133135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner return; 134135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 135135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 136135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){ 137135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (InlinedCodeInfo.ContainsCalls) 138135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner HandleCallsInBlockInlinedThroughInvoke(BB, InvokeDest, 13981dfb3885252fbf621b080827a080099864415f8Chris Lattner InvokeDestPHIValues); 140135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 141135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { 142135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // An UnwindInst requires special handling when it gets inlined into an 143135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // invoke site. Once this happens, we know that the unwind would cause 144135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // a control transfer to the invoke exception destination, so we can 145135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // transform it into a direct branch to the exception destination. 146135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner BranchInst::Create(InvokeDest, UI); 147135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 148135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Delete the unwind instruction! 149135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner UI->eraseFromParent(); 150135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 151135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Update any PHI nodes in the exceptional block to indicate that 152135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // there is now a new entry in them. 153135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner unsigned i = 0; 154135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (BasicBlock::iterator I = InvokeDest->begin(); 155135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner isa<PHINode>(I); ++I, ++i) { 156135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner PHINode *PN = cast<PHINode>(I); 157135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner PN->addIncoming(InvokeDestPHIValues[i], BB); 158cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 159cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 160cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 161cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 162cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // Now that everything is happy, we have one final detail. The PHI nodes in 163cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // the exception destination block still have entries due to the original 164cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // invoke instruction. Eliminate these entries (which might even delete the 165cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // PHI node) now. 166cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner InvokeDest->removePredecessor(II->getParent()); 167cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner} 168cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 169d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee 170d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// into the caller, update the specified callgraph to reflect the changes we 171d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// made. Note that it's possible that not all code was copied over, so only 172d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands/// some edges of the callgraph may remain. 173d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sandsstatic void UpdateCallGraphAfterInlining(CallSite CS, 174d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner Function::iterator FirstNewBlock, 1751ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola ValueToValueMapTy &VMap, 176fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner InlineFunctionInfo &IFI) { 177fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner CallGraph &CG = *IFI.CG; 178d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands const Function *Caller = CS.getInstruction()->getParent()->getParent(); 179d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands const Function *Callee = CS.getCalledFunction(); 180468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner CallGraphNode *CalleeNode = CG[Callee]; 181468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner CallGraphNode *CallerNode = CG[Caller]; 182a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 183d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Since we inlined some uninlined call sites in the callee into the caller, 184468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner // add edges from the caller to all of the callees of the callee. 185c478e52bf4c12691037856ee103c66946afeab6cGabor Greif CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end(); 186c478e52bf4c12691037856ee103c66946afeab6cGabor Greif 187c478e52bf4c12691037856ee103c66946afeab6cGabor Greif // Consider the case where CalleeNode == CallerNode. 188125329891f97baedef21e4b464ba70182c3fb45eGabor Greif CallGraphNode::CalledFunctionsVector CallCache; 189c478e52bf4c12691037856ee103c66946afeab6cGabor Greif if (CalleeNode == CallerNode) { 190c478e52bf4c12691037856ee103c66946afeab6cGabor Greif CallCache.assign(I, E); 191c478e52bf4c12691037856ee103c66946afeab6cGabor Greif I = CallCache.begin(); 192c478e52bf4c12691037856ee103c66946afeab6cGabor Greif E = CallCache.end(); 193c478e52bf4c12691037856ee103c66946afeab6cGabor Greif } 194c478e52bf4c12691037856ee103c66946afeab6cGabor Greif 195c478e52bf4c12691037856ee103c66946afeab6cGabor Greif for (; I != E; ++I) { 196a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner const Value *OrigCall = I->first; 197a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 1981ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola ValueToValueMapTy::iterator VMI = VMap.find(OrigCall); 199981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner // Only copy the edge if the call was inlined! 20029d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel if (VMI == VMap.end() || VMI->second == 0) 201135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 202135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 203135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If the call was inlined, but then constant folded, there is no edge to 204135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // add. Check for this case. 205b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner Instruction *NewCall = dyn_cast<Instruction>(VMI->second); 206b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner if (NewCall == 0) continue; 2070ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner 2080ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner // Remember that this call site got inlined for the client of 2090ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner // InlineFunction. 2100ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner IFI.InlinedCalls.push_back(NewCall); 2110ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner 212b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // It's possible that inlining the callsite will cause it to go from an 213b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // indirect to a direct call by resolving a function pointer. If this 214b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // happens, set the callee of the new call site to a more precise 215b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // destination. This can also happen if the call graph node of the caller 216b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // was just unnecessarily imprecise. 217b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner if (I->second->getFunction() == 0) 218b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner if (Function *F = CallSite(NewCall).getCalledFunction()) { 219b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // Indirect call site resolved to direct call. 22086099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif CallerNode->addCalledFunction(CallSite(NewCall), CG[F]); 22186099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif 222b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner continue; 223b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner } 22486099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif 22586099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif CallerNode->addCalledFunction(CallSite(NewCall), I->second); 226d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 227135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 22839fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen // Update the call graph by deleting the edge from Callee to Caller. We must 22939fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen // do this after the loop above in case Caller and Callee are the same. 23039fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen CallerNode->removeCallEdgeFor(CS); 231468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 232468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner 2330b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// HandleByValArgument - When inlining a call site that has a byval argument, 2340b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// we have to make the implicit memcpy explicit by adding it. 235e7ae705c32906979a527926864345016e76867b9Chris Lattnerstatic Value *HandleByValArgument(Value *Arg, Instruction *TheCall, 236e7ae705c32906979a527926864345016e76867b9Chris Lattner const Function *CalledFunc, 237e7ae705c32906979a527926864345016e76867b9Chris Lattner InlineFunctionInfo &IFI, 238e7ae705c32906979a527926864345016e76867b9Chris Lattner unsigned ByValAlignment) { 2390b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner const Type *AggTy = cast<PointerType>(Arg->getType())->getElementType(); 2400b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner 2410b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // If the called function is readonly, then it could not mutate the caller's 2420b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // copy of the byval'd memory. In this case, it is safe to elide the copy and 2430b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // temporary. 2440b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner if (CalledFunc->onlyReadsMemory()) { 2450b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // If the byval argument has a specified alignment that is greater than the 2460b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // passed in pointer, then we either have to round up the input pointer or 2470b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // give up on this transformation. 2480b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner if (ByValAlignment <= 1) // 0 = unspecified, 1 = no particular alignment. 2490b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner return Arg; 2500b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner 2517569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner // If the pointer is already known to be sufficiently aligned, or if we can 2527569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner // round it up to a larger alignment, then we don't need a temporary. 2537569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner if (getOrEnforceKnownAlignment(Arg, ByValAlignment, 2547569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner IFI.TD) >= ByValAlignment) 2557569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner return Arg; 2560b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner 2577569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner // Otherwise, we have to make a memcpy to get a safe alignment. This is bad 2587569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner // for code quality, but rarely happens and is required for correctness. 2590b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner } 260e7ae705c32906979a527926864345016e76867b9Chris Lattner 261e7ae705c32906979a527926864345016e76867b9Chris Lattner LLVMContext &Context = Arg->getContext(); 262e7ae705c32906979a527926864345016e76867b9Chris Lattner 263e7ae705c32906979a527926864345016e76867b9Chris Lattner const Type *VoidPtrTy = Type::getInt8PtrTy(Context); 264e7ae705c32906979a527926864345016e76867b9Chris Lattner 265e7ae705c32906979a527926864345016e76867b9Chris Lattner // Create the alloca. If we have TargetData, use nice alignment. 266e7ae705c32906979a527926864345016e76867b9Chris Lattner unsigned Align = 1; 267e7ae705c32906979a527926864345016e76867b9Chris Lattner if (IFI.TD) 268e7ae705c32906979a527926864345016e76867b9Chris Lattner Align = IFI.TD->getPrefTypeAlignment(AggTy); 269e7ae705c32906979a527926864345016e76867b9Chris Lattner 270e7ae705c32906979a527926864345016e76867b9Chris Lattner // If the byval had an alignment specified, we *must* use at least that 271e7ae705c32906979a527926864345016e76867b9Chris Lattner // alignment, as it is required by the byval argument (and uses of the 272e7ae705c32906979a527926864345016e76867b9Chris Lattner // pointer inside the callee). 273e7ae705c32906979a527926864345016e76867b9Chris Lattner Align = std::max(Align, ByValAlignment); 274e7ae705c32906979a527926864345016e76867b9Chris Lattner 275e7ae705c32906979a527926864345016e76867b9Chris Lattner Function *Caller = TheCall->getParent()->getParent(); 276e7ae705c32906979a527926864345016e76867b9Chris Lattner 277e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *NewAlloca = new AllocaInst(AggTy, 0, Align, Arg->getName(), 278e7ae705c32906979a527926864345016e76867b9Chris Lattner &*Caller->begin()->begin()); 279e7ae705c32906979a527926864345016e76867b9Chris Lattner // Emit a memcpy. 280e7ae705c32906979a527926864345016e76867b9Chris Lattner const Type *Tys[3] = {VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context)}; 281e7ae705c32906979a527926864345016e76867b9Chris Lattner Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(), 282e7ae705c32906979a527926864345016e76867b9Chris Lattner Intrinsic::memcpy, 283e7ae705c32906979a527926864345016e76867b9Chris Lattner Tys, 3); 284e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall); 285e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *SrcCast = new BitCastInst(Arg, VoidPtrTy, "tmp", TheCall); 286e7ae705c32906979a527926864345016e76867b9Chris Lattner 287e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *Size; 288e7ae705c32906979a527926864345016e76867b9Chris Lattner if (IFI.TD == 0) 289e7ae705c32906979a527926864345016e76867b9Chris Lattner Size = ConstantExpr::getSizeOf(AggTy); 290e7ae705c32906979a527926864345016e76867b9Chris Lattner else 291e7ae705c32906979a527926864345016e76867b9Chris Lattner Size = ConstantInt::get(Type::getInt64Ty(Context), 292e7ae705c32906979a527926864345016e76867b9Chris Lattner IFI.TD->getTypeStoreSize(AggTy)); 293e7ae705c32906979a527926864345016e76867b9Chris Lattner 294e7ae705c32906979a527926864345016e76867b9Chris Lattner // Always generate a memcpy of alignment 1 here because we don't know 295e7ae705c32906979a527926864345016e76867b9Chris Lattner // the alignment of the src pointer. Other optimizations can infer 296e7ae705c32906979a527926864345016e76867b9Chris Lattner // better alignment. 297e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *CallArgs[] = { 298e7ae705c32906979a527926864345016e76867b9Chris Lattner DestCast, SrcCast, Size, 299e7ae705c32906979a527926864345016e76867b9Chris Lattner ConstantInt::get(Type::getInt32Ty(Context), 1), 300e7ae705c32906979a527926864345016e76867b9Chris Lattner ConstantInt::getFalse(Context) // isVolatile 301e7ae705c32906979a527926864345016e76867b9Chris Lattner }; 302e7ae705c32906979a527926864345016e76867b9Chris Lattner CallInst *TheMemCpy = 303e7ae705c32906979a527926864345016e76867b9Chris Lattner CallInst::Create(MemCpyFn, CallArgs, CallArgs+5, "", TheCall); 304e7ae705c32906979a527926864345016e76867b9Chris Lattner 305e7ae705c32906979a527926864345016e76867b9Chris Lattner // If we have a call graph, update it. 306e7ae705c32906979a527926864345016e76867b9Chris Lattner if (CallGraph *CG = IFI.CG) { 307e7ae705c32906979a527926864345016e76867b9Chris Lattner CallGraphNode *MemCpyCGN = CG->getOrInsertFunction(MemCpyFn); 308e7ae705c32906979a527926864345016e76867b9Chris Lattner CallGraphNode *CallerNode = (*CG)[Caller]; 309e7ae705c32906979a527926864345016e76867b9Chris Lattner CallerNode->addCalledFunction(TheMemCpy, MemCpyCGN); 310e7ae705c32906979a527926864345016e76867b9Chris Lattner } 311e7ae705c32906979a527926864345016e76867b9Chris Lattner 312e7ae705c32906979a527926864345016e76867b9Chris Lattner // Uses of the argument in the function should use our new alloca 313e7ae705c32906979a527926864345016e76867b9Chris Lattner // instead. 314e7ae705c32906979a527926864345016e76867b9Chris Lattner return NewAlloca; 315e7ae705c32906979a527926864345016e76867b9Chris Lattner} 316e7ae705c32906979a527926864345016e76867b9Chris Lattner 317ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// InlineFunction - This function inlines the called function into the basic 318ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// block of the caller. This returns false if it is not possible to inline this 319ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// call. The program is still in a well defined state if this occurs though. 320ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// 321fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// Note that this only does one level of inlining. For example, if the 322fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 323ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// exists in the instruction stream. Similiarly this will inline a recursive 324ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// function by one level. 325ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// 32660915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) { 32780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner Instruction *TheCall = CS.getInstruction(); 328e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson LLVMContext &Context = TheCall->getContext(); 32980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner assert(TheCall->getParent() && TheCall->getParent()->getParent() && 33080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner "Instruction not in function!"); 331ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 33260915146f4d35e12f10dcdaa155596fac79184daChris Lattner // If IFI has any state in it, zap it before we fill it in. 33360915146f4d35e12f10dcdaa155596fac79184daChris Lattner IFI.reset(); 33460915146f4d35e12f10dcdaa155596fac79184daChris Lattner 33580a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner const Function *CalledFunc = CS.getCalledFunction(); 336ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner if (CalledFunc == 0 || // Can't inline external function or indirect 3375cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer CalledFunc->isDeclaration() || // call, or call to a vararg function! 3380623e90398153be61226ad19f1b40d3817874526Eric Christopher CalledFunc->getFunctionType()->isVarArg()) return false; 339ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 340af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner // If the call to the callee is not a tail call, we must clear the 'tail' 3411b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner // flags on any calls that we inline. 3421b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner bool MustClearTailCallFlags = 343af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall()); 3441b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner 345f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // If the call to the callee cannot throw, set the 'nounwind' flag on any 346f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // calls that we inline. 347f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands bool MarkNoUnwind = CS.doesNotThrow(); 348f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands 34980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock *OrigBB = TheCall->getParent(); 350ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner Function *Caller = OrigBB->getParent(); 351ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 3520e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen // GC poses two hazards to inlining, which only occur when the callee has GC: 3530e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen // 1. If the caller has no GC, then the callee's GC must be propagated to the 3540e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen // caller. 3550e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen // 2. If the caller has a differing GC, it is invalid to inline. 3565eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen if (CalledFunc->hasGC()) { 3575eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen if (!Caller->hasGC()) 3585eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen Caller->setGC(CalledFunc->getGC()); 3595eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen else if (CalledFunc->getGC() != Caller->getGC()) 3600e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen return false; 3610e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen } 362a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 3635052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner // Get an iterator to the last basic block in the function, which will have 3645052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner // the new function inlined after it. 3655052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner // 3665052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner Function::iterator LastBlock = &Caller->back(); 3675052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner 3685e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Make sure to capture all of the return instructions from the cloned 3695e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // function. 370ec1bea0d94372985a0a5eb283e644c6d0dd345dcChris Lattner SmallVector<ReturnInst*, 8> Returns; 371cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner ClonedCodeInfo InlinedFunctionInfo; 3720744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen Function::iterator FirstNewBlock; 373f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands 37429d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel { // Scope to destroy VMap after cloning. 3751ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola ValueToValueMapTy VMap; 3765b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner 3779614fcc640eb628cc5dfddb277ebae9f6cb61014Dan Gohman assert(CalledFunc->arg_size() == CS.arg_size() && 3785e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner "No varargs calls can be inlined!"); 379a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 380c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner // Calculate the vector of arguments to pass into the function cloner, which 381c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner // matches up the formal to the actual argument values. 3825e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner CallSite::arg_iterator AI = CS.arg_begin(); 383c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner unsigned ArgNo = 0; 384e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::const_arg_iterator I = CalledFunc->arg_begin(), 385c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) { 386c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner Value *ActualArg = *AI; 387a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 388d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands // When byval arguments actually inlined, we need to make the copy implied 389d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands // by them explicit. However, we don't do this if the callee is readonly 390d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands // or readnone, because the copy would be unneeded: the callee doesn't 391d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands // modify the struct. 392e7ae705c32906979a527926864345016e76867b9Chris Lattner if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal)) { 393e7ae705c32906979a527926864345016e76867b9Chris Lattner ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI, 394e7ae705c32906979a527926864345016e76867b9Chris Lattner CalledFunc->getParamAlignment(ArgNo+1)); 395e7ae705c32906979a527926864345016e76867b9Chris Lattner 3962914ba6ec793e2bb0e9ca5891af1d29ee2fee28eDuncan Sands // Calls that we inline may use the new alloca, so we need to clear 397e7ae705c32906979a527926864345016e76867b9Chris Lattner // their 'tail' flags if HandleByValArgument introduced a new alloca and 398e7ae705c32906979a527926864345016e76867b9Chris Lattner // the callee has calls. 399e7ae705c32906979a527926864345016e76867b9Chris Lattner MustClearTailCallFlags |= ActualArg != *AI; 400c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner } 401a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 40229d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel VMap[I] = ActualArg; 403c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner } 404fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4055b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // We want the inliner to prune the code as it copies. We would LOVE to 4065b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // have no dead or constant instructions leftover after inlining occurs 4075b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // (which can happen, e.g., because an argument was constant), but we'll be 4085b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // happy with whatever the cloner can do. 4096cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman CloneAndPruneFunctionInto(Caller, CalledFunc, VMap, 4106cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman /*ModuleLevelChanges=*/false, Returns, ".i", 41160915146f4d35e12f10dcdaa155596fac79184daChris Lattner &InlinedFunctionInfo, IFI.TD, TheCall); 412a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 413d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Remember the first block that is newly cloned over. 414d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner FirstNewBlock = LastBlock; ++FirstNewBlock; 415a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 416d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Update the callgraph if requested. 41760915146f4d35e12f10dcdaa155596fac79184daChris Lattner if (IFI.CG) 41829d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI); 419fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman } 420a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 421ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // If there are any alloca instructions in the block that used to be the entry 422ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // block for the callee, move them to the entry block of the caller. First 423ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // calculate which instruction they should be inserted before. We insert the 424ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // instructions at the end of the current alloca list. 425ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 42621f20558d629f7ff8f64c20746d890d29328a544Chris Lattner { 42780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock::iterator InsertPoint = Caller->begin()->begin(); 4285e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner for (BasicBlock::iterator I = FirstNewBlock->begin(), 429135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner E = FirstNewBlock->end(); I != E; ) { 430135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner AllocaInst *AI = dyn_cast<AllocaInst>(I++); 431135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (AI == 0) continue; 432135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 433135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If the alloca is now dead, remove it. This often occurs due to code 434135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // specialization. 435135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (AI->use_empty()) { 436135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner AI->eraseFromParent(); 437135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 43833bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner } 439135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 440135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!isa<Constant>(AI->getArraySize())) 441135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 442135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 44339add23dc54a1580983b1901384688d6622daa3bChris Lattner // Keep track of the static allocas that we inline into the caller. 44460915146f4d35e12f10dcdaa155596fac79184daChris Lattner IFI.StaticAllocas.push_back(AI); 4458f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner 446135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Scan for the block of allocas that we can move over, and move them 447135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // all at once. 448135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner while (isa<AllocaInst>(I) && 4498f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner isa<Constant>(cast<AllocaInst>(I)->getArraySize())) { 45060915146f4d35e12f10dcdaa155596fac79184daChris Lattner IFI.StaticAllocas.push_back(cast<AllocaInst>(I)); 451135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner ++I; 4528f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner } 453135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 454135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Transfer all of the allocas over in a block. Using splice means 455135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // that the instructions aren't removed from the symbol table, then 456135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // reinserted. 457135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Caller->getEntryBlock().getInstList().splice(InsertPoint, 458135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner FirstNewBlock->getInstList(), 459135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner AI, I); 460135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 46180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } 46280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 463bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // If the inlined code contained dynamic alloca instructions, wrap the inlined 464bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // code with llvm.stacksave/llvm.stackrestore intrinsics. 465bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner if (InlinedFunctionInfo.ContainsDynamicAllocas) { 466bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner Module *M = Caller->getParent(); 467bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Get the two intrinsics we care about. 4686128df525501c333a650d097703c18d7e878f5e8Chris Lattner Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave); 4696128df525501c333a650d097703c18d7e878f5e8Chris Lattner Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore); 470d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 471d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // If we are preserving the callgraph, add edges to the stacksave/restore 472d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // functions for the calls we insert. 47321ba23d0d88cff57dcdf104cd6914dabcbc68131Chris Lattner CallGraphNode *StackSaveCGN = 0, *StackRestoreCGN = 0, *CallerNode = 0; 47460915146f4d35e12f10dcdaa155596fac79184daChris Lattner if (CallGraph *CG = IFI.CG) { 4756128df525501c333a650d097703c18d7e878f5e8Chris Lattner StackSaveCGN = CG->getOrInsertFunction(StackSave); 4766128df525501c333a650d097703c18d7e878f5e8Chris Lattner StackRestoreCGN = CG->getOrInsertFunction(StackRestore); 477d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner CallerNode = (*CG)[Caller]; 478d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 479a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 480bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Insert the llvm.stacksave. 481a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands CallInst *SavedPtr = CallInst::Create(StackSave, "savedstack", 482051a950000e21935165db56695e35bade668193bGabor Greif FirstNewBlock->begin()); 48360915146f4d35e12f10dcdaa155596fac79184daChris Lattner if (IFI.CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN); 484a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 485bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Insert a call to llvm.stackrestore before any return instructions in the 486bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // inlined function. 487d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 488051a950000e21935165db56695e35bade668193bGabor Greif CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", Returns[i]); 48960915146f4d35e12f10dcdaa155596fac79184daChris Lattner if (IFI.CG) CallerNode->addCalledFunction(CI, StackRestoreCGN); 490d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 491468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner 492468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner // Count the number of StackRestore calls we insert. 493468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner unsigned NumStackRestores = Returns.size(); 494a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 495bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // If we are inlining an invoke instruction, insert restores before each 496bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // unwind. These unwinds will be rewritten into branches later. 497bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) { 498bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner for (Function::iterator BB = FirstNewBlock, E = Caller->end(); 499bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner BB != E; ++BB) 500468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { 5016128df525501c333a650d097703c18d7e878f5e8Chris Lattner CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", UI); 50260915146f4d35e12f10dcdaa155596fac79184daChris Lattner if (IFI.CG) CallerNode->addCalledFunction(CI, StackRestoreCGN); 503468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner ++NumStackRestores; 504468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner } 505468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner } 506bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner } 507bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner 508a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands // If we are inlining tail call instruction through a call site that isn't 5091fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner // marked 'tail', we must remove the tail marker for any calls in the inlined 510f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // code. Also, calls inlined through a 'nounwind' call site should be marked 511f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // 'nounwind'. 512f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (InlinedFunctionInfo.ContainsCalls && 513f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands (MustClearTailCallFlags || MarkNoUnwind)) { 5141b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner for (Function::iterator BB = FirstNewBlock, E = Caller->end(); 5151b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner BB != E; ++BB) 5161b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 517f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (CallInst *CI = dyn_cast<CallInst>(I)) { 518f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (MustClearTailCallFlags) 519f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands CI->setTailCall(false); 520f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (MarkNoUnwind) 521f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands CI->setDoesNotThrow(); 522f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands } 5231b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner } 5241b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner 525f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // If we are inlining through a 'nounwind' call site then any inlined 'unwind' 526f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // instructions are unreachable. 527f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind) 528f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands for (Function::iterator BB = FirstNewBlock, E = Caller->end(); 529f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands BB != E; ++BB) { 530f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands TerminatorInst *Term = BB->getTerminator(); 531f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (isa<UnwindInst>(Term)) { 5321d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson new UnreachableInst(Context, Term); 533f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands BB->getInstList().erase(Term); 534f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands } 535f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands } 536f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands 5375e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // If we are inlining for an invoke instruction, we must make sure to rewrite 5385e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // any inlined 'unwind' instructions into branches to the invoke exception 5395e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // destination, and call instructions into invoke instructions. 540cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) 54181dfb3885252fbf621b080827a080099864415f8Chris Lattner HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo); 5425e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner 54344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If we cloned in _exactly one_ basic block, and if that block ends in a 54444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // return instruction, we splice the body of the inlined callee directly into 54544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // the calling basic block. 54644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) { 54744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Move all of the instructions right before the call. 54844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(), 54944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner FirstNewBlock->begin(), FirstNewBlock->end()); 55044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Remove the cloned basic block. 55144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Caller->getBasicBlockList().pop_back(); 552fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 55344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If the call site was an invoke instruction, add a branch to the normal 55444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // destination. 55544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) 556051a950000e21935165db56695e35bade668193bGabor Greif BranchInst::Create(II->getNormalDest(), TheCall); 55744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 55844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If the return instruction returned a value, replace uses of the call with 55944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // uses of the returned value. 560dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel if (!TheCall->use_empty()) { 561dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel ReturnInst *R = Returns[0]; 5625877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman if (TheCall == R->getReturnValue()) 5639e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); 5645877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman else 5655877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman TheCall->replaceAllUsesWith(R->getReturnValue()); 566dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel } 56744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Since we are now done with the Call/Invoke, we can delete it. 5681adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman TheCall->eraseFromParent(); 56944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 57044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Since we are now done with the return instruction, delete it also. 5711adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman Returns[0]->eraseFromParent(); 57244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 57344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // We are now done with the inlining. 57444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner return true; 57544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner } 57644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 57744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Otherwise, we have the normal case, of more than one block to inline or 57844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // multiple return sites. 57944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 5805e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // We want to clone the entire callee function into the hole between the 5815e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // "starter" and "ender" blocks. How we accomplish this depends on whether 5825e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // this is an invoke instruction or a call instruction. 5835e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner BasicBlock *AfterCallBB; 5845e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { 585fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 5865e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Add an unconditional branch to make this look like the CallInst case... 587051a950000e21935165db56695e35bade668193bGabor Greif BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall); 588fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 5895e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Split the basic block. This guarantees that no PHI nodes will have to be 5905e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // updated due to new incoming edges, and make the invoke case more 5915e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // symmetric to the call case. 5925e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner AfterCallBB = OrigBB->splitBasicBlock(NewBr, 593284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner CalledFunc->getName()+".exit"); 594fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 5955e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } else { // It's a call 59644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If this is a call instruction, we need to split the basic block that 59744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // the call lives in. 5985e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // 5995e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner AfterCallBB = OrigBB->splitBasicBlock(TheCall, 600284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner CalledFunc->getName()+".exit"); 6015e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 6025e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner 60344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Change the branch that used to go to AfterCallBB to branch to the first 60444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // basic block of the inlined function. 60544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // 60644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner TerminatorInst *Br = OrigBB->getTerminator(); 607fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman assert(Br && Br->getOpcode() == Instruction::Br && 60844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner "splitBasicBlock broken!"); 60944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Br->setOperand(0, FirstNewBlock); 61044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 61144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 61244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Now that the function is correct, make it a little bit nicer. In 61344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // particular, move the basic blocks inserted from the end of the function 61444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // into the space made by splitting the source basic block. 61544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), 61644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner FirstNewBlock, Caller->end()); 61744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 6185e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Handle all of the return instructions that we just cloned in, and eliminate 6195e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // any users of the original call/invoke instruction. 620b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel const Type *RTy = CalledFunc->getReturnType(); 6212c31750cd0ebdc83a890ace97dbb6249b3abe44eDan Gohman 6226fb881c036c32728c4a128d81b6083457e534e09Duncan Sands PHINode *PHI = 0; 623fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman if (Returns.size() > 1) { 6245e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // The PHI node should go at the front of the new basic block to merge all 6255e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // possible incoming values. 6265e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (!TheCall->use_empty()) { 6273ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad PHI = PHINode::Create(RTy, Returns.size(), TheCall->getName(), 628fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman AfterCallBB->begin()); 629fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman // Anything that used the result of the function call should now use the 630fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman // PHI node as their operand. 631a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands TheCall->replaceAllUsesWith(PHI); 6325e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 633fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 634c478e52bf4c12691037856ee103c66946afeab6cGabor Greif // Loop over all of the return instructions adding entries to the PHI node 635c478e52bf4c12691037856ee103c66946afeab6cGabor Greif // as appropriate. 636fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman if (PHI) { 637fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 638fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman ReturnInst *RI = Returns[i]; 639fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman assert(RI->getReturnValue()->getType() == PHI->getType() && 640fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman "Ret value not consistent in function!"); 641fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman PHI->addIncoming(RI->getReturnValue(), RI->getParent()); 6425e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 64312a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel } 644fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 645c581acbbba3cb1af6a08e17314b26344333f9267Chris Lattner 646de62aeaec49ddcf4a4c61fbbb3a22d3a4dd448f0Gabor Greif // Add a branch to the merge points and remove return instructions. 64712a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 64812a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel ReturnInst *RI = Returns[i]; 6490744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen BranchInst::Create(AfterCallBB, RI); 650b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel RI->eraseFromParent(); 6515e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 652b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel } else if (!Returns.empty()) { 653b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // Otherwise, if there is exactly one return value, just replace anything 654b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // using the return value of the call with the computed value. 6555877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman if (!TheCall->use_empty()) { 6565877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman if (TheCall == Returns[0]->getReturnValue()) 6579e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); 6585877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman else 6595877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); 6605877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman } 661a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 662b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // Splice the code from the return block into the block that it will return 663b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // to, which contains the code that was after the call. 664b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel BasicBlock *ReturnBB = Returns[0]->getParent(); 665b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel AfterCallBB->getInstList().splice(AfterCallBB->begin(), 666b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel ReturnBB->getInstList()); 667a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 668b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // Update PHI nodes that use the ReturnBB to use the AfterCallBB. 669b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel ReturnBB->replaceAllUsesWith(AfterCallBB); 670a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 671b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // Delete the return instruction now and empty ReturnBB now. 672b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel Returns[0]->eraseFromParent(); 673b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel ReturnBB->eraseFromParent(); 6743787e765facfad5ea62753922d940bcdd52afd57Chris Lattner } else if (!TheCall->use_empty()) { 6753787e765facfad5ea62753922d940bcdd52afd57Chris Lattner // No returns, but something is using the return value of the call. Just 6763787e765facfad5ea62753922d940bcdd52afd57Chris Lattner // nuke the result. 6779e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); 6785e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 679fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 6805e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Since we are now done with the Call/Invoke, we can delete it. 6813787e765facfad5ea62753922d940bcdd52afd57Chris Lattner TheCall->eraseFromParent(); 682ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 6837152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // We should always be able to fold the entry block of the function into the 6847152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // single predecessor of the block... 685cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); 6867152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); 68744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 688cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Splice the code entry block into calling block, right before the 689cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // unconditional branch. 690cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); 691cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes 692cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner 693cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Remove the unconditional branch. 694cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner OrigBB->getInstList().erase(Br); 695cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner 696cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Now we can remove the CalleeEntry block, which is now empty. 697cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner Caller->getBasicBlockList().erase(CalleeEntry); 698a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 6996fb881c036c32728c4a128d81b6083457e534e09Duncan Sands // If we inserted a phi node, check to see if it has a single value (e.g. all 7006fb881c036c32728c4a128d81b6083457e534e09Duncan Sands // the entries are the same or undef). If so, remove the PHI so it doesn't 7016fb881c036c32728c4a128d81b6083457e534e09Duncan Sands // block other optimizations. 7026fb881c036c32728c4a128d81b6083457e534e09Duncan Sands if (PHI) 7036fb881c036c32728c4a128d81b6083457e534e09Duncan Sands if (Value *V = SimplifyInstruction(PHI, IFI.TD)) { 7046fb881c036c32728c4a128d81b6083457e534e09Duncan Sands PHI->replaceAllUsesWith(V); 7056fb881c036c32728c4a128d81b6083457e534e09Duncan Sands PHI->eraseFromParent(); 7066fb881c036c32728c4a128d81b6083457e534e09Duncan Sands } 7076fb881c036c32728c4a128d81b6083457e534e09Duncan Sands 708ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner return true; 709ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner} 710