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 | // |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 13 | // The code in this file for handling inlines through invoke |
| 14 | // instructions preserves semantics only under some assumptions about |
| 15 | // the behavior of unwinders which correspond to gcc-style libUnwind |
| 16 | // exception personality functions. Eventually the IR will be |
| 17 | // improved to make this unnecessary, but until then, this code is |
| 18 | // marked [LIBUNWIND]. |
| 19 | // |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 3787e76 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 26 | #include "llvm/Instructions.h" |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 27 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 28 | #include "llvm/Intrinsics.h" |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 29 | #include "llvm/Attributes.h" |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/CallGraph.h" |
Devang Patel | 517576d | 2009-04-15 00:17:06 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/DebugInfo.h" |
Duncan Sands | 6fb881c | 2010-11-17 11:16:23 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/InstructionSimplify.h" |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 7569d79 | 2010-12-25 20:42:38 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallVector.h" |
Devang Patel | 641ca93 | 2008-03-10 18:22:16 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CallSite.h" |
Nick Lewycky | 6d55f22 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 38 | #include "llvm/Support/IRBuilder.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 41 | bool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI) { |
| 42 | return InlineFunction(CallSite(CI), IFI); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 44 | bool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI) { |
| 45 | return InlineFunction(CallSite(II), IFI); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 47 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 48 | /// [LIBUNWIND] Find the (possibly absent) call to @llvm.eh.selector in |
| 49 | /// the given landing pad. |
| 50 | static EHSelectorInst *findSelectorForLandingPad(BasicBlock *lpad) { |
| 51 | // The llvm.eh.exception call is required to be in the landing pad. |
| 52 | for (BasicBlock::iterator i = lpad->begin(), e = lpad->end(); i != e; i++) { |
| 53 | EHExceptionInst *exn = dyn_cast<EHExceptionInst>(i); |
| 54 | if (!exn) continue; |
| 55 | |
| 56 | EHSelectorInst *selector = 0; |
| 57 | for (Instruction::use_iterator |
| 58 | ui = exn->use_begin(), ue = exn->use_end(); ui != ue; ++ui) { |
| 59 | EHSelectorInst *sel = dyn_cast<EHSelectorInst>(*ui); |
| 60 | if (!sel) continue; |
| 61 | |
| 62 | // Immediately accept an eh.selector in the landing pad. |
| 63 | if (sel->getParent() == lpad) return sel; |
| 64 | |
| 65 | // Otherwise, use the first selector we see. |
| 66 | if (!selector) selector = sel; |
| 67 | } |
| 68 | |
| 69 | return selector; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 75 | namespace { |
| 76 | /// A class for recording information about inlining through an invoke. |
| 77 | class InvokeInliningInfo { |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 78 | BasicBlock *OuterUnwindDest; |
| 79 | EHSelectorInst *OuterSelector; |
| 80 | BasicBlock *InnerUnwindDest; |
| 81 | PHINode *InnerExceptionPHI; |
| 82 | PHINode *InnerSelectorPHI; |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 83 | SmallVector<Value*, 8> UnwindDestPHIValues; |
| 84 | |
| 85 | public: |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 86 | InvokeInliningInfo(InvokeInst *II) : |
| 87 | OuterUnwindDest(II->getUnwindDest()), OuterSelector(0), |
| 88 | InnerUnwindDest(0), InnerExceptionPHI(0), InnerSelectorPHI(0) { |
| 89 | |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 90 | // If there are PHI nodes in the unwind destination block, we |
| 91 | // need to keep track of which values came into them from the |
| 92 | // invoke before removing the edge from this block. |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 93 | llvm::BasicBlock *invokeBB = II->getParent(); |
| 94 | for (BasicBlock::iterator I = OuterUnwindDest->begin(); |
| 95 | isa<PHINode>(I); ++I) { |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 96 | // Save the value to use for this edge. |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 97 | PHINode *phi = cast<PHINode>(I); |
| 98 | UnwindDestPHIValues.push_back(phi->getIncomingValueForBlock(invokeBB)); |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 102 | /// The outer unwind destination is the target of unwind edges |
| 103 | /// introduced for calls within the inlined function. |
| 104 | BasicBlock *getOuterUnwindDest() const { |
| 105 | return OuterUnwindDest; |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 106 | } |
| 107 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 108 | EHSelectorInst *getOuterSelector() { |
| 109 | if (!OuterSelector) |
| 110 | OuterSelector = findSelectorForLandingPad(OuterUnwindDest); |
| 111 | return OuterSelector; |
| 112 | } |
| 113 | |
| 114 | BasicBlock *getInnerUnwindDest(); |
| 115 | |
| 116 | bool forwardEHResume(CallInst *call, BasicBlock *src); |
| 117 | |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 118 | /// Add incoming-PHI values to the unwind destination block for |
| 119 | /// the given basic block, using the values for the original |
| 120 | /// invoke's source block. |
| 121 | void addIncomingPHIValuesFor(BasicBlock *BB) const { |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 122 | addIncomingPHIValuesForInto(BB, OuterUnwindDest); |
| 123 | } |
| 124 | |
| 125 | void addIncomingPHIValuesForInto(BasicBlock *src, BasicBlock *dest) const { |
| 126 | BasicBlock::iterator I = dest->begin(); |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 127 | for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) { |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 128 | PHINode *phi = cast<PHINode>(I); |
| 129 | phi->addIncoming(UnwindDestPHIValues[i], src); |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | }; |
| 133 | } |
| 134 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 135 | /// Replace all the instruction uses of a value with a different value. |
| 136 | /// This has the advantage of not screwing up the CallGraph. |
| 137 | static void replaceAllInsnUsesWith(Instruction *insn, Value *replacement) { |
| 138 | for (Value::use_iterator i = insn->use_begin(), e = insn->use_end(); |
| 139 | i != e; ) { |
| 140 | Use &use = i.getUse(); |
| 141 | ++i; |
| 142 | if (isa<Instruction>(use.getUser())) |
| 143 | use.set(replacement); |
| 144 | } |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 145 | } |
| 146 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 147 | /// Get or create a target for the branch out of rewritten calls to |
| 148 | /// llvm.eh.resume. |
| 149 | BasicBlock *InvokeInliningInfo::getInnerUnwindDest() { |
| 150 | if (InnerUnwindDest) return InnerUnwindDest; |
| 151 | |
| 152 | // Find and hoist the llvm.eh.exception and llvm.eh.selector calls |
| 153 | // in the outer landing pad to immediately following the phis. |
| 154 | EHSelectorInst *selector = getOuterSelector(); |
| 155 | if (!selector) return 0; |
| 156 | |
| 157 | // The call to llvm.eh.exception *must* be in the landing pad. |
| 158 | Instruction *exn = cast<Instruction>(selector->getArgOperand(0)); |
| 159 | assert(exn->getParent() == OuterUnwindDest); |
| 160 | |
| 161 | // TODO: recognize when we've already done this, so that we don't |
| 162 | // get a linear number of these when inlining calls into lots of |
| 163 | // invokes with the same landing pad. |
| 164 | |
| 165 | // Do the hoisting. |
| 166 | Instruction *splitPoint = exn->getParent()->getFirstNonPHI(); |
| 167 | assert(splitPoint != selector && "selector-on-exception dominance broken!"); |
| 168 | if (splitPoint == exn) { |
| 169 | selector->removeFromParent(); |
| 170 | selector->insertAfter(exn); |
| 171 | splitPoint = selector->getNextNode(); |
| 172 | } else { |
| 173 | exn->moveBefore(splitPoint); |
| 174 | selector->moveBefore(splitPoint); |
| 175 | } |
| 176 | |
| 177 | // Split the landing pad. |
| 178 | InnerUnwindDest = OuterUnwindDest->splitBasicBlock(splitPoint, |
| 179 | OuterUnwindDest->getName() + ".body"); |
| 180 | |
| 181 | // The number of incoming edges we expect to the inner landing pad. |
| 182 | const unsigned phiCapacity = 2; |
| 183 | |
| 184 | // Create corresponding new phis for all the phis in the outer landing pad. |
| 185 | BasicBlock::iterator insertPoint = InnerUnwindDest->begin(); |
| 186 | BasicBlock::iterator I = OuterUnwindDest->begin(); |
| 187 | for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) { |
| 188 | PHINode *outerPhi = cast<PHINode>(I); |
| 189 | PHINode *innerPhi = PHINode::Create(outerPhi->getType(), phiCapacity, |
| 190 | outerPhi->getName() + ".lpad-body", |
| 191 | insertPoint); |
| 192 | innerPhi->addIncoming(outerPhi, OuterUnwindDest); |
| 193 | } |
| 194 | |
| 195 | // Create a phi for the exception value... |
| 196 | InnerExceptionPHI = PHINode::Create(exn->getType(), phiCapacity, |
| 197 | "exn.lpad-body", insertPoint); |
| 198 | replaceAllInsnUsesWith(exn, InnerExceptionPHI); |
| 199 | selector->setArgOperand(0, exn); // restore this use |
| 200 | InnerExceptionPHI->addIncoming(exn, OuterUnwindDest); |
| 201 | |
| 202 | // ...and the selector. |
| 203 | InnerSelectorPHI = PHINode::Create(selector->getType(), phiCapacity, |
| 204 | "selector.lpad-body", insertPoint); |
| 205 | replaceAllInsnUsesWith(selector, InnerSelectorPHI); |
| 206 | InnerSelectorPHI->addIncoming(selector, OuterUnwindDest); |
| 207 | |
| 208 | // All done. |
| 209 | return InnerUnwindDest; |
| 210 | } |
| 211 | |
| 212 | /// [LIBUNWIND] Try to forward the given call, which logically occurs |
| 213 | /// at the end of the given block, as a branch to the inner unwind |
| 214 | /// block. Returns true if the call was forwarded. |
| 215 | bool InvokeInliningInfo::forwardEHResume(CallInst *call, BasicBlock *src) { |
| 216 | Function *fn = dyn_cast<Function>(call->getCalledValue()); |
| 217 | if (!fn || fn->getName() != "llvm.eh.resume") |
| 218 | return false; |
| 219 | |
| 220 | // If this fails, maybe it should be a fatal error. |
| 221 | BasicBlock *dest = getInnerUnwindDest(); |
| 222 | if (!dest) return false; |
| 223 | |
| 224 | // Make a branch. |
| 225 | BranchInst::Create(dest, src); |
| 226 | |
| 227 | // Update the phis in the destination. They were inserted in an |
| 228 | // order which makes this work. |
| 229 | addIncomingPHIValuesForInto(src, dest); |
| 230 | InnerExceptionPHI->addIncoming(call->getArgOperand(0), src); |
| 231 | InnerSelectorPHI->addIncoming(call->getArgOperand(1), src); |
| 232 | |
| 233 | return true; |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /// [LIBUNWIND] Check whether this selector is "only cleanups": |
| 237 | /// call i32 @llvm.eh.selector(blah, blah, i32 0) |
| 238 | static bool isCleanupOnlySelector(EHSelectorInst *selector) { |
| 239 | if (selector->getNumArgOperands() != 3) return false; |
| 240 | ConstantInt *val = dyn_cast<ConstantInt>(selector->getArgOperand(2)); |
| 241 | return (val && val->isZero()); |
| 242 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 243 | |
| 244 | /// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into |
Eric Christopher | f61f89a | 2009-09-06 22:20:54 +0000 | [diff] [blame] | 245 | /// 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] | 246 | /// invokes. This function analyze BB to see if there are any calls, and if so, |
| 247 | /// 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] | 248 | /// nodes in that block with the values specified in InvokeDestPHIValues. |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 249 | /// |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 250 | /// Returns true to indicate that the next block should be skipped. |
| 251 | static bool HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB, |
| 252 | InvokeInliningInfo &Invoke) { |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 253 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 254 | Instruction *I = BBI++; |
| 255 | |
| 256 | // We only need to check for function calls: inlined invoke |
| 257 | // instructions require no special handling. |
| 258 | CallInst *CI = dyn_cast<CallInst>(I); |
| 259 | if (CI == 0) continue; |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 260 | |
| 261 | // LIBUNWIND: merge selector instructions. |
| 262 | if (EHSelectorInst *Inner = dyn_cast<EHSelectorInst>(CI)) { |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 263 | EHSelectorInst *Outer = Invoke.getOuterSelector(); |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 264 | if (!Outer) continue; |
| 265 | |
| 266 | bool innerIsOnlyCleanup = isCleanupOnlySelector(Inner); |
| 267 | bool outerIsOnlyCleanup = isCleanupOnlySelector(Outer); |
| 268 | |
| 269 | // If both selectors contain only cleanups, we don't need to do |
| 270 | // anything. TODO: this is really just a very specific instance |
| 271 | // of a much more general optimization. |
| 272 | if (innerIsOnlyCleanup && outerIsOnlyCleanup) continue; |
| 273 | |
| 274 | // Otherwise, we just append the outer selector to the inner selector. |
| 275 | SmallVector<Value*, 16> NewSelector; |
| 276 | for (unsigned i = 0, e = Inner->getNumArgOperands(); i != e; ++i) |
| 277 | NewSelector.push_back(Inner->getArgOperand(i)); |
| 278 | for (unsigned i = 2, e = Outer->getNumArgOperands(); i != e; ++i) |
| 279 | NewSelector.push_back(Outer->getArgOperand(i)); |
| 280 | |
| 281 | CallInst *NewInner = CallInst::Create(Inner->getCalledValue(), |
| 282 | NewSelector.begin(), |
| 283 | NewSelector.end(), |
| 284 | "", |
| 285 | Inner); |
| 286 | // No need to copy attributes, calling convention, etc. |
| 287 | NewInner->takeName(Inner); |
| 288 | Inner->replaceAllUsesWith(NewInner); |
| 289 | Inner->eraseFromParent(); |
| 290 | continue; |
| 291 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 292 | |
| 293 | // If this call cannot unwind, don't convert it to an invoke. |
| 294 | if (CI->doesNotThrow()) |
| 295 | continue; |
| 296 | |
| 297 | // Convert this function call into an invoke instruction. |
| 298 | // First, split the basic block. |
| 299 | BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 300 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 301 | // Delete the unconditional branch inserted by splitBasicBlock |
| 302 | BB->getInstList().pop_back(); |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 303 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 304 | // LIBUNWIND: If this is a call to @llvm.eh.resume, just branch |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 305 | // directly to the new landing pad. |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 306 | if (Invoke.forwardEHResume(CI, BB)) { |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 307 | // TODO: 'Split' is now unreachable; clean it up. |
| 308 | |
| 309 | // We want to leave the original call intact so that the call |
| 310 | // graph and other structures won't get misled. We also have to |
| 311 | // avoid processing the next block, or we'll iterate here forever. |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 312 | return true; |
| 313 | } |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 314 | |
| 315 | // Otherwise, create the new invoke instruction. |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 316 | ImmutableCallSite CS(CI); |
| 317 | SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end()); |
| 318 | InvokeInst *II = |
| 319 | InvokeInst::Create(CI->getCalledValue(), Split, |
| 320 | Invoke.getOuterUnwindDest(), |
| 321 | InvokeArgs.begin(), InvokeArgs.end(), |
| 322 | CI->getName(), BB); |
| 323 | II->setCallingConv(CI->getCallingConv()); |
| 324 | II->setAttributes(CI->getAttributes()); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 325 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 326 | // Make sure that anything using the call now uses the invoke! This also |
| 327 | // updates the CallGraph if present, because it uses a WeakVH. |
| 328 | CI->replaceAllUsesWith(II); |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 329 | |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 330 | Split->getInstList().pop_front(); // Delete the original call |
| 331 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 332 | // Update any PHI nodes in the exceptional block to indicate that |
| 333 | // there is now a new entry in them. |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 334 | Invoke.addIncomingPHIValuesFor(BB); |
John McCall | d7c1086 | 2011-05-28 07:45:59 +0000 | [diff] [blame^] | 335 | return false; |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 336 | } |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 337 | |
| 338 | return false; |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 342 | /// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls |
| 343 | /// in the body of the inlined function into invokes and turn unwind |
| 344 | /// instructions into branches to the invoke unwind dest. |
| 345 | /// |
Nick Lewycky | dac5c4b | 2009-02-03 04:34:40 +0000 | [diff] [blame] | 346 | /// II is the invoke instruction being inlined. FirstNewBlock is the first |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 347 | /// block of the inlined code (the last block is the end of the function), |
| 348 | /// and InlineCodeInfo is information about the code that got inlined. |
| 349 | static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, |
Chris Lattner | 81dfb38 | 2009-09-01 18:44:06 +0000 | [diff] [blame] | 350 | ClonedCodeInfo &InlinedCodeInfo) { |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 351 | BasicBlock *InvokeDest = II->getUnwindDest(); |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 352 | |
| 353 | Function *Caller = FirstNewBlock->getParent(); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 354 | |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 355 | // The inlined code is currently at the end of the function, scan from the |
| 356 | // 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] | 357 | // rewrite. If the code doesn't have calls or unwinds, we know there is |
| 358 | // nothing to rewrite. |
| 359 | if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) { |
| 360 | // Now that everything is happy, we have one final detail. The PHI nodes in |
| 361 | // the exception destination block still have entries due to the original |
| 362 | // invoke instruction. Eliminate these entries (which might even delete the |
| 363 | // PHI node) now. |
| 364 | InvokeDest->removePredecessor(II->getParent()); |
| 365 | return; |
| 366 | } |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 367 | |
| 368 | InvokeInliningInfo Invoke(II); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 370 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){ |
| 371 | if (InlinedCodeInfo.ContainsCalls) |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 372 | if (HandleCallsInBlockInlinedThroughInvoke(BB, Invoke)) { |
| 373 | // Honor a request to skip the next block. We don't need to |
| 374 | // consider UnwindInsts in this case either. |
| 375 | ++BB; |
| 376 | continue; |
| 377 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 379 | if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 380 | // An UnwindInst requires special handling when it gets inlined into an |
| 381 | // invoke site. Once this happens, we know that the unwind would cause |
| 382 | // a control transfer to the invoke exception destination, so we can |
| 383 | // transform it into a direct branch to the exception destination. |
| 384 | BranchInst::Create(InvokeDest, UI); |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 386 | // Delete the unwind instruction! |
| 387 | UI->eraseFromParent(); |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 389 | // Update any PHI nodes in the exceptional block to indicate that |
| 390 | // there is now a new entry in them. |
John McCall | a3de16b | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 391 | Invoke.addIncomingPHIValuesFor(BB); |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | // Now that everything is happy, we have one final detail. The PHI nodes in |
| 396 | // the exception destination block still have entries due to the original |
| 397 | // invoke instruction. Eliminate these entries (which might even delete the |
| 398 | // PHI node) now. |
| 399 | InvokeDest->removePredecessor(II->getParent()); |
| 400 | } |
| 401 | |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 402 | /// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee |
| 403 | /// into the caller, update the specified callgraph to reflect the changes we |
| 404 | /// 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] | 405 | /// some edges of the callgraph may remain. |
| 406 | static void UpdateCallGraphAfterInlining(CallSite CS, |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 407 | Function::iterator FirstNewBlock, |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 408 | ValueToValueMapTy &VMap, |
Chris Lattner | fe9af3b | 2010-04-22 23:37:35 +0000 | [diff] [blame] | 409 | InlineFunctionInfo &IFI) { |
| 410 | CallGraph &CG = *IFI.CG; |
Duncan Sands | d7b9851 | 2008-09-08 11:05:51 +0000 | [diff] [blame] | 411 | const Function *Caller = CS.getInstruction()->getParent()->getParent(); |
| 412 | const Function *Callee = CS.getCalledFunction(); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 413 | CallGraphNode *CalleeNode = CG[Callee]; |
| 414 | CallGraphNode *CallerNode = CG[Caller]; |
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 | // 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] | 417 | // 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] | 418 | CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end(); |
| 419 | |
| 420 | // Consider the case where CalleeNode == CallerNode. |
Gabor Greif | 1253298 | 2009-01-17 00:09:08 +0000 | [diff] [blame] | 421 | CallGraphNode::CalledFunctionsVector CallCache; |
Gabor Greif | c478e52 | 2009-01-15 18:40:09 +0000 | [diff] [blame] | 422 | if (CalleeNode == CallerNode) { |
| 423 | CallCache.assign(I, E); |
| 424 | I = CallCache.begin(); |
| 425 | E = CallCache.end(); |
| 426 | } |
| 427 | |
| 428 | for (; I != E; ++I) { |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 429 | const Value *OrigCall = I->first; |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 430 | |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 431 | ValueToValueMapTy::iterator VMI = VMap.find(OrigCall); |
Chris Lattner | 981418b | 2006-07-12 21:37:11 +0000 | [diff] [blame] | 432 | // Only copy the edge if the call was inlined! |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 433 | if (VMI == VMap.end() || VMI->second == 0) |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 434 | continue; |
| 435 | |
| 436 | // If the call was inlined, but then constant folded, there is no edge to |
| 437 | // add. Check for this case. |
Chris Lattner | b957a5e | 2010-04-22 21:31:00 +0000 | [diff] [blame] | 438 | Instruction *NewCall = dyn_cast<Instruction>(VMI->second); |
| 439 | if (NewCall == 0) continue; |
Chris Lattner | 0ca2f28 | 2010-05-01 01:26:13 +0000 | [diff] [blame] | 440 | |
| 441 | // Remember that this call site got inlined for the client of |
| 442 | // InlineFunction. |
| 443 | IFI.InlinedCalls.push_back(NewCall); |
| 444 | |
Chris Lattner | b957a5e | 2010-04-22 21:31:00 +0000 | [diff] [blame] | 445 | // It's possible that inlining the callsite will cause it to go from an |
| 446 | // indirect to a direct call by resolving a function pointer. If this |
| 447 | // happens, set the callee of the new call site to a more precise |
| 448 | // destination. This can also happen if the call graph node of the caller |
| 449 | // was just unnecessarily imprecise. |
| 450 | if (I->second->getFunction() == 0) |
| 451 | if (Function *F = CallSite(NewCall).getCalledFunction()) { |
| 452 | // Indirect call site resolved to direct call. |
Gabor Greif | 8609934 | 2010-07-27 15:02:37 +0000 | [diff] [blame] | 453 | CallerNode->addCalledFunction(CallSite(NewCall), CG[F]); |
| 454 | |
Chris Lattner | b957a5e | 2010-04-22 21:31:00 +0000 | [diff] [blame] | 455 | continue; |
| 456 | } |
Gabor Greif | 8609934 | 2010-07-27 15:02:37 +0000 | [diff] [blame] | 457 | |
| 458 | CallerNode->addCalledFunction(CallSite(NewCall), I->second); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 459 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 460 | |
Dale Johannesen | 39fa324 | 2009-01-13 22:43:37 +0000 | [diff] [blame] | 461 | // Update the call graph by deleting the edge from Callee to Caller. We must |
| 462 | // do this after the loop above in case Caller and Callee are the same. |
| 463 | CallerNode->removeCallEdgeFor(CS); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Chris Lattner | 0b66f63 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 466 | /// HandleByValArgument - When inlining a call site that has a byval argument, |
| 467 | /// we have to make the implicit memcpy explicit by adding it. |
Chris Lattner | e7ae705 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 468 | static Value *HandleByValArgument(Value *Arg, Instruction *TheCall, |
| 469 | const Function *CalledFunc, |
| 470 | InlineFunctionInfo &IFI, |
| 471 | unsigned ByValAlignment) { |
Chris Lattner | 0b66f63 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 472 | const Type *AggTy = cast<PointerType>(Arg->getType())->getElementType(); |
| 473 | |
| 474 | // If the called function is readonly, then it could not mutate the caller's |
| 475 | // copy of the byval'd memory. In this case, it is safe to elide the copy and |
| 476 | // temporary. |
| 477 | if (CalledFunc->onlyReadsMemory()) { |
| 478 | // If the byval argument has a specified alignment that is greater than the |
| 479 | // passed in pointer, then we either have to round up the input pointer or |
| 480 | // give up on this transformation. |
| 481 | if (ByValAlignment <= 1) // 0 = unspecified, 1 = no particular alignment. |
| 482 | return Arg; |
| 483 | |
Chris Lattner | 7569d79 | 2010-12-25 20:42:38 +0000 | [diff] [blame] | 484 | // If the pointer is already known to be sufficiently aligned, or if we can |
| 485 | // round it up to a larger alignment, then we don't need a temporary. |
| 486 | if (getOrEnforceKnownAlignment(Arg, ByValAlignment, |
| 487 | IFI.TD) >= ByValAlignment) |
| 488 | return Arg; |
Chris Lattner | 0b66f63 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 7569d79 | 2010-12-25 20:42:38 +0000 | [diff] [blame] | 490 | // Otherwise, we have to make a memcpy to get a safe alignment. This is bad |
| 491 | // for code quality, but rarely happens and is required for correctness. |
Chris Lattner | 0b66f63 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 492 | } |
Chris Lattner | e7ae705 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 493 | |
| 494 | LLVMContext &Context = Arg->getContext(); |
| 495 | |
Chris Lattner | e7ae705 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 496 | const Type *VoidPtrTy = Type::getInt8PtrTy(Context); |
| 497 | |
| 498 | // Create the alloca. If we have TargetData, use nice alignment. |
| 499 | unsigned Align = 1; |
| 500 | if (IFI.TD) |
| 501 | Align = IFI.TD->getPrefTypeAlignment(AggTy); |
| 502 | |
| 503 | // If the byval had an alignment specified, we *must* use at least that |
| 504 | // alignment, as it is required by the byval argument (and uses of the |
| 505 | // pointer inside the callee). |
| 506 | Align = std::max(Align, ByValAlignment); |
| 507 | |
| 508 | Function *Caller = TheCall->getParent()->getParent(); |
| 509 | |
| 510 | Value *NewAlloca = new AllocaInst(AggTy, 0, Align, Arg->getName(), |
| 511 | &*Caller->begin()->begin()); |
| 512 | // Emit a memcpy. |
| 513 | const Type *Tys[3] = {VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context)}; |
| 514 | Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(), |
| 515 | Intrinsic::memcpy, |
| 516 | Tys, 3); |
| 517 | Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall); |
| 518 | Value *SrcCast = new BitCastInst(Arg, VoidPtrTy, "tmp", TheCall); |
| 519 | |
| 520 | Value *Size; |
| 521 | if (IFI.TD == 0) |
| 522 | Size = ConstantExpr::getSizeOf(AggTy); |
| 523 | else |
| 524 | Size = ConstantInt::get(Type::getInt64Ty(Context), |
| 525 | IFI.TD->getTypeStoreSize(AggTy)); |
| 526 | |
| 527 | // Always generate a memcpy of alignment 1 here because we don't know |
| 528 | // the alignment of the src pointer. Other optimizations can infer |
| 529 | // better alignment. |
| 530 | Value *CallArgs[] = { |
| 531 | DestCast, SrcCast, Size, |
| 532 | ConstantInt::get(Type::getInt32Ty(Context), 1), |
| 533 | ConstantInt::getFalse(Context) // isVolatile |
| 534 | }; |
| 535 | CallInst *TheMemCpy = |
| 536 | CallInst::Create(MemCpyFn, CallArgs, CallArgs+5, "", TheCall); |
| 537 | |
| 538 | // If we have a call graph, update it. |
| 539 | if (CallGraph *CG = IFI.CG) { |
| 540 | CallGraphNode *MemCpyCGN = CG->getOrInsertFunction(MemCpyFn); |
| 541 | CallGraphNode *CallerNode = (*CG)[Caller]; |
| 542 | CallerNode->addCalledFunction(TheMemCpy, MemCpyCGN); |
| 543 | } |
| 544 | |
| 545 | // Uses of the argument in the function should use our new alloca |
| 546 | // instead. |
| 547 | return NewAlloca; |
| 548 | } |
| 549 | |
Nick Lewycky | 6d55f22 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 550 | // isUsedByLifetimeMarker - Check whether this Value is used by a lifetime |
| 551 | // intrinsic. |
| 552 | static bool isUsedByLifetimeMarker(Value *V) { |
| 553 | for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE; |
| 554 | ++UI) { |
| 555 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*UI)) { |
| 556 | switch (II->getIntrinsicID()) { |
| 557 | default: break; |
| 558 | case Intrinsic::lifetime_start: |
| 559 | case Intrinsic::lifetime_end: |
| 560 | return true; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | // hasLifetimeMarkers - Check whether the given alloca already has |
| 568 | // lifetime.start or lifetime.end intrinsics. |
| 569 | static bool hasLifetimeMarkers(AllocaInst *AI) { |
| 570 | const Type *Int8PtrTy = Type::getInt8PtrTy(AI->getType()->getContext()); |
| 571 | if (AI->getType() == Int8PtrTy) |
| 572 | return isUsedByLifetimeMarker(AI); |
| 573 | |
| 574 | // Do a scan to find all the bitcasts to i8*. |
| 575 | for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); I != E; |
| 576 | ++I) { |
| 577 | if (I->getType() != Int8PtrTy) continue; |
| 578 | if (!isa<BitCastInst>(*I)) continue; |
| 579 | if (isUsedByLifetimeMarker(*I)) |
| 580 | return true; |
| 581 | } |
| 582 | return false; |
| 583 | } |
| 584 | |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 585 | // InlineFunction - This function inlines the called function into the basic |
| 586 | // block of the caller. This returns false if it is not possible to inline this |
| 587 | // call. The program is still in a well defined state if this occurs though. |
| 588 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 589 | // Note that this only does one level of inlining. For example, if the |
| 590 | // 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] | 591 | // exists in the instruction stream. Similarly this will inline a recursive |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 592 | // function by one level. |
| 593 | // |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 594 | bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 595 | Instruction *TheCall = CS.getInstruction(); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 596 | LLVMContext &Context = TheCall->getContext(); |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 597 | assert(TheCall->getParent() && TheCall->getParent()->getParent() && |
| 598 | "Instruction not in function!"); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 600 | // If IFI has any state in it, zap it before we fill it in. |
| 601 | IFI.reset(); |
| 602 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 603 | const Function *CalledFunc = CS.getCalledFunction(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 604 | if (CalledFunc == 0 || // Can't inline external function or indirect |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 605 | CalledFunc->isDeclaration() || // call, or call to a vararg function! |
Eric Christopher | 0623e90 | 2010-03-24 23:35:21 +0000 | [diff] [blame] | 606 | CalledFunc->getFunctionType()->isVarArg()) return false; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 607 | |
Chris Lattner | af9985c | 2009-02-12 07:06:42 +0000 | [diff] [blame] | 608 | // 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] | 609 | // flags on any calls that we inline. |
| 610 | bool MustClearTailCallFlags = |
Chris Lattner | af9985c | 2009-02-12 07:06:42 +0000 | [diff] [blame] | 611 | !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall()); |
Chris Lattner | 1b49141 | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 612 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 613 | // If the call to the callee cannot throw, set the 'nounwind' flag on any |
| 614 | // calls that we inline. |
| 615 | bool MarkNoUnwind = CS.doesNotThrow(); |
| 616 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 617 | BasicBlock *OrigBB = TheCall->getParent(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 618 | Function *Caller = OrigBB->getParent(); |
| 619 | |
Gordon Henriksen | 0e13821 | 2007-12-25 03:10:07 +0000 | [diff] [blame] | 620 | // GC poses two hazards to inlining, which only occur when the callee has GC: |
| 621 | // 1. If the caller has no GC, then the callee's GC must be propagated to the |
| 622 | // caller. |
| 623 | // 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] | 624 | if (CalledFunc->hasGC()) { |
| 625 | if (!Caller->hasGC()) |
| 626 | Caller->setGC(CalledFunc->getGC()); |
| 627 | else if (CalledFunc->getGC() != Caller->getGC()) |
Gordon Henriksen | 0e13821 | 2007-12-25 03:10:07 +0000 | [diff] [blame] | 628 | return false; |
| 629 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 5052c91 | 2004-02-04 01:41:09 +0000 | [diff] [blame] | 631 | // Get an iterator to the last basic block in the function, which will have |
| 632 | // the new function inlined after it. |
| 633 | // |
| 634 | Function::iterator LastBlock = &Caller->back(); |
| 635 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 636 | // Make sure to capture all of the return instructions from the cloned |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 637 | // function. |
Chris Lattner | ec1bea0 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 638 | SmallVector<ReturnInst*, 8> Returns; |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 639 | ClonedCodeInfo InlinedFunctionInfo; |
Dale Johannesen | 0744f09 | 2009-03-04 02:09:48 +0000 | [diff] [blame] | 640 | Function::iterator FirstNewBlock; |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 641 | |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 642 | { // Scope to destroy VMap after cloning. |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 643 | ValueToValueMapTy VMap; |
Chris Lattner | 5b5bc30 | 2006-05-27 01:28:04 +0000 | [diff] [blame] | 644 | |
Dan Gohman | 9614fcc | 2008-06-20 17:11:32 +0000 | [diff] [blame] | 645 | assert(CalledFunc->arg_size() == CS.arg_size() && |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 646 | "No varargs calls can be inlined!"); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 647 | |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 648 | // Calculate the vector of arguments to pass into the function cloner, which |
| 649 | // matches up the formal to the actual argument values. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 650 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 651 | unsigned ArgNo = 0; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 652 | for (Function::const_arg_iterator I = CalledFunc->arg_begin(), |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 653 | E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) { |
| 654 | Value *ActualArg = *AI; |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 655 | |
Duncan Sands | d82375c | 2008-01-27 18:12:58 +0000 | [diff] [blame] | 656 | // When byval arguments actually inlined, we need to make the copy implied |
| 657 | // by them explicit. However, we don't do this if the callee is readonly |
| 658 | // or readnone, because the copy would be unneeded: the callee doesn't |
| 659 | // modify the struct. |
Chris Lattner | e7ae705 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 660 | if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal)) { |
| 661 | ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI, |
| 662 | CalledFunc->getParamAlignment(ArgNo+1)); |
| 663 | |
Duncan Sands | 2914ba6 | 2010-05-31 21:00:26 +0000 | [diff] [blame] | 664 | // 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] | 665 | // their 'tail' flags if HandleByValArgument introduced a new alloca and |
| 666 | // the callee has calls. |
| 667 | MustClearTailCallFlags |= ActualArg != *AI; |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 668 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 669 | |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 670 | VMap[I] = ActualArg; |
Chris Lattner | c93adca | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 671 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 5b5bc30 | 2006-05-27 01:28:04 +0000 | [diff] [blame] | 673 | // We want the inliner to prune the code as it copies. We would LOVE to |
| 674 | // have no dead or constant instructions leftover after inlining occurs |
| 675 | // (which can happen, e.g., because an argument was constant), but we'll be |
| 676 | // happy with whatever the cloner can do. |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 677 | CloneAndPruneFunctionInto(Caller, CalledFunc, VMap, |
| 678 | /*ModuleLevelChanges=*/false, Returns, ".i", |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 679 | &InlinedFunctionInfo, IFI.TD, TheCall); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 680 | |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 681 | // Remember the first block that is newly cloned over. |
| 682 | FirstNewBlock = LastBlock; ++FirstNewBlock; |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 683 | |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 684 | // Update the callgraph if requested. |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 685 | if (IFI.CG) |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 686 | UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 687 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 688 | |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 689 | // If there are any alloca instructions in the block that used to be the entry |
| 690 | // block for the callee, move them to the entry block of the caller. First |
| 691 | // calculate which instruction they should be inserted before. We insert the |
| 692 | // instructions at the end of the current alloca list. |
| 693 | // |
Chris Lattner | 21f2055 | 2006-01-13 18:16:48 +0000 | [diff] [blame] | 694 | { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 695 | BasicBlock::iterator InsertPoint = Caller->begin()->begin(); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 696 | for (BasicBlock::iterator I = FirstNewBlock->begin(), |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 697 | E = FirstNewBlock->end(); I != E; ) { |
| 698 | AllocaInst *AI = dyn_cast<AllocaInst>(I++); |
| 699 | if (AI == 0) continue; |
| 700 | |
| 701 | // If the alloca is now dead, remove it. This often occurs due to code |
| 702 | // specialization. |
| 703 | if (AI->use_empty()) { |
| 704 | AI->eraseFromParent(); |
| 705 | continue; |
Chris Lattner | 33bb3c8 | 2006-09-13 19:23:57 +0000 | [diff] [blame] | 706 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 707 | |
| 708 | if (!isa<Constant>(AI->getArraySize())) |
| 709 | continue; |
| 710 | |
Chris Lattner | 39add23 | 2010-12-06 07:43:04 +0000 | [diff] [blame] | 711 | // Keep track of the static allocas that we inline into the caller. |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 712 | IFI.StaticAllocas.push_back(AI); |
Chris Lattner | 8f2718f | 2009-08-27 04:20:52 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 714 | // Scan for the block of allocas that we can move over, and move them |
| 715 | // all at once. |
| 716 | while (isa<AllocaInst>(I) && |
Chris Lattner | 8f2718f | 2009-08-27 04:20:52 +0000 | [diff] [blame] | 717 | isa<Constant>(cast<AllocaInst>(I)->getArraySize())) { |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 718 | IFI.StaticAllocas.push_back(cast<AllocaInst>(I)); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 719 | ++I; |
Chris Lattner | 8f2718f | 2009-08-27 04:20:52 +0000 | [diff] [blame] | 720 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 721 | |
| 722 | // Transfer all of the allocas over in a block. Using splice means |
| 723 | // that the instructions aren't removed from the symbol table, then |
| 724 | // reinserted. |
| 725 | Caller->getEntryBlock().getInstList().splice(InsertPoint, |
| 726 | FirstNewBlock->getInstList(), |
| 727 | AI, I); |
| 728 | } |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 729 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 730 | |
Nick Lewycky | 6d55f22 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 731 | // Leave lifetime markers for the static alloca's, scoping them to the |
| 732 | // function we just inlined. |
| 733 | if (!IFI.StaticAllocas.empty()) { |
| 734 | // Also preserve the call graph, if applicable. |
| 735 | CallGraphNode *StartCGN = 0, *EndCGN = 0, *CallerNode = 0; |
| 736 | if (CallGraph *CG = IFI.CG) { |
| 737 | Function *Start = Intrinsic::getDeclaration(Caller->getParent(), |
| 738 | Intrinsic::lifetime_start); |
| 739 | Function *End = Intrinsic::getDeclaration(Caller->getParent(), |
| 740 | Intrinsic::lifetime_end); |
| 741 | StartCGN = CG->getOrInsertFunction(Start); |
| 742 | EndCGN = CG->getOrInsertFunction(End); |
| 743 | CallerNode = (*CG)[Caller]; |
| 744 | } |
| 745 | |
| 746 | IRBuilder<> builder(FirstNewBlock->begin()); |
| 747 | for (unsigned ai = 0, ae = IFI.StaticAllocas.size(); ai != ae; ++ai) { |
| 748 | AllocaInst *AI = IFI.StaticAllocas[ai]; |
| 749 | |
| 750 | // If the alloca is already scoped to something smaller than the whole |
| 751 | // function then there's no need to add redundant, less accurate markers. |
| 752 | if (hasLifetimeMarkers(AI)) |
| 753 | continue; |
| 754 | |
| 755 | CallInst *StartCall = builder.CreateLifetimeStart(AI); |
| 756 | if (IFI.CG) CallerNode->addCalledFunction(StartCall, StartCGN); |
| 757 | for (unsigned ri = 0, re = Returns.size(); ri != re; ++ri) { |
| 758 | IRBuilder<> builder(Returns[ri]); |
| 759 | CallInst *EndCall = builder.CreateLifetimeEnd(AI); |
| 760 | if (IFI.CG) CallerNode->addCalledFunction(EndCall, EndCGN); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 765 | // If the inlined code contained dynamic alloca instructions, wrap the inlined |
| 766 | // code with llvm.stacksave/llvm.stackrestore intrinsics. |
| 767 | if (InlinedFunctionInfo.ContainsDynamicAllocas) { |
| 768 | Module *M = Caller->getParent(); |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 769 | // Get the two intrinsics we care about. |
Chris Lattner | 6128df5 | 2009-10-17 05:39:39 +0000 | [diff] [blame] | 770 | Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave); |
| 771 | Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 772 | |
| 773 | // If we are preserving the callgraph, add edges to the stacksave/restore |
| 774 | // functions for the calls we insert. |
Chris Lattner | 21ba23d | 2006-07-18 21:48:57 +0000 | [diff] [blame] | 775 | CallGraphNode *StackSaveCGN = 0, *StackRestoreCGN = 0, *CallerNode = 0; |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 776 | if (CallGraph *CG = IFI.CG) { |
Chris Lattner | 6128df5 | 2009-10-17 05:39:39 +0000 | [diff] [blame] | 777 | StackSaveCGN = CG->getOrInsertFunction(StackSave); |
| 778 | StackRestoreCGN = CG->getOrInsertFunction(StackRestore); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 779 | CallerNode = (*CG)[Caller]; |
| 780 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 781 | |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 782 | // Insert the llvm.stacksave. |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 783 | CallInst *SavedPtr = CallInst::Create(StackSave, "savedstack", |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 784 | FirstNewBlock->begin()); |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 785 | if (IFI.CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 786 | |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 787 | // Insert a call to llvm.stackrestore before any return instructions in the |
| 788 | // inlined function. |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 789 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 790 | CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", Returns[i]); |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 791 | if (IFI.CG) CallerNode->addCalledFunction(CI, StackRestoreCGN); |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 792 | } |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 793 | |
| 794 | // Count the number of StackRestore calls we insert. |
| 795 | unsigned NumStackRestores = Returns.size(); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 796 | |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 797 | // If we are inlining an invoke instruction, insert restores before each |
| 798 | // unwind. These unwinds will be rewritten into branches later. |
| 799 | if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) { |
| 800 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 801 | BB != E; ++BB) |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 802 | if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
Chris Lattner | 6128df5 | 2009-10-17 05:39:39 +0000 | [diff] [blame] | 803 | CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", UI); |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 804 | if (IFI.CG) CallerNode->addCalledFunction(CI, StackRestoreCGN); |
Chris Lattner | 468fb1d | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 805 | ++NumStackRestores; |
| 806 | } |
| 807 | } |
Chris Lattner | bf229f4 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 810 | // 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] | 811 | // 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] | 812 | // code. Also, calls inlined through a 'nounwind' call site should be marked |
| 813 | // 'nounwind'. |
| 814 | if (InlinedFunctionInfo.ContainsCalls && |
| 815 | (MustClearTailCallFlags || MarkNoUnwind)) { |
Chris Lattner | 1b49141 | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 816 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 817 | BB != E; ++BB) |
| 818 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 819 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 820 | if (MustClearTailCallFlags) |
| 821 | CI->setTailCall(false); |
| 822 | if (MarkNoUnwind) |
| 823 | CI->setDoesNotThrow(); |
| 824 | } |
Chris Lattner | 1b49141 | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 827 | // If we are inlining through a 'nounwind' call site then any inlined 'unwind' |
| 828 | // instructions are unreachable. |
| 829 | if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind) |
| 830 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 831 | BB != E; ++BB) { |
| 832 | TerminatorInst *Term = BB->getTerminator(); |
| 833 | if (isa<UnwindInst>(Term)) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 834 | new UnreachableInst(Context, Term); |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 835 | BB->getInstList().erase(Term); |
| 836 | } |
| 837 | } |
| 838 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 839 | // If we are inlining for an invoke instruction, we must make sure to rewrite |
| 840 | // any inlined 'unwind' instructions into branches to the invoke exception |
| 841 | // destination, and call instructions into invoke instructions. |
Chris Lattner | cd4d339 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 842 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) |
Chris Lattner | 81dfb38 | 2009-09-01 18:44:06 +0000 | [diff] [blame] | 843 | HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 845 | // If we cloned in _exactly one_ basic block, and if that block ends in a |
| 846 | // return instruction, we splice the body of the inlined callee directly into |
| 847 | // the calling basic block. |
| 848 | if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) { |
| 849 | // Move all of the instructions right before the call. |
| 850 | OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(), |
| 851 | FirstNewBlock->begin(), FirstNewBlock->end()); |
| 852 | // Remove the cloned basic block. |
| 853 | Caller->getBasicBlockList().pop_back(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 855 | // If the call site was an invoke instruction, add a branch to the normal |
| 856 | // destination. |
| 857 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 858 | BranchInst::Create(II->getNormalDest(), TheCall); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 859 | |
| 860 | // If the return instruction returned a value, replace uses of the call with |
| 861 | // uses of the returned value. |
Devang Patel | dc00d42 | 2008-03-04 21:15:15 +0000 | [diff] [blame] | 862 | if (!TheCall->use_empty()) { |
| 863 | ReturnInst *R = Returns[0]; |
Eli Friedman | 5877ad7 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 864 | if (TheCall == R->getReturnValue()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 865 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Eli Friedman | 5877ad7 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 866 | else |
| 867 | TheCall->replaceAllUsesWith(R->getReturnValue()); |
Devang Patel | dc00d42 | 2008-03-04 21:15:15 +0000 | [diff] [blame] | 868 | } |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 869 | // 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] | 870 | TheCall->eraseFromParent(); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 871 | |
| 872 | // Since we are now done with the return instruction, delete it also. |
Dan Gohman | 1adec83 | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 873 | Returns[0]->eraseFromParent(); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 874 | |
| 875 | // We are now done with the inlining. |
| 876 | return true; |
| 877 | } |
| 878 | |
| 879 | // Otherwise, we have the normal case, of more than one block to inline or |
| 880 | // multiple return sites. |
| 881 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 882 | // We want to clone the entire callee function into the hole between the |
| 883 | // "starter" and "ender" blocks. How we accomplish this depends on whether |
| 884 | // this is an invoke instruction or a call instruction. |
| 885 | BasicBlock *AfterCallBB; |
| 886 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 888 | // Add an unconditional branch to make this look like the CallInst case... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 889 | BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 890 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 891 | // Split the basic block. This guarantees that no PHI nodes will have to be |
| 892 | // updated due to new incoming edges, and make the invoke case more |
| 893 | // symmetric to the call case. |
| 894 | AfterCallBB = OrigBB->splitBasicBlock(NewBr, |
Chris Lattner | 284d1b8 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 895 | CalledFunc->getName()+".exit"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 896 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 897 | } else { // It's a call |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 898 | // If this is a call instruction, we need to split the basic block that |
| 899 | // the call lives in. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 900 | // |
| 901 | AfterCallBB = OrigBB->splitBasicBlock(TheCall, |
Chris Lattner | 284d1b8 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 902 | CalledFunc->getName()+".exit"); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 905 | // Change the branch that used to go to AfterCallBB to branch to the first |
| 906 | // basic block of the inlined function. |
| 907 | // |
| 908 | TerminatorInst *Br = OrigBB->getTerminator(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 909 | assert(Br && Br->getOpcode() == Instruction::Br && |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 910 | "splitBasicBlock broken!"); |
| 911 | Br->setOperand(0, FirstNewBlock); |
| 912 | |
| 913 | |
| 914 | // Now that the function is correct, make it a little bit nicer. In |
| 915 | // particular, move the basic blocks inserted from the end of the function |
| 916 | // into the space made by splitting the source basic block. |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 917 | Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), |
| 918 | FirstNewBlock, Caller->end()); |
| 919 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 920 | // Handle all of the return instructions that we just cloned in, and eliminate |
| 921 | // any users of the original call/invoke instruction. |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 922 | const Type *RTy = CalledFunc->getReturnType(); |
Dan Gohman | 2c31750 | 2008-06-20 01:03:44 +0000 | [diff] [blame] | 923 | |
Duncan Sands | 6fb881c | 2010-11-17 11:16:23 +0000 | [diff] [blame] | 924 | PHINode *PHI = 0; |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 925 | if (Returns.size() > 1) { |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 926 | // The PHI node should go at the front of the new basic block to merge all |
| 927 | // possible incoming values. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 928 | if (!TheCall->use_empty()) { |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 929 | PHI = PHINode::Create(RTy, Returns.size(), TheCall->getName(), |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 930 | AfterCallBB->begin()); |
| 931 | // Anything that used the result of the function call should now use the |
| 932 | // PHI node as their operand. |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 933 | TheCall->replaceAllUsesWith(PHI); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 934 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 935 | |
Gabor Greif | c478e52 | 2009-01-15 18:40:09 +0000 | [diff] [blame] | 936 | // Loop over all of the return instructions adding entries to the PHI node |
| 937 | // as appropriate. |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 938 | if (PHI) { |
| 939 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 940 | ReturnInst *RI = Returns[i]; |
| 941 | assert(RI->getReturnValue()->getType() == PHI->getType() && |
| 942 | "Ret value not consistent in function!"); |
| 943 | PHI->addIncoming(RI->getReturnValue(), RI->getParent()); |
Devang Patel | 12a466b | 2008-03-07 20:06:16 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Chris Lattner | c581acb | 2009-10-27 05:39:41 +0000 | [diff] [blame] | 947 | |
Gabor Greif | de62aea | 2009-01-16 23:08:50 +0000 | [diff] [blame] | 948 | // Add a branch to the merge points and remove return instructions. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 949 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 950 | ReturnInst *RI = Returns[i]; |
Dale Johannesen | 0744f09 | 2009-03-04 02:09:48 +0000 | [diff] [blame] | 951 | BranchInst::Create(AfterCallBB, RI); |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 952 | RI->eraseFromParent(); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 953 | } |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 954 | } else if (!Returns.empty()) { |
| 955 | // Otherwise, if there is exactly one return value, just replace anything |
| 956 | // using the return value of the call with the computed value. |
Eli Friedman | 5877ad7 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 957 | if (!TheCall->use_empty()) { |
| 958 | if (TheCall == Returns[0]->getReturnValue()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 959 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Eli Friedman | 5877ad7 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 960 | else |
| 961 | TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); |
| 962 | } |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 963 | |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 964 | // Splice the code from the return block into the block that it will return |
| 965 | // to, which contains the code that was after the call. |
| 966 | BasicBlock *ReturnBB = Returns[0]->getParent(); |
| 967 | AfterCallBB->getInstList().splice(AfterCallBB->begin(), |
| 968 | ReturnBB->getInstList()); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 969 | |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 970 | // Update PHI nodes that use the ReturnBB to use the AfterCallBB. |
| 971 | ReturnBB->replaceAllUsesWith(AfterCallBB); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 972 | |
Devang Patel | b8f198a | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 973 | // Delete the return instruction now and empty ReturnBB now. |
| 974 | Returns[0]->eraseFromParent(); |
| 975 | ReturnBB->eraseFromParent(); |
Chris Lattner | 3787e76 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 976 | } else if (!TheCall->use_empty()) { |
| 977 | // No returns, but something is using the return value of the call. Just |
| 978 | // nuke the result. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 979 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 980 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 981 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 982 | // 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] | 983 | TheCall->eraseFromParent(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 985 | // We should always be able to fold the entry block of the function into the |
| 986 | // single predecessor of the block... |
Chris Lattner | cd01ae5 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 987 | assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 988 | BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 989 | |
Chris Lattner | cd01ae5 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 990 | // Splice the code entry block into calling block, right before the |
| 991 | // unconditional branch. |
| 992 | OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); |
| 993 | CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes |
| 994 | |
| 995 | // Remove the unconditional branch. |
| 996 | OrigBB->getInstList().erase(Br); |
| 997 | |
| 998 | // Now we can remove the CalleeEntry block, which is now empty. |
| 999 | Caller->getBasicBlockList().erase(CalleeEntry); |
Duncan Sands | a7212e5 | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 1000 | |
Duncan Sands | 6fb881c | 2010-11-17 11:16:23 +0000 | [diff] [blame] | 1001 | // If we inserted a phi node, check to see if it has a single value (e.g. all |
| 1002 | // the entries are the same or undef). If so, remove the PHI so it doesn't |
| 1003 | // block other optimizations. |
| 1004 | if (PHI) |
| 1005 | if (Value *V = SimplifyInstruction(PHI, IFI.TD)) { |
| 1006 | PHI->replaceAllUsesWith(V); |
| 1007 | PHI->eraseFromParent(); |
| 1008 | } |
| 1009 | |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 1010 | return true; |
| 1011 | } |