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