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