Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 1 | //===- InlineFunction.cpp - Code to perform function inlining -------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements inlining of a function into a call site, resolving |
| 11 | // parameters and the return value as appropriate. |
| 12 | // |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 3787e76 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 21 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 22 | #include "llvm/Intrinsics.h" |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 23 | #include "llvm/Attributes.h" |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/CallGraph.h" |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/DebugInfo.h" |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Devang Patel | 641ca93 | 2008-03-10 18:22:16 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CallSite.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 32 | bool llvm::InlineFunction(CallInst *CI, CallGraph *CG, const TargetData *TD) { |
| 33 | return InlineFunction(CallSite(CI), CG, TD); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 34 | } |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 35 | bool llvm::InlineFunction(InvokeInst *II, CallGraph *CG, const TargetData *TD) { |
| 36 | return InlineFunction(CallSite(II), CG, TD); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 37 | } |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 39 | |
| 40 | /// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into |
| 41 | /// an invoke, we have to check all of all of the calls that can throw into |
| 42 | /// invokes. This function analyze BB to see if there are any calls, and if so, |
| 43 | /// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI |
| 44 | /// nodes in that block with the values specified in InvokeDestPHIValues. If |
| 45 | /// CallerCGN is specified, this function updates the call graph. |
| 46 | /// |
| 47 | static void HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB, |
| 48 | BasicBlock *InvokeDest, |
| 49 | const SmallVectorImpl<Value*> &InvokeDestPHIValues, |
| 50 | CallGraphNode *CallerCGN) { |
| 51 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 52 | Instruction *I = BBI++; |
| 53 | |
| 54 | // We only need to check for function calls: inlined invoke |
| 55 | // instructions require no special handling. |
| 56 | CallInst *CI = dyn_cast<CallInst>(I); |
| 57 | if (CI == 0) continue; |
| 58 | |
| 59 | // If this call cannot unwind, don't convert it to an invoke. |
| 60 | if (CI->doesNotThrow()) |
| 61 | continue; |
| 62 | |
| 63 | // Convert this function call into an invoke instruction. |
| 64 | // First, split the basic block. |
| 65 | BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); |
| 66 | |
| 67 | // Next, create the new invoke instruction, inserting it at the end |
| 68 | // of the old basic block. |
| 69 | SmallVector<Value*, 8> InvokeArgs(CI->op_begin()+1, CI->op_end()); |
| 70 | InvokeInst *II = |
| 71 | InvokeInst::Create(CI->getCalledValue(), Split, InvokeDest, |
| 72 | InvokeArgs.begin(), InvokeArgs.end(), |
| 73 | CI->getName(), BB->getTerminator()); |
| 74 | II->setCallingConv(CI->getCallingConv()); |
| 75 | II->setAttributes(CI->getAttributes()); |
| 76 | |
| 77 | // Make sure that anything using the call now uses the invoke! |
| 78 | CI->replaceAllUsesWith(II); |
| 79 | |
| 80 | // Update the callgraph if present. |
| 81 | if (CallerCGN) { |
| 82 | // We should be able to do this: |
| 83 | // (*CG)[Caller]->replaceCallSite(CI, II); |
| 84 | // but that fails if the old call site isn't in the call graph, |
| 85 | // which, because of LLVM bug 3601, it sometimes isn't. |
| 86 | for (CallGraphNode::iterator NI = CallerCGN->begin(), NE = CallerCGN->end(); |
| 87 | NI != NE; ++NI) { |
| 88 | if (NI->first == CI) { |
| 89 | NI->first = II; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Delete the unconditional branch inserted by splitBasicBlock |
| 96 | BB->getInstList().pop_back(); |
| 97 | Split->getInstList().pop_front(); // Delete the original call |
| 98 | |
| 99 | // Update any PHI nodes in the exceptional block to indicate that |
| 100 | // there is now a new entry in them. |
| 101 | unsigned i = 0; |
| 102 | for (BasicBlock::iterator I = InvokeDest->begin(); |
| 103 | isa<PHINode>(I); ++I, ++i) |
| 104 | cast<PHINode>(I)->addIncoming(InvokeDestPHIValues[i], BB); |
| 105 | |
| 106 | // This basic block is now complete, the caller will continue scanning the |
| 107 | // next one. |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 113 | /// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls |
| 114 | /// in the body of the inlined function into invokes and turn unwind |
| 115 | /// instructions into branches to the invoke unwind dest. |
| 116 | /// |
Nick Lewycky | dac5c4b | 2009-02-03 04:34:40 +0000 | [diff] [blame] | 117 | /// II is the invoke instruction being inlined. FirstNewBlock is the first |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 118 | /// block of the inlined code (the last block is the end of the function), |
| 119 | /// and InlineCodeInfo is information about the code that got inlined. |
| 120 | static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, |
Devang Patel | 28c531c | 2009-03-31 17:36:12 +0000 | [diff] [blame] | 121 | ClonedCodeInfo &InlinedCodeInfo, |
| 122 | CallGraph *CG) { |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 123 | BasicBlock *InvokeDest = II->getUnwindDest(); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 124 | SmallVector<Value*, 8> InvokeDestPHIValues; |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 125 | |
| 126 | // If there are PHI nodes in the unwind destination block, we need to |
| 127 | // keep track of which values came into them from this invoke, then remove |
| 128 | // the entry for this block. |
| 129 | BasicBlock *InvokeBlock = II->getParent(); |
| 130 | for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) { |
| 131 | PHINode *PN = cast<PHINode>(I); |
| 132 | // Save the value to use for this edge. |
| 133 | InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock)); |
| 134 | } |
| 135 | |
| 136 | Function *Caller = FirstNewBlock->getParent(); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 137 | |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 138 | // The inlined code is currently at the end of the function, scan from the |
| 139 | // start of the inlined code to its end, checking for stuff we need to |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 140 | // rewrite. If the code doesn't have calls or unwinds, we know there is |
| 141 | // nothing to rewrite. |
| 142 | if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) { |
| 143 | // Now that everything is happy, we have one final detail. The PHI nodes in |
| 144 | // the exception destination block still have entries due to the original |
| 145 | // invoke instruction. Eliminate these entries (which might even delete the |
| 146 | // PHI node) now. |
| 147 | InvokeDest->removePredecessor(II->getParent()); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | CallGraphNode *CallerCGN = 0; |
| 152 | if (CG) CallerCGN = (*CG)[Caller]; |
| 153 | |
| 154 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){ |
| 155 | if (InlinedCodeInfo.ContainsCalls) |
| 156 | HandleCallsInBlockInlinedThroughInvoke(BB, InvokeDest, |
| 157 | InvokeDestPHIValues, CallerCGN); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 159 | if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 160 | // An UnwindInst requires special handling when it gets inlined into an |
| 161 | // invoke site. Once this happens, we know that the unwind would cause |
| 162 | // a control transfer to the invoke exception destination, so we can |
| 163 | // transform it into a direct branch to the exception destination. |
| 164 | BranchInst::Create(InvokeDest, UI); |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 166 | // Delete the unwind instruction! |
| 167 | UI->eraseFromParent(); |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 169 | // Update any PHI nodes in the exceptional block to indicate that |
| 170 | // there is now a new entry in them. |
| 171 | unsigned i = 0; |
| 172 | for (BasicBlock::iterator I = InvokeDest->begin(); |
| 173 | isa<PHINode>(I); ++I, ++i) { |
| 174 | PHINode *PN = cast<PHINode>(I); |
| 175 | PN->addIncoming(InvokeDestPHIValues[i], BB); |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Now that everything is happy, we have one final detail. The PHI nodes in |
| 181 | // the exception destination block still have entries due to the original |
| 182 | // invoke instruction. Eliminate these entries (which might even delete the |
| 183 | // PHI node) now. |
| 184 | InvokeDest->removePredecessor(II->getParent()); |
| 185 | } |
| 186 | |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 187 | /// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee |
| 188 | /// into the caller, update the specified callgraph to reflect the changes we |
| 189 | /// made. Note that it's possible that not all code was copied over, so only |
Duncan Sands | d7b9851 | 2008-09-08 11:05:51 +0000 | [diff] [blame] | 190 | /// some edges of the callgraph may remain. |
| 191 | static void UpdateCallGraphAfterInlining(CallSite CS, |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 192 | Function::iterator FirstNewBlock, |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 193 | DenseMap<const Value*, Value*> &ValueMap, |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 194 | CallGraph &CG) { |
Duncan Sands | d7b9851 | 2008-09-08 11:05:51 +0000 | [diff] [blame] | 195 | const Function *Caller = CS.getInstruction()->getParent()->getParent(); |
| 196 | const Function *Callee = CS.getCalledFunction(); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 197 | CallGraphNode *CalleeNode = CG[Callee]; |
| 198 | CallGraphNode *CallerNode = CG[Caller]; |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 199 | |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 200 | // Since we inlined some uninlined call sites in the callee into the caller, |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 201 | // add edges from the caller to all of the callees of the callee. |
Gabor Greif | c478e52 | 2009-01-15 18:40:09 +0000 | [diff] [blame] | 202 | CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end(); |
| 203 | |
| 204 | // Consider the case where CalleeNode == CallerNode. |
Gabor Greif | 1253298 | 2009-01-17 00:09:08 +0000 | [diff] [blame] | 205 | CallGraphNode::CalledFunctionsVector CallCache; |
Gabor Greif | c478e52 | 2009-01-15 18:40:09 +0000 | [diff] [blame] | 206 | if (CalleeNode == CallerNode) { |
| 207 | CallCache.assign(I, E); |
| 208 | I = CallCache.begin(); |
| 209 | E = CallCache.end(); |
| 210 | } |
| 211 | |
| 212 | for (; I != E; ++I) { |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 213 | const Instruction *OrigCall = I->first.getInstruction(); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 215 | DenseMap<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall); |
Chris Lattner | 981418b | 2006-07-12 21:37:11 +0000 | [diff] [blame] | 216 | // Only copy the edge if the call was inlined! |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 217 | if (VMI == ValueMap.end() || VMI->second == 0) |
| 218 | continue; |
| 219 | |
| 220 | // If the call was inlined, but then constant folded, there is no edge to |
| 221 | // add. Check for this case. |
| 222 | if (Instruction *NewCall = dyn_cast<Instruction>(VMI->second)) |
| 223 | CallerNode->addCalledFunction(CallSite::get(NewCall), I->second); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 225 | |
Dale Johannesen | 39fa324 | 2009-01-13 22:43:37 +0000 | [diff] [blame] | 226 | // Update the call graph by deleting the edge from Callee to Caller. We must |
| 227 | // do this after the loop above in case Caller and Callee are the same. |
| 228 | CallerNode->removeCallEdgeFor(CS); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 231 | /// findFnRegionEndMarker - This is a utility routine that is used by |
| 232 | /// InlineFunction. Return llvm.dbg.region.end intrinsic that corresponds |
| 233 | /// to the llvm.dbg.func.start of the function F. Otherwise return NULL. |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 234 | /// |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 235 | static const DbgRegionEndInst *findFnRegionEndMarker(const Function *F) { |
| 236 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 237 | GlobalVariable *FnStart = NULL; |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 238 | const DbgRegionEndInst *FnEnd = NULL; |
| 239 | for (Function::const_iterator FI = F->begin(), FE =F->end(); FI != FE; ++FI) |
| 240 | for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end(); BI != BE; |
| 241 | ++BI) { |
| 242 | if (FnStart == NULL) { |
| 243 | if (const DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI)) { |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 244 | DISubprogram SP(cast<GlobalVariable>(FSI->getSubprogram())); |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 245 | assert (SP.isNull() == false && "Invalid llvm.dbg.func.start"); |
| 246 | if (SP.describes(F)) |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 247 | FnStart = SP.getGV(); |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 249 | continue; |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 251 | |
| 252 | if (const DbgRegionEndInst *REI = dyn_cast<DbgRegionEndInst>(BI)) |
| 253 | if (REI->getContext() == FnStart) |
| 254 | FnEnd = REI; |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 255 | } |
| 256 | return FnEnd; |
| 257 | } |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 258 | |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 259 | // InlineFunction - This function inlines the called function into the basic |
| 260 | // block of the caller. This returns false if it is not possible to inline this |
| 261 | // call. The program is still in a well defined state if this occurs though. |
| 262 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 263 | // Note that this only does one level of inlining. For example, if the |
| 264 | // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 265 | // exists in the instruction stream. Similiarly this will inline a recursive |
| 266 | // function by one level. |
| 267 | // |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 268 | bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 269 | Instruction *TheCall = CS.getInstruction(); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 270 | LLVMContext &Context = TheCall->getContext(); |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 271 | assert(TheCall->getParent() && TheCall->getParent()->getParent() && |
| 272 | "Instruction not in function!"); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 274 | const Function *CalledFunc = CS.getCalledFunction(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 275 | if (CalledFunc == 0 || // Can't inline external function or indirect |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 276 | CalledFunc->isDeclaration() || // call, or call to a vararg function! |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 277 | CalledFunc->getFunctionType()->isVarArg()) return false; |
| 278 | |
Chris Lattner | 1b49141 | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 279 | |
Chris Lattner | af9985c | 2009-02-12 07:06:42 +0000 | [diff] [blame] | 280 | // If the call to the callee is not a tail call, we must clear the 'tail' |
Chris Lattner | 1b49141 | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 281 | // flags on any calls that we inline. |
| 282 | bool MustClearTailCallFlags = |
Chris Lattner | af9985c | 2009-02-12 07:06:42 +0000 | [diff] [blame] | 283 | !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall()); |
Chris Lattner | 1b49141 | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 284 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 285 | // If the call to the callee cannot throw, set the 'nounwind' flag on any |
| 286 | // calls that we inline. |
| 287 | bool MarkNoUnwind = CS.doesNotThrow(); |
| 288 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 289 | BasicBlock *OrigBB = TheCall->getParent(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 290 | Function *Caller = OrigBB->getParent(); |
| 291 | |
Gordon Henriksen | 0e13821 | 2007-12-25 03:10:07 +0000 | [diff] [blame] | 292 | // GC poses two hazards to inlining, which only occur when the callee has GC: |
| 293 | // 1. If the caller has no GC, then the callee's GC must be propagated to the |
| 294 | // caller. |
| 295 | // 2. If the caller has a differing GC, it is invalid to inline. |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 296 | if (CalledFunc->hasGC()) { |
| 297 | if (!Caller->hasGC()) |
| 298 | Caller->setGC(CalledFunc->getGC()); |
| 299 | else if (CalledFunc->getGC() != Caller->getGC()) |
Gordon Henriksen | 0e13821 | 2007-12-25 03:10:07 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 5052c91 | 2004-02-04 01:41:09 +0000 | [diff] [blame] | 303 | // Get an iterator to the last basic block in the function, which will have |
| 304 | // the new function inlined after it. |
| 305 | // |
| 306 | Function::iterator LastBlock = &Caller->back(); |
| 307 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 308 | // Make sure to capture all of the return instructions from the cloned |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 309 | // function. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 310 | std::vector<ReturnInst*> Returns; |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 311 | ClonedCodeInfo InlinedFunctionInfo; |
Dale Johannesen | 0744f09 | 2009-03-04 02:09:48 +0000 | [diff] [blame] | 312 | Function::iterator FirstNewBlock; |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 314 | { // Scope to destroy ValueMap after cloning. |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 315 | DenseMap<const Value*, Value*> ValueMap; |
Chris Lattner | 5b5bc30 | 2006-05-27 01:28:04 +0000 | [diff] [blame] | 316 | |
Dan Gohman | 9614fcc | 2008-06-20 17:11:32 +0000 | [diff] [blame] | 317 | assert(CalledFunc->arg_size() == CS.arg_size() && |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 318 | "No varargs calls can be inlined!"); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 319 | |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 320 | // Calculate the vector of arguments to pass into the function cloner, which |
| 321 | // matches up the formal to the actual argument values. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 322 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 323 | unsigned ArgNo = 0; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 324 | for (Function::const_arg_iterator I = CalledFunc->arg_begin(), |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 325 | E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) { |
| 326 | Value *ActualArg = *AI; |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 327 | |
Duncan Sands | d82375c | 2008-01-27 18:12:58 +0000 | [diff] [blame] | 328 | // When byval arguments actually inlined, we need to make the copy implied |
| 329 | // by them explicit. However, we don't do this if the callee is readonly |
| 330 | // or readnone, because the copy would be unneeded: the callee doesn't |
| 331 | // modify the struct. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 332 | if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal) && |
Duncan Sands | d82375c | 2008-01-27 18:12:58 +0000 | [diff] [blame] | 333 | !CalledFunc->onlyReadsMemory()) { |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 334 | const Type *AggTy = cast<PointerType>(I->getType())->getElementType(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 335 | const Type *VoidPtrTy = |
| 336 | PointerType::getUnqual(Type::getInt8Ty(Context)); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 337 | |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 338 | // Create the alloca. If we have TargetData, use nice alignment. |
| 339 | unsigned Align = 1; |
| 340 | if (TD) Align = TD->getPrefTypeAlignment(AggTy); |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 341 | Value *NewAlloca = new AllocaInst(AggTy, 0, Align, |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 342 | I->getName(), |
| 343 | &*Caller->begin()->begin()); |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 344 | // Emit a memcpy. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 345 | const Type *Tys[] = { Type::getInt64Ty(Context) }; |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 346 | Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(), |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 347 | Intrinsic::memcpy, |
| 348 | Tys, 1); |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 349 | Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall); |
| 350 | Value *SrcCast = new BitCastInst(*AI, VoidPtrTy, "tmp", TheCall); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 351 | |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 352 | Value *Size; |
| 353 | if (TD == 0) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 354 | Size = ConstantExpr::getSizeOf(AggTy); |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 355 | else |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 356 | Size = ConstantInt::get(Type::getInt64Ty(Context), |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 357 | TD->getTypeStoreSize(AggTy)); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 358 | |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 359 | // Always generate a memcpy of alignment 1 here because we don't know |
| 360 | // the alignment of the src pointer. Other optimizations can infer |
| 361 | // better alignment. |
| 362 | Value *CallArgs[] = { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 363 | DestCast, SrcCast, Size, |
| 364 | ConstantInt::get(Type::getInt32Ty(Context), 1) |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 365 | }; |
| 366 | CallInst *TheMemCpy = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 367 | CallInst::Create(MemCpyFn, CallArgs, CallArgs+4, "", TheCall); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 368 | |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 369 | // If we have a call graph, update it. |
| 370 | if (CG) { |
| 371 | CallGraphNode *MemCpyCGN = CG->getOrInsertFunction(MemCpyFn); |
| 372 | CallGraphNode *CallerNode = (*CG)[Caller]; |
| 373 | CallerNode->addCalledFunction(TheMemCpy, MemCpyCGN); |
| 374 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 375 | |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 376 | // Uses of the argument in the function should use our new alloca |
| 377 | // instead. |
| 378 | ActualArg = NewAlloca; |
| 379 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 380 | |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 381 | ValueMap[I] = ActualArg; |
| 382 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 383 | |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 384 | // Adjust llvm.dbg.region.end. If the CalledFunc has region end |
| 385 | // marker then clone that marker after next stop point at the |
| 386 | // call site. The function body cloner does not clone original |
| 387 | // region end marker from the CalledFunc. This will ensure that |
| 388 | // inlined function's scope ends at the right place. |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 389 | if (const DbgRegionEndInst *DREI = findFnRegionEndMarker(CalledFunc)) { |
| 390 | for (BasicBlock::iterator BI = TheCall, BE = TheCall->getParent()->end(); |
| 391 | BI != BE; ++BI) { |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 392 | if (DbgStopPointInst *DSPI = dyn_cast<DbgStopPointInst>(BI)) { |
| 393 | if (DbgRegionEndInst *NewDREI = |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 394 | dyn_cast<DbgRegionEndInst>(DREI->clone(Context))) |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 395 | NewDREI->insertAfter(DSPI); |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Chris Lattner | 5b5bc30 | 2006-05-27 01:28:04 +0000 | [diff] [blame] | 401 | // We want the inliner to prune the code as it copies. We would LOVE to |
| 402 | // have no dead or constant instructions leftover after inlining occurs |
| 403 | // (which can happen, e.g., because an argument was constant), but we'll be |
| 404 | // happy with whatever the cloner can do. |
| 405 | CloneAndPruneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i", |
Chris Lattner | 1dfdf82 | 2007-01-30 23:22:39 +0000 | [diff] [blame] | 406 | &InlinedFunctionInfo, TD); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 407 | |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 408 | // Remember the first block that is newly cloned over. |
| 409 | FirstNewBlock = LastBlock; ++FirstNewBlock; |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 410 | |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 411 | // Update the callgraph if requested. |
| 412 | if (CG) |
Duncan Sands | d7b9851 | 2008-09-08 11:05:51 +0000 | [diff] [blame] | 413 | UpdateCallGraphAfterInlining(CS, FirstNewBlock, ValueMap, *CG); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 414 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 415 | |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 416 | // If there are any alloca instructions in the block that used to be the entry |
| 417 | // block for the callee, move them to the entry block of the caller. First |
| 418 | // calculate which instruction they should be inserted before. We insert the |
| 419 | // instructions at the end of the current alloca list. |
| 420 | // |
Chris Lattner | 21f2055 | 2006-01-13 18:16:48 +0000 | [diff] [blame] | 421 | { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 422 | BasicBlock::iterator InsertPoint = Caller->begin()->begin(); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 423 | for (BasicBlock::iterator I = FirstNewBlock->begin(), |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 424 | E = FirstNewBlock->end(); I != E; ) { |
| 425 | AllocaInst *AI = dyn_cast<AllocaInst>(I++); |
| 426 | if (AI == 0) continue; |
| 427 | |
| 428 | // If the alloca is now dead, remove it. This often occurs due to code |
| 429 | // specialization. |
| 430 | if (AI->use_empty()) { |
| 431 | AI->eraseFromParent(); |
| 432 | continue; |
Chris Lattner | 33bb3c8 | 2006-09-13 19:23:57 +0000 | [diff] [blame] | 433 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame^] | 434 | |
| 435 | if (!isa<Constant>(AI->getArraySize())) |
| 436 | continue; |
| 437 | |
| 438 | // Scan for the block of allocas that we can move over, and move them |
| 439 | // all at once. |
| 440 | while (isa<AllocaInst>(I) && |
| 441 | isa<Constant>(cast<AllocaInst>(I)->getArraySize())) |
| 442 | ++I; |
| 443 | |
| 444 | // Transfer all of the allocas over in a block. Using splice means |
| 445 | // that the instructions aren't removed from the symbol table, then |
| 446 | // reinserted. |
| 447 | Caller->getEntryBlock().getInstList().splice(InsertPoint, |
| 448 | FirstNewBlock->getInstList(), |
| 449 | AI, I); |
| 450 | } |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 451 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 452 | |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 453 | // If the inlined code contained dynamic alloca instructions, wrap the inlined |
| 454 | // code with llvm.stacksave/llvm.stackrestore intrinsics. |
| 455 | if (InlinedFunctionInfo.ContainsDynamicAllocas) { |
| 456 | Module *M = Caller->getParent(); |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 457 | // Get the two intrinsics we care about. |
Chris Lattner | a121fdd | 2007-01-07 07:54:34 +0000 | [diff] [blame] | 458 | Constant *StackSave, *StackRestore; |
Duncan Sands | 3d292ac | 2008-04-07 13:43:58 +0000 | [diff] [blame] | 459 | StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave); |
| 460 | StackRestore = Intrinsic::getDeclaration(M, Intrinsic::stackrestore); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 461 | |
| 462 | // If we are preserving the callgraph, add edges to the stacksave/restore |
| 463 | // functions for the calls we insert. |
Chris Lattner | 21ba23d | 2006-07-18 21:48:57 +0000 | [diff] [blame] | 464 | CallGraphNode *StackSaveCGN = 0, *StackRestoreCGN = 0, *CallerNode = 0; |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 465 | if (CG) { |
Chris Lattner | a121fdd | 2007-01-07 07:54:34 +0000 | [diff] [blame] | 466 | // We know that StackSave/StackRestore are Function*'s, because they are |
| 467 | // intrinsics which must have the right types. |
| 468 | StackSaveCGN = CG->getOrInsertFunction(cast<Function>(StackSave)); |
| 469 | StackRestoreCGN = CG->getOrInsertFunction(cast<Function>(StackRestore)); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 470 | CallerNode = (*CG)[Caller]; |
| 471 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 472 | |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 473 | // Insert the llvm.stacksave. |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 474 | CallInst *SavedPtr = CallInst::Create(StackSave, "savedstack", |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 475 | FirstNewBlock->begin()); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 476 | if (CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 477 | |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 478 | // Insert a call to llvm.stackrestore before any return instructions in the |
| 479 | // inlined function. |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 480 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 481 | CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", Returns[i]); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 482 | if (CG) CallerNode->addCalledFunction(CI, StackRestoreCGN); |
| 483 | } |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 484 | |
| 485 | // Count the number of StackRestore calls we insert. |
| 486 | unsigned NumStackRestores = Returns.size(); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 487 | |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 488 | // If we are inlining an invoke instruction, insert restores before each |
| 489 | // unwind. These unwinds will be rewritten into branches later. |
| 490 | if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) { |
| 491 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 492 | BB != E; ++BB) |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 493 | if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
Duncan Sands | 3d292ac | 2008-04-07 13:43:58 +0000 | [diff] [blame] | 494 | CallInst::Create(StackRestore, SavedPtr, "", UI); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 495 | ++NumStackRestores; |
| 496 | } |
| 497 | } |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 500 | // If we are inlining tail call instruction through a call site that isn't |
Chris Lattner | 1fdf4a8 | 2006-01-13 19:18:11 +0000 | [diff] [blame] | 501 | // marked 'tail', we must remove the tail marker for any calls in the inlined |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 502 | // code. Also, calls inlined through a 'nounwind' call site should be marked |
| 503 | // 'nounwind'. |
| 504 | if (InlinedFunctionInfo.ContainsCalls && |
| 505 | (MustClearTailCallFlags || MarkNoUnwind)) { |
Chris Lattner | 1b49141 | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 506 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 507 | BB != E; ++BB) |
| 508 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 509 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 510 | if (MustClearTailCallFlags) |
| 511 | CI->setTailCall(false); |
| 512 | if (MarkNoUnwind) |
| 513 | CI->setDoesNotThrow(); |
| 514 | } |
Chris Lattner | 1b49141 | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 517 | // If we are inlining through a 'nounwind' call site then any inlined 'unwind' |
| 518 | // instructions are unreachable. |
| 519 | if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind) |
| 520 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 521 | BB != E; ++BB) { |
| 522 | TerminatorInst *Term = BB->getTerminator(); |
| 523 | if (isa<UnwindInst>(Term)) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 524 | new UnreachableInst(Context, Term); |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 525 | BB->getInstList().erase(Term); |
| 526 | } |
| 527 | } |
| 528 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 529 | // If we are inlining for an invoke instruction, we must make sure to rewrite |
| 530 | // any inlined 'unwind' instructions into branches to the invoke exception |
| 531 | // destination, and call instructions into invoke instructions. |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 532 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) |
Devang Patel | 28c531c | 2009-03-31 17:36:12 +0000 | [diff] [blame] | 533 | HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo, CG); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 534 | |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 535 | // If we cloned in _exactly one_ basic block, and if that block ends in a |
| 536 | // return instruction, we splice the body of the inlined callee directly into |
| 537 | // the calling basic block. |
| 538 | if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) { |
| 539 | // Move all of the instructions right before the call. |
| 540 | OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(), |
| 541 | FirstNewBlock->begin(), FirstNewBlock->end()); |
| 542 | // Remove the cloned basic block. |
| 543 | Caller->getBasicBlockList().pop_back(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 545 | // If the call site was an invoke instruction, add a branch to the normal |
| 546 | // destination. |
| 547 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 548 | BranchInst::Create(II->getNormalDest(), TheCall); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 549 | |
| 550 | // If the return instruction returned a value, replace uses of the call with |
| 551 | // uses of the returned value. |
Devang Patel | dc00d42 | 2008-03-04 21:15:15 +0000 | [diff] [blame] | 552 | if (!TheCall->use_empty()) { |
| 553 | ReturnInst *R = Returns[0]; |
Eli Friedman | 5877ad7 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 554 | if (TheCall == R->getReturnValue()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 555 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Eli Friedman | 5877ad7 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 556 | else |
| 557 | TheCall->replaceAllUsesWith(R->getReturnValue()); |
Devang Patel | dc00d42 | 2008-03-04 21:15:15 +0000 | [diff] [blame] | 558 | } |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 559 | // Since we are now done with the Call/Invoke, we can delete it. |
Dan Gohman | 1adec83 | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 560 | TheCall->eraseFromParent(); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 561 | |
| 562 | // Since we are now done with the return instruction, delete it also. |
Dan Gohman | 1adec83 | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 563 | Returns[0]->eraseFromParent(); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 564 | |
| 565 | // We are now done with the inlining. |
| 566 | return true; |
| 567 | } |
| 568 | |
| 569 | // Otherwise, we have the normal case, of more than one block to inline or |
| 570 | // multiple return sites. |
| 571 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 572 | // We want to clone the entire callee function into the hole between the |
| 573 | // "starter" and "ender" blocks. How we accomplish this depends on whether |
| 574 | // this is an invoke instruction or a call instruction. |
| 575 | BasicBlock *AfterCallBB; |
| 576 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 578 | // Add an unconditional branch to make this look like the CallInst case... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 579 | BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 581 | // Split the basic block. This guarantees that no PHI nodes will have to be |
| 582 | // updated due to new incoming edges, and make the invoke case more |
| 583 | // symmetric to the call case. |
| 584 | AfterCallBB = OrigBB->splitBasicBlock(NewBr, |
Chris Lattner | 284d1b8 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 585 | CalledFunc->getName()+".exit"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 587 | } else { // It's a call |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 588 | // If this is a call instruction, we need to split the basic block that |
| 589 | // the call lives in. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 590 | // |
| 591 | AfterCallBB = OrigBB->splitBasicBlock(TheCall, |
Chris Lattner | 284d1b8 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 592 | CalledFunc->getName()+".exit"); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 595 | // Change the branch that used to go to AfterCallBB to branch to the first |
| 596 | // basic block of the inlined function. |
| 597 | // |
| 598 | TerminatorInst *Br = OrigBB->getTerminator(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 599 | assert(Br && Br->getOpcode() == Instruction::Br && |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 600 | "splitBasicBlock broken!"); |
| 601 | Br->setOperand(0, FirstNewBlock); |
| 602 | |
| 603 | |
| 604 | // Now that the function is correct, make it a little bit nicer. In |
| 605 | // particular, move the basic blocks inserted from the end of the function |
| 606 | // into the space made by splitting the source basic block. |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 607 | Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), |
| 608 | FirstNewBlock, Caller->end()); |
| 609 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 610 | // Handle all of the return instructions that we just cloned in, and eliminate |
| 611 | // any users of the original call/invoke instruction. |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 612 | const Type *RTy = CalledFunc->getReturnType(); |
Dan Gohman | 2c31750 | 2008-06-20 01:03:44 +0000 | [diff] [blame] | 613 | |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 614 | if (Returns.size() > 1) { |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 615 | // The PHI node should go at the front of the new basic block to merge all |
| 616 | // possible incoming values. |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 617 | PHINode *PHI = 0; |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 618 | if (!TheCall->use_empty()) { |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 619 | PHI = PHINode::Create(RTy, TheCall->getName(), |
| 620 | AfterCallBB->begin()); |
| 621 | // Anything that used the result of the function call should now use the |
| 622 | // PHI node as their operand. |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 623 | TheCall->replaceAllUsesWith(PHI); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 624 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 625 | |
Gabor Greif | c478e52 | 2009-01-15 18:40:09 +0000 | [diff] [blame] | 626 | // Loop over all of the return instructions adding entries to the PHI node |
| 627 | // as appropriate. |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 628 | if (PHI) { |
| 629 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 630 | ReturnInst *RI = Returns[i]; |
| 631 | assert(RI->getReturnValue()->getType() == PHI->getType() && |
| 632 | "Ret value not consistent in function!"); |
| 633 | PHI->addIncoming(RI->getReturnValue(), RI->getParent()); |
Devang Patel | 12a466b | 2008-03-07 20:06:16 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Gabor Greif | de62aea | 2009-01-16 23:08:50 +0000 | [diff] [blame] | 637 | // Add a branch to the merge points and remove return instructions. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 638 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 639 | ReturnInst *RI = Returns[i]; |
Dale Johannesen | 0744f09 | 2009-03-04 02:09:48 +0000 | [diff] [blame] | 640 | BranchInst::Create(AfterCallBB, RI); |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 641 | RI->eraseFromParent(); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 642 | } |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 643 | } else if (!Returns.empty()) { |
| 644 | // Otherwise, if there is exactly one return value, just replace anything |
| 645 | // using the return value of the call with the computed value. |
Eli Friedman | 5877ad7 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 646 | if (!TheCall->use_empty()) { |
| 647 | if (TheCall == Returns[0]->getReturnValue()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 648 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Eli Friedman | 5877ad7 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 649 | else |
| 650 | TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); |
| 651 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 652 | |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 653 | // Splice the code from the return block into the block that it will return |
| 654 | // to, which contains the code that was after the call. |
| 655 | BasicBlock *ReturnBB = Returns[0]->getParent(); |
| 656 | AfterCallBB->getInstList().splice(AfterCallBB->begin(), |
| 657 | ReturnBB->getInstList()); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 658 | |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 659 | // Update PHI nodes that use the ReturnBB to use the AfterCallBB. |
| 660 | ReturnBB->replaceAllUsesWith(AfterCallBB); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 661 | |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 662 | // Delete the return instruction now and empty ReturnBB now. |
| 663 | Returns[0]->eraseFromParent(); |
| 664 | ReturnBB->eraseFromParent(); |
Chris Lattner | 3787e76 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 665 | } else if (!TheCall->use_empty()) { |
| 666 | // No returns, but something is using the return value of the call. Just |
| 667 | // nuke the result. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 668 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 669 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 671 | // Since we are now done with the Call/Invoke, we can delete it. |
Chris Lattner | 3787e76 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 672 | TheCall->eraseFromParent(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 674 | // We should always be able to fold the entry block of the function into the |
| 675 | // single predecessor of the block... |
Chris Lattner | cd01ae5 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 676 | assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 677 | BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 678 | |
Chris Lattner | cd01ae5 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 679 | // Splice the code entry block into calling block, right before the |
| 680 | // unconditional branch. |
| 681 | OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); |
| 682 | CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes |
| 683 | |
| 684 | // Remove the unconditional branch. |
| 685 | OrigBB->getInstList().erase(Br); |
| 686 | |
| 687 | // Now we can remove the CalleeEntry block, which is now empty. |
| 688 | Caller->getBasicBlockList().erase(CalleeEntry); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 689 | |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 690 | return true; |
| 691 | } |