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