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