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