InlineFunction.cpp revision f48509787acfcfc3f9eee2fb3084c2e8c7b4a009
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" 16d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallVector.h" 17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/StringExtras.h" 18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/CallGraph.h" 19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/InstructionSimplify.h" 200bcbd1df7a204e1e512f1a27066d725309de1b13Bill Wendling#include "llvm/DebugInfo.h" 210b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Attributes.h" 220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h" 230b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h" 240b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h" 250b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IRBuilder.h" 260b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instructions.h" 270b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h" 280b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Intrinsics.h" 290b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h" 3006cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth#include "llvm/Support/CallSite.h" 317569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner#include "llvm/Transforms/Utils/Local.h" 32f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm; 33ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 34373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopherbool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI, 35373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopher bool InsertLifetime) { 36fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier return InlineFunction(CallSite(CI), IFI, InsertLifetime); 37468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 38373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopherbool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI, 39373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopher bool InsertLifetime) { 40fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier return InlineFunction(CallSite(II), IFI, InsertLifetime); 41468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 4280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 43a3de16bc8f36638d5444e3e7b0112998af54f826John McCallnamespace { 44a3de16bc8f36638d5444e3e7b0112998af54f826John McCall /// A class for recording information about inlining through an invoke. 45a3de16bc8f36638d5444e3e7b0112998af54f826John McCall class InvokeInliningInfo { 4677592fe39c404f3c48b06fae48b965058b3a5ee8Dmitri Gribenko BasicBlock *OuterResumeDest; ///< Destination of the invoke's unwind. 4777592fe39c404f3c48b06fae48b965058b3a5ee8Dmitri Gribenko BasicBlock *InnerResumeDest; ///< Destination for the callee's resume. 4877592fe39c404f3c48b06fae48b965058b3a5ee8Dmitri Gribenko LandingPadInst *CallerLPad; ///< LandingPadInst associated with the invoke. 4977592fe39c404f3c48b06fae48b965058b3a5ee8Dmitri Gribenko PHINode *InnerEHValuesPHI; ///< PHI for EH values from landingpad insts. 504dbd9b8ebfddb845c5675bbf2567a4d0e04871e7Bill Wendling SmallVector<Value*, 8> UnwindDestPHIValues; 51fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 522bf84c15d24bb373987d9dbc6308092eac1b8324Bill Wendling public: 53fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling InvokeInliningInfo(InvokeInst *II) 5408d01462d13fdfac756a8bd0f38bbfbceb247403Bill Wendling : OuterResumeDest(II->getUnwindDest()), InnerResumeDest(0), 55fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling CallerLPad(0), InnerEHValuesPHI(0) { 56fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // If there are PHI nodes in the unwind destination block, we need to keep 57fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // track of which values came into them from the invoke before removing 58fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // the edge from this block. 59fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling llvm::BasicBlock *InvokeBB = II->getParent(); 6008d01462d13fdfac756a8bd0f38bbfbceb247403Bill Wendling BasicBlock::iterator I = OuterResumeDest->begin(); 61fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling for (; isa<PHINode>(I); ++I) { 62a3de16bc8f36638d5444e3e7b0112998af54f826John McCall // Save the value to use for this edge. 63fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling PHINode *PHI = cast<PHINode>(I); 64fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling UnwindDestPHIValues.push_back(PHI->getIncomingValueForBlock(InvokeBB)); 65fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling } 66fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 6727b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling CallerLPad = cast<LandingPadInst>(I); 68a3de16bc8f36638d5444e3e7b0112998af54f826John McCall } 69a3de16bc8f36638d5444e3e7b0112998af54f826John McCall 7008d01462d13fdfac756a8bd0f38bbfbceb247403Bill Wendling /// getOuterResumeDest - The outer unwind destination is the target of 7108d01462d13fdfac756a8bd0f38bbfbceb247403Bill Wendling /// unwind edges introduced for calls within the inlined function. 724dbd9b8ebfddb845c5675bbf2567a4d0e04871e7Bill Wendling BasicBlock *getOuterResumeDest() const { 7308d01462d13fdfac756a8bd0f38bbfbceb247403Bill Wendling return OuterResumeDest; 74a3de16bc8f36638d5444e3e7b0112998af54f826John McCall } 75a3de16bc8f36638d5444e3e7b0112998af54f826John McCall 7613b1c31412372ef3790934ca213546fec595fbbcBill Wendling BasicBlock *getInnerResumeDest(); 77fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 78fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling LandingPadInst *getLandingPadInst() const { return CallerLPad; } 79fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 80fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling /// forwardResume - Forward the 'resume' instruction to the caller's landing 81fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling /// pad block. When the landing pad block has only one predecessor, this is 82fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling /// a simple branch. When there is more than one predecessor, we need to 83fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling /// split the landing pad block after the landingpad instruction and jump 84fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling /// to there. 854c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling void forwardResume(ResumeInst *RI, 864c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling SmallPtrSet<LandingPadInst*, 16> &InlinedLPads); 87fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 88fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling /// addIncomingPHIValuesFor - Add incoming-PHI values to the unwind 89fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling /// destination block for the given basic block, using the values for the 90fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling /// original invoke's source block. 91a3de16bc8f36638d5444e3e7b0112998af54f826John McCall void addIncomingPHIValuesFor(BasicBlock *BB) const { 9208d01462d13fdfac756a8bd0f38bbfbceb247403Bill Wendling addIncomingPHIValuesForInto(BB, OuterResumeDest); 93d7c10862016939c9850cadfe5e1c35513c0adf28John McCall } 9410c6d12a9fd4dab411091f64db4db69670b88850Bill Wendling 95d7c10862016939c9850cadfe5e1c35513c0adf28John McCall void addIncomingPHIValuesForInto(BasicBlock *src, BasicBlock *dest) const { 96d7c10862016939c9850cadfe5e1c35513c0adf28John McCall BasicBlock::iterator I = dest->begin(); 97a3de16bc8f36638d5444e3e7b0112998af54f826John McCall for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) { 9810c6d12a9fd4dab411091f64db4db69670b88850Bill Wendling PHINode *phi = cast<PHINode>(I); 9910c6d12a9fd4dab411091f64db4db69670b88850Bill Wendling phi->addIncoming(UnwindDestPHIValues[i], src); 100a3de16bc8f36638d5444e3e7b0112998af54f826John McCall } 101a3de16bc8f36638d5444e3e7b0112998af54f826John McCall } 102a3de16bc8f36638d5444e3e7b0112998af54f826John McCall }; 103a3de16bc8f36638d5444e3e7b0112998af54f826John McCall} 104a3de16bc8f36638d5444e3e7b0112998af54f826John McCall 10513b1c31412372ef3790934ca213546fec595fbbcBill Wendling/// getInnerResumeDest - Get or create a target for the branch from ResumeInsts. 10613b1c31412372ef3790934ca213546fec595fbbcBill WendlingBasicBlock *InvokeInliningInfo::getInnerResumeDest() { 107fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling if (InnerResumeDest) return InnerResumeDest; 108fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 109fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // Split the landing pad. 110fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling BasicBlock::iterator SplitPoint = CallerLPad; ++SplitPoint; 111fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling InnerResumeDest = 112fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling OuterResumeDest->splitBasicBlock(SplitPoint, 113fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling OuterResumeDest->getName() + ".body"); 114fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 115fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // The number of incoming edges we expect to the inner landing pad. 116fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling const unsigned PHICapacity = 2; 117fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 118fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // Create corresponding new PHIs for all the PHIs in the outer landing pad. 119fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling BasicBlock::iterator InsertPoint = InnerResumeDest->begin(); 120fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling BasicBlock::iterator I = OuterResumeDest->begin(); 121fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) { 122fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling PHINode *OuterPHI = cast<PHINode>(I); 123fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling PHINode *InnerPHI = PHINode::Create(OuterPHI->getType(), PHICapacity, 124fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling OuterPHI->getName() + ".lpad-body", 125fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling InsertPoint); 126fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling OuterPHI->replaceAllUsesWith(InnerPHI); 127fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling InnerPHI->addIncoming(OuterPHI, OuterResumeDest); 128fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling } 129fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 130fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // Create a PHI for the exception values. 131fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling InnerEHValuesPHI = PHINode::Create(CallerLPad->getType(), PHICapacity, 132fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling "eh.lpad-body", InsertPoint); 133fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling CallerLPad->replaceAllUsesWith(InnerEHValuesPHI); 134fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling InnerEHValuesPHI->addIncoming(CallerLPad, OuterResumeDest); 135fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 136fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // All done. 137fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling return InnerResumeDest; 138fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling} 139fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 140fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// forwardResume - Forward the 'resume' instruction to the caller's landing pad 141fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// block. When the landing pad block has only one predecessor, this is a simple 142fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// branch. When there is more than one predecessor, we need to split the 143fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// landing pad block after the landingpad instruction and jump to there. 144d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendlingvoid InvokeInliningInfo::forwardResume(ResumeInst *RI, 1454c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling SmallPtrSet<LandingPadInst*, 16> &InlinedLPads) { 14613b1c31412372ef3790934ca213546fec595fbbcBill Wendling BasicBlock *Dest = getInnerResumeDest(); 147d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling LandingPadInst *OuterLPad = getLandingPadInst(); 148fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling BasicBlock *Src = RI->getParent(); 149fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 150fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling BranchInst::Create(Dest, Src); 151fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 152fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // Update the PHIs in the destination. They were inserted in an order which 153fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling // makes this work. 154fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling addIncomingPHIValuesForInto(Src, Dest); 155fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 156fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling InnerEHValuesPHI->addIncoming(RI->getOperand(0), Src); 157fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling RI->eraseFromParent(); 158d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling 1594c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling // Append the clauses from the outer landing pad instruction into the inlined 1604c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling // landing pad instructions. 161d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling for (SmallPtrSet<LandingPadInst*, 16>::iterator I = InlinedLPads.begin(), 162d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling E = InlinedLPads.end(); I != E; ++I) { 163d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling LandingPadInst *InlinedLPad = *I; 164d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling for (unsigned OuterIdx = 0, OuterNum = OuterLPad->getNumClauses(); 1654c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling OuterIdx != OuterNum; ++OuterIdx) 1664c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling InlinedLPad->addClause(OuterLPad->getClause(OuterIdx)); 167d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling } 168fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling} 169fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 170135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into 171f61f89ae14cf332a014a598153137113af34002fEric Christopher/// an invoke, we have to turn all of the calls that can throw into 172135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// invokes. This function analyze BB to see if there are any calls, and if so, 173135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI 17481dfb3885252fbf621b080827a080099864415f8Chris Lattner/// nodes in that block with the values specified in InvokeDestPHIValues. 175135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// 176a3de16bc8f36638d5444e3e7b0112998af54f826John McCall/// Returns true to indicate that the next block should be skipped. 177a3de16bc8f36638d5444e3e7b0112998af54f826John McCallstatic bool HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB, 178a3de16bc8f36638d5444e3e7b0112998af54f826John McCall InvokeInliningInfo &Invoke) { 179fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling LandingPadInst *LPI = Invoke.getLandingPadInst(); 180fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 181135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { 182135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Instruction *I = BBI++; 183fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 18427b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling if (LandingPadInst *L = dyn_cast<LandingPadInst>(I)) { 18527b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling unsigned NumClauses = LPI->getNumClauses(); 18627b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling L->reserveClauses(NumClauses); 18727b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling for (unsigned i = 0; i != NumClauses; ++i) 18827b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling L->addClause(LPI->getClause(i)); 18927b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling } 190fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 191135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // We only need to check for function calls: inlined invoke 192135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // instructions require no special handling. 193135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallInst *CI = dyn_cast<CallInst>(I); 194675f63886944d72e05e5210c36838c797364a0aaBill Wendling 195135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If this call cannot unwind, don't convert it to an invoke. 196675f63886944d72e05e5210c36838c797364a0aaBill Wendling if (!CI || CI->doesNotThrow()) 197135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 198675f63886944d72e05e5210c36838c797364a0aaBill Wendling 199675f63886944d72e05e5210c36838c797364a0aaBill Wendling // Convert this function call into an invoke instruction. First, split the 200675f63886944d72e05e5210c36838c797364a0aaBill Wendling // basic block. 201135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); 202a3de16bc8f36638d5444e3e7b0112998af54f826John McCall 203d7c10862016939c9850cadfe5e1c35513c0adf28John McCall // Delete the unconditional branch inserted by splitBasicBlock 204d7c10862016939c9850cadfe5e1c35513c0adf28John McCall BB->getInstList().pop_back(); 205a3de16bc8f36638d5444e3e7b0112998af54f826John McCall 2069e9a34c5688500eee47d6a7800c6e9ef93b90684Bill Wendling // Create the new invoke instruction. 207d7c10862016939c9850cadfe5e1c35513c0adf28John McCall ImmutableCallSite CS(CI); 208d7c10862016939c9850cadfe5e1c35513c0adf28John McCall SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end()); 20906881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling InvokeInst *II = InvokeInst::Create(CI->getCalledValue(), Split, 2104dbd9b8ebfddb845c5675bbf2567a4d0e04871e7Bill Wendling Invoke.getOuterResumeDest(), 21106881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling InvokeArgs, CI->getName(), BB); 212d7c10862016939c9850cadfe5e1c35513c0adf28John McCall II->setCallingConv(CI->getCallingConv()); 213d7c10862016939c9850cadfe5e1c35513c0adf28John McCall II->setAttributes(CI->getAttributes()); 214135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 215d7c10862016939c9850cadfe5e1c35513c0adf28John McCall // Make sure that anything using the call now uses the invoke! This also 216d7c10862016939c9850cadfe5e1c35513c0adf28John McCall // updates the CallGraph if present, because it uses a WeakVH. 217d7c10862016939c9850cadfe5e1c35513c0adf28John McCall CI->replaceAllUsesWith(II); 218d7c10862016939c9850cadfe5e1c35513c0adf28John McCall 21906881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling // Delete the original call 22006881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling Split->getInstList().pop_front(); 221a3de16bc8f36638d5444e3e7b0112998af54f826John McCall 22206881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling // Update any PHI nodes in the exceptional block to indicate that there is 22306881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling // now a new entry in them. 224a3de16bc8f36638d5444e3e7b0112998af54f826John McCall Invoke.addIncomingPHIValuesFor(BB); 225d7c10862016939c9850cadfe5e1c35513c0adf28John McCall return false; 226135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 227a3de16bc8f36638d5444e3e7b0112998af54f826John McCall 228a3de16bc8f36638d5444e3e7b0112998af54f826John McCall return false; 229135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner} 230135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 231cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls 2328833ef03b9ceaa52063116819fff8b3d16fd8933Bill Wendling/// in the body of the inlined function into invokes. 233cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// 234dac5c4b10b387b55c2394cd98a64f3f1394df2e8Nick Lewycky/// II is the invoke instruction being inlined. FirstNewBlock is the first 235cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// block of the inlined code (the last block is the end of the function), 236cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// and InlineCodeInfo is information about the code that got inlined. 237cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattnerstatic void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, 23881dfb3885252fbf621b080827a080099864415f8Chris Lattner ClonedCodeInfo &InlinedCodeInfo) { 239cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner BasicBlock *InvokeDest = II->getUnwindDest(); 240cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 241cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner Function *Caller = FirstNewBlock->getParent(); 242a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 243cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // The inlined code is currently at the end of the function, scan from the 244cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // start of the inlined code to its end, checking for stuff we need to 245d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling // rewrite. 246a3de16bc8f36638d5444e3e7b0112998af54f826John McCall InvokeInliningInfo Invoke(II); 247d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling 2484c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling // Get all of the inlined landing pad instructions. 2494c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling SmallPtrSet<LandingPadInst*, 16> InlinedLPads; 2504c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling for (Function::iterator I = FirstNewBlock, E = Caller->end(); I != E; ++I) 2514c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) 2524c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling InlinedLPads.insert(II->getLandingPadInst()); 2534c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling 254135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){ 255135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (InlinedCodeInfo.ContainsCalls) 256a3de16bc8f36638d5444e3e7b0112998af54f826John McCall if (HandleCallsInBlockInlinedThroughInvoke(BB, Invoke)) { 2578833ef03b9ceaa52063116819fff8b3d16fd8933Bill Wendling // Honor a request to skip the next block. 258a3de16bc8f36638d5444e3e7b0112998af54f826John McCall ++BB; 259a3de16bc8f36638d5444e3e7b0112998af54f826John McCall continue; 260a3de16bc8f36638d5444e3e7b0112998af54f826John McCall } 261135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 262d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling // Forward any resumes that are remaining here. 2639e9a34c5688500eee47d6a7800c6e9ef93b90684Bill Wendling if (ResumeInst *RI = dyn_cast<ResumeInst>(BB->getTerminator())) 2644c6250247053810d44119d2e34eb4f07ba56d035Bill Wendling Invoke.forwardResume(RI, InlinedLPads); 265cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner } 266cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 267cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // Now that everything is happy, we have one final detail. The PHI nodes in 268cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // the exception destination block still have entries due to the original 269d9ff8c83d137586d8c06f98bdf8adbf0d1fa79caBill Wendling // invoke instruction. Eliminate these entries (which might even delete the 270cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner // PHI node) now. 271cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner InvokeDest->removePredecessor(II->getParent()); 272cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner} 273cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner 274d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee 275d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// into the caller, update the specified callgraph to reflect the changes we 276d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// made. Note that it's possible that not all code was copied over, so only 277d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands/// some edges of the callgraph may remain. 278d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sandsstatic void UpdateCallGraphAfterInlining(CallSite CS, 279d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner Function::iterator FirstNewBlock, 2801ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola ValueToValueMapTy &VMap, 281fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner InlineFunctionInfo &IFI) { 282fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner CallGraph &CG = *IFI.CG; 283d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands const Function *Caller = CS.getInstruction()->getParent()->getParent(); 284d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands const Function *Callee = CS.getCalledFunction(); 285468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner CallGraphNode *CalleeNode = CG[Callee]; 286468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner CallGraphNode *CallerNode = CG[Caller]; 287a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 288d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Since we inlined some uninlined call sites in the callee into the caller, 289468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner // add edges from the caller to all of the callees of the callee. 290c478e52bf4c12691037856ee103c66946afeab6cGabor Greif CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end(); 291c478e52bf4c12691037856ee103c66946afeab6cGabor Greif 292c478e52bf4c12691037856ee103c66946afeab6cGabor Greif // Consider the case where CalleeNode == CallerNode. 293125329891f97baedef21e4b464ba70182c3fb45eGabor Greif CallGraphNode::CalledFunctionsVector CallCache; 294c478e52bf4c12691037856ee103c66946afeab6cGabor Greif if (CalleeNode == CallerNode) { 295c478e52bf4c12691037856ee103c66946afeab6cGabor Greif CallCache.assign(I, E); 296c478e52bf4c12691037856ee103c66946afeab6cGabor Greif I = CallCache.begin(); 297c478e52bf4c12691037856ee103c66946afeab6cGabor Greif E = CallCache.end(); 298c478e52bf4c12691037856ee103c66946afeab6cGabor Greif } 299c478e52bf4c12691037856ee103c66946afeab6cGabor Greif 300c478e52bf4c12691037856ee103c66946afeab6cGabor Greif for (; I != E; ++I) { 301a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner const Value *OrigCall = I->first; 302a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 3031ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola ValueToValueMapTy::iterator VMI = VMap.find(OrigCall); 304981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner // Only copy the edge if the call was inlined! 30529d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel if (VMI == VMap.end() || VMI->second == 0) 306135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 307135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 308135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If the call was inlined, but then constant folded, there is no edge to 309135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // add. Check for this case. 310b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner Instruction *NewCall = dyn_cast<Instruction>(VMI->second); 311b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner if (NewCall == 0) continue; 3120ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner 3130ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner // Remember that this call site got inlined for the client of 3140ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner // InlineFunction. 3150ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner IFI.InlinedCalls.push_back(NewCall); 3160ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner 317b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // It's possible that inlining the callsite will cause it to go from an 318b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // indirect to a direct call by resolving a function pointer. If this 319b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // happens, set the callee of the new call site to a more precise 320b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // destination. This can also happen if the call graph node of the caller 321b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // was just unnecessarily imprecise. 322b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner if (I->second->getFunction() == 0) 323b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner if (Function *F = CallSite(NewCall).getCalledFunction()) { 324b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner // Indirect call site resolved to direct call. 32586099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif CallerNode->addCalledFunction(CallSite(NewCall), CG[F]); 32686099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif 327b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner continue; 328b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner } 32986099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif 33086099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif CallerNode->addCalledFunction(CallSite(NewCall), I->second); 331d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 332135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 33339fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen // Update the call graph by deleting the edge from Callee to Caller. We must 33439fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen // do this after the loop above in case Caller and Callee are the same. 33539fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen CallerNode->removeCallEdgeFor(CS); 336468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner} 337468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner 3380b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// HandleByValArgument - When inlining a call site that has a byval argument, 3390b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// we have to make the implicit memcpy explicit by adding it. 340e7ae705c32906979a527926864345016e76867b9Chris Lattnerstatic Value *HandleByValArgument(Value *Arg, Instruction *TheCall, 341e7ae705c32906979a527926864345016e76867b9Chris Lattner const Function *CalledFunc, 342e7ae705c32906979a527926864345016e76867b9Chris Lattner InlineFunctionInfo &IFI, 343e7ae705c32906979a527926864345016e76867b9Chris Lattner unsigned ByValAlignment) { 344db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *AggTy = cast<PointerType>(Arg->getType())->getElementType(); 3450b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner 3460b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // If the called function is readonly, then it could not mutate the caller's 3470b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // copy of the byval'd memory. In this case, it is safe to elide the copy and 3480b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // temporary. 3490b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner if (CalledFunc->onlyReadsMemory()) { 3500b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // If the byval argument has a specified alignment that is greater than the 3510b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // passed in pointer, then we either have to round up the input pointer or 3520b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner // give up on this transformation. 3530b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner if (ByValAlignment <= 1) // 0 = unspecified, 1 = no particular alignment. 3540b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner return Arg; 3550b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner 3567569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner // If the pointer is already known to be sufficiently aligned, or if we can 3577569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner // round it up to a larger alignment, then we don't need a temporary. 3587569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner if (getOrEnforceKnownAlignment(Arg, ByValAlignment, 3597569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner IFI.TD) >= ByValAlignment) 3607569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner return Arg; 3610b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner 3627569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner // Otherwise, we have to make a memcpy to get a safe alignment. This is bad 3637569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner // for code quality, but rarely happens and is required for correctness. 3640b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner } 365e7ae705c32906979a527926864345016e76867b9Chris Lattner 366e7ae705c32906979a527926864345016e76867b9Chris Lattner LLVMContext &Context = Arg->getContext(); 367e7ae705c32906979a527926864345016e76867b9Chris Lattner 3685fdd6c8793462549e3593890ec61573da06e3346Jay Foad Type *VoidPtrTy = Type::getInt8PtrTy(Context); 369e7ae705c32906979a527926864345016e76867b9Chris Lattner 3703574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow // Create the alloca. If we have DataLayout, use nice alignment. 371e7ae705c32906979a527926864345016e76867b9Chris Lattner unsigned Align = 1; 372e7ae705c32906979a527926864345016e76867b9Chris Lattner if (IFI.TD) 373e7ae705c32906979a527926864345016e76867b9Chris Lattner Align = IFI.TD->getPrefTypeAlignment(AggTy); 374e7ae705c32906979a527926864345016e76867b9Chris Lattner 375e7ae705c32906979a527926864345016e76867b9Chris Lattner // If the byval had an alignment specified, we *must* use at least that 376e7ae705c32906979a527926864345016e76867b9Chris Lattner // alignment, as it is required by the byval argument (and uses of the 377e7ae705c32906979a527926864345016e76867b9Chris Lattner // pointer inside the callee). 378e7ae705c32906979a527926864345016e76867b9Chris Lattner Align = std::max(Align, ByValAlignment); 379e7ae705c32906979a527926864345016e76867b9Chris Lattner 380e7ae705c32906979a527926864345016e76867b9Chris Lattner Function *Caller = TheCall->getParent()->getParent(); 381e7ae705c32906979a527926864345016e76867b9Chris Lattner 382e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *NewAlloca = new AllocaInst(AggTy, 0, Align, Arg->getName(), 383e7ae705c32906979a527926864345016e76867b9Chris Lattner &*Caller->begin()->begin()); 384e7ae705c32906979a527926864345016e76867b9Chris Lattner // Emit a memcpy. 3855fdd6c8793462549e3593890ec61573da06e3346Jay Foad Type *Tys[3] = {VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context)}; 386e7ae705c32906979a527926864345016e76867b9Chris Lattner Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(), 387e7ae705c32906979a527926864345016e76867b9Chris Lattner Intrinsic::memcpy, 388eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin Kramer Tys); 389e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall); 390e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *SrcCast = new BitCastInst(Arg, VoidPtrTy, "tmp", TheCall); 391e7ae705c32906979a527926864345016e76867b9Chris Lattner 392e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *Size; 393e7ae705c32906979a527926864345016e76867b9Chris Lattner if (IFI.TD == 0) 394e7ae705c32906979a527926864345016e76867b9Chris Lattner Size = ConstantExpr::getSizeOf(AggTy); 395e7ae705c32906979a527926864345016e76867b9Chris Lattner else 396e7ae705c32906979a527926864345016e76867b9Chris Lattner Size = ConstantInt::get(Type::getInt64Ty(Context), 397e7ae705c32906979a527926864345016e76867b9Chris Lattner IFI.TD->getTypeStoreSize(AggTy)); 398e7ae705c32906979a527926864345016e76867b9Chris Lattner 399e7ae705c32906979a527926864345016e76867b9Chris Lattner // Always generate a memcpy of alignment 1 here because we don't know 400e7ae705c32906979a527926864345016e76867b9Chris Lattner // the alignment of the src pointer. Other optimizations can infer 401e7ae705c32906979a527926864345016e76867b9Chris Lattner // better alignment. 402e7ae705c32906979a527926864345016e76867b9Chris Lattner Value *CallArgs[] = { 403e7ae705c32906979a527926864345016e76867b9Chris Lattner DestCast, SrcCast, Size, 404e7ae705c32906979a527926864345016e76867b9Chris Lattner ConstantInt::get(Type::getInt32Ty(Context), 1), 405e7ae705c32906979a527926864345016e76867b9Chris Lattner ConstantInt::getFalse(Context) // isVolatile 406e7ae705c32906979a527926864345016e76867b9Chris Lattner }; 407a3efbb15ddd5aa9006564cd79086723640084878Jay Foad IRBuilder<>(TheCall).CreateCall(MemCpyFn, CallArgs); 408e7ae705c32906979a527926864345016e76867b9Chris Lattner 409e7ae705c32906979a527926864345016e76867b9Chris Lattner // Uses of the argument in the function should use our new alloca 410e7ae705c32906979a527926864345016e76867b9Chris Lattner // instead. 411e7ae705c32906979a527926864345016e76867b9Chris Lattner return NewAlloca; 412e7ae705c32906979a527926864345016e76867b9Chris Lattner} 413e7ae705c32906979a527926864345016e76867b9Chris Lattner 4146d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// isUsedByLifetimeMarker - Check whether this Value is used by a lifetime 4156d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// intrinsic. 4166d55f2269e20298a1d6a683be72d9552482156a9Nick Lewyckystatic bool isUsedByLifetimeMarker(Value *V) { 4176d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE; 4186d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky ++UI) { 4196d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*UI)) { 4206d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky switch (II->getIntrinsicID()) { 4216d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky default: break; 4226d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky case Intrinsic::lifetime_start: 4236d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky case Intrinsic::lifetime_end: 4246d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky return true; 4256d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky } 4266d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky } 4276d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky } 4286d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky return false; 4296d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky} 4306d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky 4316d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// hasLifetimeMarkers - Check whether the given alloca already has 4326d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// lifetime.start or lifetime.end intrinsics. 4336d55f2269e20298a1d6a683be72d9552482156a9Nick Lewyckystatic bool hasLifetimeMarkers(AllocaInst *AI) { 434db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Int8PtrTy = Type::getInt8PtrTy(AI->getType()->getContext()); 4356d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky if (AI->getType() == Int8PtrTy) 4366d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky return isUsedByLifetimeMarker(AI); 4376d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky 438708c1ac077fbc0cb73d489b4f4df3b2718566b05Nick Lewycky // Do a scan to find all the casts to i8*. 4396d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); I != E; 4406d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky ++I) { 4416d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky if (I->getType() != Int8PtrTy) continue; 442708c1ac077fbc0cb73d489b4f4df3b2718566b05Nick Lewycky if (I->stripPointerCasts() != AI) continue; 4436d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky if (isUsedByLifetimeMarker(*I)) 4446d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky return true; 4456d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky } 4466d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky return false; 4476d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky} 4486d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky 449373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopher/// updateInlinedAtInfo - Helper function used by fixupLineNumbers to 450373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopher/// recursively update InlinedAtEntry of a DebugLoc. 4512cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patelstatic DebugLoc updateInlinedAtInfo(const DebugLoc &DL, 4522cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel const DebugLoc &InlinedAtDL, 4532cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel LLVMContext &Ctx) { 4542cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel if (MDNode *IA = DL.getInlinedAt(Ctx)) { 4552cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel DebugLoc NewInlinedAtDL 4562cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel = updateInlinedAtInfo(DebugLoc::getFromDILocation(IA), InlinedAtDL, Ctx); 4572cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx), 4582cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel NewInlinedAtDL.getAsMDNode(Ctx)); 4592cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel } 460373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopher 4612cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx), 4622cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel InlinedAtDL.getAsMDNode(Ctx)); 4632cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel} 4642cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel 4652cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// fixupLineNumbers - Update inlined instructions' line numbers to 4662cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// to encode location where these instructions are inlined. 4672cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patelstatic void fixupLineNumbers(Function *Fn, Function::iterator FI, 4687d8eb711e4608dcca9366141be22941af8d1eff8Eric Christopher Instruction *TheCall) { 4692cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel DebugLoc TheCallDL = TheCall->getDebugLoc(); 4702cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel if (TheCallDL.isUnknown()) 4712cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel return; 4722cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel 4732cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel for (; FI != Fn->end(); ++FI) { 4742cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); 4752cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel BI != BE; ++BI) { 4762cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel DebugLoc DL = BI->getDebugLoc(); 477b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel if (!DL.isUnknown()) { 4782cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel BI->setDebugLoc(updateInlinedAtInfo(DL, TheCallDL, BI->getContext())); 479b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) { 480b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel LLVMContext &Ctx = BI->getContext(); 481b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel MDNode *InlinedAt = BI->getDebugLoc().getInlinedAt(Ctx); 482b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel DVI->setOperand(2, createInlinedVariable(DVI->getVariable(), 483b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel InlinedAt, Ctx)); 484b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel } 485b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel } 4862cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel } 4872cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel } 4882cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel} 4892cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel 49006881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// InlineFunction - This function inlines the called function into the basic 49106881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// block of the caller. This returns false if it is not possible to inline 49206881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// this call. The program is still in a well defined state if this occurs 49306881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// though. 49406881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// 49506881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// Note that this only does one level of inlining. For example, if the 49606881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now 49706881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// exists in the instruction stream. Similarly this will inline a recursive 49806881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// function by one level. 499373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopherbool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI, 500373c2d37072026e82f9b307eb40cf12baafd5f93Eric Christopher bool InsertLifetime) { 50180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner Instruction *TheCall = CS.getInstruction(); 50280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner assert(TheCall->getParent() && TheCall->getParent()->getParent() && 50380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner "Instruction not in function!"); 504ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 50560915146f4d35e12f10dcdaa155596fac79184daChris Lattner // If IFI has any state in it, zap it before we fill it in. 50660915146f4d35e12f10dcdaa155596fac79184daChris Lattner IFI.reset(); 50760915146f4d35e12f10dcdaa155596fac79184daChris Lattner 50880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner const Function *CalledFunc = CS.getCalledFunction(); 509ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner if (CalledFunc == 0 || // Can't inline external function or indirect 5105cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer CalledFunc->isDeclaration() || // call, or call to a vararg function! 5110623e90398153be61226ad19f1b40d3817874526Eric Christopher CalledFunc->getFunctionType()->isVarArg()) return false; 512ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 513af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner // If the call to the callee is not a tail call, we must clear the 'tail' 5141b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner // flags on any calls that we inline. 5151b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner bool MustClearTailCallFlags = 516af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall()); 5171b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner 518f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // If the call to the callee cannot throw, set the 'nounwind' flag on any 519f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // calls that we inline. 520f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands bool MarkNoUnwind = CS.doesNotThrow(); 521f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands 52280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock *OrigBB = TheCall->getParent(); 523ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner Function *Caller = OrigBB->getParent(); 524ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 5250e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen // GC poses two hazards to inlining, which only occur when the callee has GC: 5260e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen // 1. If the caller has no GC, then the callee's GC must be propagated to the 5270e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen // caller. 5280e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen // 2. If the caller has a differing GC, it is invalid to inline. 5295eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen if (CalledFunc->hasGC()) { 5305eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen if (!Caller->hasGC()) 5315eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen Caller->setGC(CalledFunc->getGC()); 5325eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen else if (CalledFunc->getGC() != Caller->getGC()) 5330e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen return false; 5340e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen } 535a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 53630fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer // Get the personality function from the callee if it contains a landing pad. 53730fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer Value *CalleePersonality = 0; 53830fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer for (Function::const_iterator I = CalledFunc->begin(), E = CalledFunc->end(); 53930fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer I != E; ++I) 540fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) { 541fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling const BasicBlock *BB = II->getUnwindDest(); 54227b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling const LandingPadInst *LP = BB->getLandingPadInst(); 54327b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling CalleePersonality = LP->getPersonalityFn(); 544fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling break; 545fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling } 546fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling 54730fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer // Find the personality function used by the landing pads of the caller. If it 54830fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer // exists, then check to see that it matches the personality function used in 54930fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer // the callee. 55006881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling if (CalleePersonality) { 55130fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer for (Function::const_iterator I = Caller->begin(), E = Caller->end(); 55230fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer I != E; ++I) 55330fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) { 55430fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer const BasicBlock *BB = II->getUnwindDest(); 55527b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling const LandingPadInst *LP = BB->getLandingPadInst(); 55630fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer 55730fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer // If the personality functions match, then we can perform the 55830fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer // inlining. Otherwise, we can't inline. 55930fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer // TODO: This isn't 100% true. Some personality functions are proper 56030fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer // supersets of others and can be used in place of the other. 56130fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer if (LP->getPersonalityFn() != CalleePersonality) 56230fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer return false; 56330fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer 56430fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer break; 56530fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer } 56606881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling } 56730fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer 5685052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner // Get an iterator to the last basic block in the function, which will have 5695052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner // the new function inlined after it. 5705052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner Function::iterator LastBlock = &Caller->back(); 5715052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner 5725e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Make sure to capture all of the return instructions from the cloned 5735e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // function. 574ec1bea0d94372985a0a5eb283e644c6d0dd345dcChris Lattner SmallVector<ReturnInst*, 8> Returns; 575cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner ClonedCodeInfo InlinedFunctionInfo; 5760744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen Function::iterator FirstNewBlock; 577f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands 57829d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel { // Scope to destroy VMap after cloning. 5791ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola ValueToValueMapTy VMap; 5805b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner 5819614fcc640eb628cc5dfddb277ebae9f6cb61014Dan Gohman assert(CalledFunc->arg_size() == CS.arg_size() && 5825e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner "No varargs calls can be inlined!"); 583a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 584c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner // Calculate the vector of arguments to pass into the function cloner, which 585c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner // matches up the formal to the actual argument values. 5865e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner CallSite::arg_iterator AI = CS.arg_begin(); 587c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner unsigned ArgNo = 0; 588e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner for (Function::const_arg_iterator I = CalledFunc->arg_begin(), 589c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) { 590c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner Value *ActualArg = *AI; 591a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 592d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands // When byval arguments actually inlined, we need to make the copy implied 593d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands // by them explicit. However, we don't do this if the callee is readonly 594d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands // or readnone, because the copy would be unneeded: the callee doesn't 595d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands // modify the struct. 596173862e5468fbcf4b022b9088d2c81b25c2d60c5Nick Lewycky if (CS.isByValArgument(ArgNo)) { 597e7ae705c32906979a527926864345016e76867b9Chris Lattner ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI, 598e7ae705c32906979a527926864345016e76867b9Chris Lattner CalledFunc->getParamAlignment(ArgNo+1)); 599e7ae705c32906979a527926864345016e76867b9Chris Lattner 6002914ba6ec793e2bb0e9ca5891af1d29ee2fee28eDuncan Sands // Calls that we inline may use the new alloca, so we need to clear 601e7ae705c32906979a527926864345016e76867b9Chris Lattner // their 'tail' flags if HandleByValArgument introduced a new alloca and 602e7ae705c32906979a527926864345016e76867b9Chris Lattner // the callee has calls. 603e7ae705c32906979a527926864345016e76867b9Chris Lattner MustClearTailCallFlags |= ActualArg != *AI; 604c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner } 605a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 60629d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel VMap[I] = ActualArg; 607c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner } 608fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 6095b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // We want the inliner to prune the code as it copies. We would LOVE to 6105b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // have no dead or constant instructions leftover after inlining occurs 6115b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // (which can happen, e.g., because an argument was constant), but we'll be 6125b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner // happy with whatever the cloner can do. 6136cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman CloneAndPruneFunctionInto(Caller, CalledFunc, VMap, 6146cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman /*ModuleLevelChanges=*/false, Returns, ".i", 61560915146f4d35e12f10dcdaa155596fac79184daChris Lattner &InlinedFunctionInfo, IFI.TD, TheCall); 616a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 617d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Remember the first block that is newly cloned over. 618d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner FirstNewBlock = LastBlock; ++FirstNewBlock; 619a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 620d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner // Update the callgraph if requested. 62160915146f4d35e12f10dcdaa155596fac79184daChris Lattner if (IFI.CG) 62229d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI); 6232cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel 6242cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel // Update inlined instructions' line number information. 6252cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel fixupLineNumbers(Caller, FirstNewBlock, TheCall); 626fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman } 627a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 628ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // If there are any alloca instructions in the block that used to be the entry 629ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // block for the callee, move them to the entry block of the caller. First 630ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // calculate which instruction they should be inserted before. We insert the 631ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // instructions at the end of the current alloca list. 63221f20558d629f7ff8f64c20746d890d29328a544Chris Lattner { 63380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner BasicBlock::iterator InsertPoint = Caller->begin()->begin(); 6345e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner for (BasicBlock::iterator I = FirstNewBlock->begin(), 635135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner E = FirstNewBlock->end(); I != E; ) { 636135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner AllocaInst *AI = dyn_cast<AllocaInst>(I++); 637135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (AI == 0) continue; 638135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 639135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If the alloca is now dead, remove it. This often occurs due to code 640135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // specialization. 641135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (AI->use_empty()) { 642135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner AI->eraseFromParent(); 643135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 64433bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner } 645135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 646135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!isa<Constant>(AI->getArraySize())) 647135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 648135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 64939add23dc54a1580983b1901384688d6622daa3bChris Lattner // Keep track of the static allocas that we inline into the caller. 65060915146f4d35e12f10dcdaa155596fac79184daChris Lattner IFI.StaticAllocas.push_back(AI); 6518f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner 652135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Scan for the block of allocas that we can move over, and move them 653135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // all at once. 654135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner while (isa<AllocaInst>(I) && 6558f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner isa<Constant>(cast<AllocaInst>(I)->getArraySize())) { 65660915146f4d35e12f10dcdaa155596fac79184daChris Lattner IFI.StaticAllocas.push_back(cast<AllocaInst>(I)); 657135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner ++I; 6588f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner } 659135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 660135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Transfer all of the allocas over in a block. Using splice means 661135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // that the instructions aren't removed from the symbol table, then 662135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // reinserted. 663135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Caller->getEntryBlock().getInstList().splice(InsertPoint, 664135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner FirstNewBlock->getInstList(), 665135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner AI, I); 666135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 66780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner } 66880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner 6696d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky // Leave lifetime markers for the static alloca's, scoping them to the 6706d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky // function we just inlined. 671fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier if (InsertLifetime && !IFI.StaticAllocas.empty()) { 6726d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky IRBuilder<> builder(FirstNewBlock->begin()); 6736d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky for (unsigned ai = 0, ae = IFI.StaticAllocas.size(); ai != ae; ++ai) { 6746d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky AllocaInst *AI = IFI.StaticAllocas[ai]; 6756d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky 6766d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky // If the alloca is already scoped to something smaller than the whole 6776d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky // function then there's no need to add redundant, less accurate markers. 6786d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky if (hasLifetimeMarkers(AI)) 6796d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky continue; 6806d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky 681009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov // Try to determine the size of the allocation. 682009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov ConstantInt *AllocaSize = 0; 683009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov if (ConstantInt *AIArraySize = 684009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov dyn_cast<ConstantInt>(AI->getArraySize())) { 685009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov if (IFI.TD) { 686009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov Type *AllocaType = AI->getAllocatedType(); 687009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov uint64_t AllocaTypeSize = IFI.TD->getTypeAllocSize(AllocaType); 688009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov uint64_t AllocaArraySize = AIArraySize->getLimitedValue(); 689009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov assert(AllocaArraySize > 0 && "array size of AllocaInst is zero"); 690009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov // Check that array size doesn't saturate uint64_t and doesn't 691009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov // overflow when it's multiplied by type size. 692009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov if (AllocaArraySize != ~0ULL && 693009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov UINT64_MAX / AllocaArraySize >= AllocaTypeSize) { 694009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov AllocaSize = ConstantInt::get(Type::getInt64Ty(AI->getContext()), 695009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov AllocaArraySize * AllocaTypeSize); 696009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov } 697009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov } 698009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov } 699009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov 700009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov builder.CreateLifetimeStart(AI, AllocaSize); 7016d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky for (unsigned ri = 0, re = Returns.size(); ri != re; ++ri) { 7026d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky IRBuilder<> builder(Returns[ri]); 703009c4d86c14bb2f46f9473f8f517393dabab7e9eAlexey Samsonov builder.CreateLifetimeEnd(AI, AllocaSize); 7046d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky } 7056d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky } 7066d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky } 7076d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky 708bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // If the inlined code contained dynamic alloca instructions, wrap the inlined 709bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // code with llvm.stacksave/llvm.stackrestore intrinsics. 710bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner if (InlinedFunctionInfo.ContainsDynamicAllocas) { 711bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner Module *M = Caller->getParent(); 712bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Get the two intrinsics we care about. 7136128df525501c333a650d097703c18d7e878f5e8Chris Lattner Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave); 7146128df525501c333a650d097703c18d7e878f5e8Chris Lattner Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore); 715d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner 716bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Insert the llvm.stacksave. 717c975a51ac042eb15bcb04a293cb737810ff40a00John McCall CallInst *SavedPtr = IRBuilder<>(FirstNewBlock, FirstNewBlock->begin()) 718c975a51ac042eb15bcb04a293cb737810ff40a00John McCall .CreateCall(StackSave, "savedstack"); 719a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 720bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // Insert a call to llvm.stackrestore before any return instructions in the 721bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner // inlined function. 722d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 723c975a51ac042eb15bcb04a293cb737810ff40a00John McCall IRBuilder<>(Returns[i]).CreateCall(StackRestore, SavedPtr); 724d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner } 725bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner } 726bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner 727a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands // If we are inlining tail call instruction through a call site that isn't 7281fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner // marked 'tail', we must remove the tail marker for any calls in the inlined 729f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // code. Also, calls inlined through a 'nounwind' call site should be marked 730f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands // 'nounwind'. 731f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (InlinedFunctionInfo.ContainsCalls && 732f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands (MustClearTailCallFlags || MarkNoUnwind)) { 7331b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner for (Function::iterator BB = FirstNewBlock, E = Caller->end(); 7341b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner BB != E; ++BB) 7351b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 736f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (CallInst *CI = dyn_cast<CallInst>(I)) { 737f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (MustClearTailCallFlags) 738f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands CI->setTailCall(false); 739f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands if (MarkNoUnwind) 740f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands CI->setDoesNotThrow(); 741f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands } 7421b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner } 7431b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner 7445e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // If we are inlining for an invoke instruction, we must make sure to rewrite 7458833ef03b9ceaa52063116819fff8b3d16fd8933Bill Wendling // any call instructions into invoke instructions. 746cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) 74781dfb3885252fbf621b080827a080099864415f8Chris Lattner HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo); 7485e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner 74944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If we cloned in _exactly one_ basic block, and if that block ends in a 75044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // return instruction, we splice the body of the inlined callee directly into 75144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // the calling basic block. 75244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) { 75344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Move all of the instructions right before the call. 75444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(), 75544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner FirstNewBlock->begin(), FirstNewBlock->end()); 75644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Remove the cloned basic block. 75744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Caller->getBasicBlockList().pop_back(); 758fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 75944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If the call site was an invoke instruction, add a branch to the normal 76044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // destination. 761f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { 762f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall); 763f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl NewBr->setDebugLoc(Returns[0]->getDebugLoc()); 764f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl } 76544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 76644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If the return instruction returned a value, replace uses of the call with 76744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // uses of the returned value. 768dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel if (!TheCall->use_empty()) { 769dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel ReturnInst *R = Returns[0]; 7705877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman if (TheCall == R->getReturnValue()) 7719e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); 7725877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman else 7735877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman TheCall->replaceAllUsesWith(R->getReturnValue()); 774dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel } 77544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Since we are now done with the Call/Invoke, we can delete it. 7761adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman TheCall->eraseFromParent(); 77744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 77844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Since we are now done with the return instruction, delete it also. 7791adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman Returns[0]->eraseFromParent(); 78044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 78144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // We are now done with the inlining. 78244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner return true; 78344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner } 78444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 78544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Otherwise, we have the normal case, of more than one block to inline or 78644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // multiple return sites. 78744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 7885e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // We want to clone the entire callee function into the hole between the 7895e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // "starter" and "ender" blocks. How we accomplish this depends on whether 7905e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // this is an invoke instruction or a call instruction. 7915e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner BasicBlock *AfterCallBB; 792f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl BranchInst *CreatedBranchToNormalDest = NULL; 7935e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { 794fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 7955e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Add an unconditional branch to make this look like the CallInst case... 796f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl CreatedBranchToNormalDest = BranchInst::Create(II->getNormalDest(), TheCall); 797fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 7985e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Split the basic block. This guarantees that no PHI nodes will have to be 7995e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // updated due to new incoming edges, and make the invoke case more 8005e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // symmetric to the call case. 801f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl AfterCallBB = OrigBB->splitBasicBlock(CreatedBranchToNormalDest, 802284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner CalledFunc->getName()+".exit"); 803fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 8045e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } else { // It's a call 80544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // If this is a call instruction, we need to split the basic block that 80644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // the call lives in. 8075e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // 8085e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner AfterCallBB = OrigBB->splitBasicBlock(TheCall, 809284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner CalledFunc->getName()+".exit"); 8105e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 8115e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner 81244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Change the branch that used to go to AfterCallBB to branch to the first 81344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // basic block of the inlined function. 81444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // 81544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner TerminatorInst *Br = OrigBB->getTerminator(); 816fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman assert(Br && Br->getOpcode() == Instruction::Br && 81744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner "splitBasicBlock broken!"); 81844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Br->setOperand(0, FirstNewBlock); 81944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 82044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 82144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // Now that the function is correct, make it a little bit nicer. In 82244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // particular, move the basic blocks inserted from the end of the function 82344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner // into the space made by splitting the source basic block. 82444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), 82544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner FirstNewBlock, Caller->end()); 82644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 8275e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Handle all of the return instructions that we just cloned in, and eliminate 8285e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // any users of the original call/invoke instruction. 829db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *RTy = CalledFunc->getReturnType(); 8302c31750cd0ebdc83a890ace97dbb6249b3abe44eDan Gohman 8316fb881c036c32728c4a128d81b6083457e534e09Duncan Sands PHINode *PHI = 0; 832fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman if (Returns.size() > 1) { 8335e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // The PHI node should go at the front of the new basic block to merge all 8345e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // possible incoming values. 8355e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner if (!TheCall->use_empty()) { 8363ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad PHI = PHINode::Create(RTy, Returns.size(), TheCall->getName(), 837fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman AfterCallBB->begin()); 838fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman // Anything that used the result of the function call should now use the 839fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman // PHI node as their operand. 840a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands TheCall->replaceAllUsesWith(PHI); 8415e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 842fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 843c478e52bf4c12691037856ee103c66946afeab6cGabor Greif // Loop over all of the return instructions adding entries to the PHI node 844c478e52bf4c12691037856ee103c66946afeab6cGabor Greif // as appropriate. 845fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman if (PHI) { 846fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 847fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman ReturnInst *RI = Returns[i]; 848fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman assert(RI->getReturnValue()->getType() == PHI->getType() && 849fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman "Ret value not consistent in function!"); 850fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman PHI->addIncoming(RI->getReturnValue(), RI->getParent()); 8515e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 85212a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel } 853fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 854c581acbbba3cb1af6a08e17314b26344333f9267Chris Lattner 855de62aeaec49ddcf4a4c61fbbb3a22d3a4dd448f0Gabor Greif // Add a branch to the merge points and remove return instructions. 85612a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel for (unsigned i = 0, e = Returns.size(); i != e; ++i) { 85712a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel ReturnInst *RI = Returns[i]; 8580744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen BranchInst::Create(AfterCallBB, RI); 859b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel RI->eraseFromParent(); 8605e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 861b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel } else if (!Returns.empty()) { 862b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // Otherwise, if there is exactly one return value, just replace anything 863b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // using the return value of the call with the computed value. 8645877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman if (!TheCall->use_empty()) { 8655877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman if (TheCall == Returns[0]->getReturnValue()) 8669e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); 8675877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman else 8685877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); 8695877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman } 870a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 87195c3e48f9557adb6064d580684bb14cacec2f826Jay Foad // Update PHI nodes that use the ReturnBB to use the AfterCallBB. 87295c3e48f9557adb6064d580684bb14cacec2f826Jay Foad BasicBlock *ReturnBB = Returns[0]->getParent(); 87395c3e48f9557adb6064d580684bb14cacec2f826Jay Foad ReturnBB->replaceAllUsesWith(AfterCallBB); 87495c3e48f9557adb6064d580684bb14cacec2f826Jay Foad 875b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // Splice the code from the return block into the block that it will return 876b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // to, which contains the code that was after the call. 877b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel AfterCallBB->getInstList().splice(AfterCallBB->begin(), 878b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel ReturnBB->getInstList()); 879a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 880f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl if (CreatedBranchToNormalDest) 881f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl CreatedBranchToNormalDest->setDebugLoc(Returns[0]->getDebugLoc()); 882f48509787acfcfc3f9eee2fb3084c2e8c7b4a009Adrian Prantl 883b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel // Delete the return instruction now and empty ReturnBB now. 884b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel Returns[0]->eraseFromParent(); 885b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel ReturnBB->eraseFromParent(); 8863787e765facfad5ea62753922d940bcdd52afd57Chris Lattner } else if (!TheCall->use_empty()) { 8873787e765facfad5ea62753922d940bcdd52afd57Chris Lattner // No returns, but something is using the return value of the call. Just 8883787e765facfad5ea62753922d940bcdd52afd57Chris Lattner // nuke the result. 8899e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); 8905e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner } 891fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 8925e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner // Since we are now done with the Call/Invoke, we can delete it. 8933787e765facfad5ea62753922d940bcdd52afd57Chris Lattner TheCall->eraseFromParent(); 894ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 8957152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // We should always be able to fold the entry block of the function into the 8967152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner // single predecessor of the block... 897cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); 8987152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); 89944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner 900cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Splice the code entry block into calling block, right before the 901cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // unconditional branch. 902e59fbc04ad343435705c28b3cf7038d65fe4af0aEric Christopher CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes 90395c3e48f9557adb6064d580684bb14cacec2f826Jay Foad OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); 904cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner 905cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Remove the unconditional branch. 906cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner OrigBB->getInstList().erase(Br); 907cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner 908cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner // Now we can remove the CalleeEntry block, which is now empty. 909cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner Caller->getBasicBlockList().erase(CalleeEntry); 910a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands 9116fb881c036c32728c4a128d81b6083457e534e09Duncan Sands // If we inserted a phi node, check to see if it has a single value (e.g. all 9126fb881c036c32728c4a128d81b6083457e534e09Duncan Sands // the entries are the same or undef). If so, remove the PHI so it doesn't 9136fb881c036c32728c4a128d81b6083457e534e09Duncan Sands // block other optimizations. 91406881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling if (PHI) { 9156fb881c036c32728c4a128d81b6083457e534e09Duncan Sands if (Value *V = SimplifyInstruction(PHI, IFI.TD)) { 9166fb881c036c32728c4a128d81b6083457e534e09Duncan Sands PHI->replaceAllUsesWith(V); 9176fb881c036c32728c4a128d81b6083457e534e09Duncan Sands PHI->eraseFromParent(); 9186fb881c036c32728c4a128d81b6083457e534e09Duncan Sands } 91906881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling } 9206fb881c036c32728c4a128d81b6083457e534e09Duncan Sands 921ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner return true; 922ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner} 923