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