InlineFunction.cpp revision db125cfaf57cc83e7dd7453de2d509bc8efd0e5e
1ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===- InlineFunction.cpp - Code to perform function inlining -------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
10ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// This file implements inlining of a function into a call site, resolving
11ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// parameters and the return value as appropriate.
12ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
13a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// The code in this file for handling inlines through invoke
14a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// instructions preserves semantics only under some assumptions about
15a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// the behavior of unwinders which correspond to gcc-style libUnwind
16a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// exception personality functions.  Eventually the IR will be
17a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// improved to make this unnecessary, but until then, this code is
18a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// marked [LIBUNWIND].
19a3de16bc8f36638d5444e3e7b0112998af54f826John McCall//
20ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===----------------------------------------------------------------------===//
21ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
22ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/Transforms/Utils/Cloning.h"
233787e765facfad5ea62753922d940bcdd52afd57Chris Lattner#include "llvm/Constants.h"
247152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner#include "llvm/DerivedTypes.h"
25ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/Module.h"
2680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Instructions.h"
27517576d6f96a0acde9bab79553d89f4ceba20cf6Devang Patel#include "llvm/IntrinsicInst.h"
2880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Intrinsics.h"
29eaf42abab6d465c38891345d999255871cf03943Devang Patel#include "llvm/Attributes.h"
30468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner#include "llvm/Analysis/CallGraph.h"
31517576d6f96a0acde9bab79553d89f4ceba20cf6Devang Patel#include "llvm/Analysis/DebugInfo.h"
326fb881c036c32728c4a128d81b6083457e534e09Duncan Sands#include "llvm/Analysis/InstructionSimplify.h"
33c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner#include "llvm/Target/TargetData.h"
347569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner#include "llvm/Transforms/Utils/Local.h"
3593e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner#include "llvm/ADT/SmallVector.h"
36641ca93cff0f957fc5fb9dfb05d2a4a340aa8af7Devang Patel#include "llvm/ADT/StringExtras.h"
3780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Support/CallSite.h"
386d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky#include "llvm/Support/IRBuilder.h"
39f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm;
40ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
4160915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI) {
4260915146f4d35e12f10dcdaa155596fac79184daChris Lattner  return InlineFunction(CallSite(CI), IFI);
43468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
4460915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI) {
4560915146f4d35e12f10dcdaa155596fac79184daChris Lattner  return InlineFunction(CallSite(II), IFI);
46468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
4780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
481dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Look for an llvm.eh.exception call in the given block.
491dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHExceptionInst *findExceptionInBlock(BasicBlock *bb) {
501dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (BasicBlock::iterator i = bb->begin(), e = bb->end(); i != e; i++) {
51d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHExceptionInst *exn = dyn_cast<EHExceptionInst>(i);
521dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (exn) return exn;
531dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
541dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
551dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return 0;
561dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall}
571dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
581dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Look for the 'best' llvm.eh.selector instruction for
591dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// the given llvm.eh.exception call.
601dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHSelectorInst *findSelectorForException(EHExceptionInst *exn) {
611dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *exnBlock = exn->getParent();
62d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
631dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *outOfBlockSelector = 0;
641dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (Instruction::use_iterator
651dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall         ui = exn->use_begin(), ue = exn->use_end(); ui != ue; ++ui) {
661dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    EHSelectorInst *sel = dyn_cast<EHSelectorInst>(*ui);
671dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!sel) continue;
68d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
691dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Immediately accept an eh.selector in the same block as the
701dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // excepton call.
711dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (sel->getParent() == exnBlock) return sel;
72d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
731dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Otherwise, use the first selector we see.
741dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!outOfBlockSelector) outOfBlockSelector = sel;
751dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
761dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
771dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return outOfBlockSelector;
781dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall}
791dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
801dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Find the (possibly absent) call to @llvm.eh.selector
811dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// in the given landing pad.  In principle, llvm.eh.exception is
821dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// required to be in the landing pad; in practice, SplitCriticalEdge
831dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// can break that invariant, and then inlining can break it further.
841dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// There's a real need for a reliable solution here, but until that
851dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// happens, we have some fragile workarounds here.
861dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHSelectorInst *findSelectorForLandingPad(BasicBlock *lpad) {
871dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Look for an exception call in the actual landing pad.
881dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHExceptionInst *exn = findExceptionInBlock(lpad);
891dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (exn) return findSelectorForException(exn);
901dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
911dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Okay, if that failed, look for one in an obvious successor.  If
921dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // we find one, we'll fix the IR by moving things back to the
931dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // landing pad.
941dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
951dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  bool dominates = true; // does the lpad dominate the exn call
961dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *nonDominated = 0; // if not, the first non-dominated block
971dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *lastDominated = 0; // and the block which branched to it
981dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
991dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *exnBlock = lpad;
1001dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1011dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // We need to protect against lpads that lead into infinite loops.
1021dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  SmallPtrSet<BasicBlock*,4> visited;
1031dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  visited.insert(exnBlock);
1041dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1051dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  do {
1061dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // We're not going to apply this hack to anything more complicated
1071dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // than a series of unconditional branches, so if the block
1081dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // doesn't terminate in an unconditional branch, just fail.  More
1091dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // complicated cases can arise when, say, sinking a call into a
1101dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // split unwind edge and then inlining it; but that can do almost
1111dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // *anything* to the CFG, including leaving the selector
1121dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // completely unreachable.  The only way to fix that properly is
1131dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // to (1) prohibit transforms which move the exception or selector
1141dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // values away from the landing pad, e.g. by producing them with
1151dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // instructions that are pinned to an edge like a phi, or
1161dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // producing them with not-really-instructions, and (2) making
1171dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // transforms which split edges deal with that.
1181dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    BranchInst *branch = dyn_cast<BranchInst>(&exnBlock->back());
1191dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!branch || branch->isConditional()) return 0;
1201dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1211dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    BasicBlock *successor = branch->getSuccessor(0);
1221dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1231dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Fail if we found an infinite loop.
1241dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!visited.insert(successor)) return 0;
1251dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1261dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // If the successor isn't dominated by exnBlock:
1271dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!successor->getSinglePredecessor()) {
1281dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // We don't want to have to deal with threading the exception
1291dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // through multiple levels of phi, so give up if we've already
1301dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // followed a non-dominating edge.
1311dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      if (!dominates) return 0;
1321dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1331dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // Otherwise, remember this as a non-dominating edge.
1341dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      dominates = false;
1351dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      nonDominated = successor;
1361dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      lastDominated = exnBlock;
137d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
138d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
1391dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exnBlock = successor;
1401dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1411dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Can we stop here?
1421dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exn = findExceptionInBlock(exnBlock);
1431dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  } while (!exn);
1441dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1451dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Look for a selector call for the exception we found.
1461dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *selector = findSelectorForException(exn);
1471dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (!selector) return 0;
1481dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1491dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // The easy case is when the landing pad still dominates the
1501dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // exception call, in which case we can just move both calls back to
1511dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // the landing pad.
1521dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (dominates) {
1531dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    selector->moveBefore(lpad->getFirstNonPHI());
1541dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exn->moveBefore(selector);
155d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return selector;
156d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
157d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
1581dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Otherwise, we have to split at the first non-dominating block.
1591dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // The CFG looks basically like this:
1601dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    lpad:
1611dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_0
1621dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_1
1631dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %nonDominated
1641dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    nonDominated:
1651dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2
1661dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insns_3
1671dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn = call i8* @llvm.eh.exception()
1681dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_4
1691dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector = call @llvm.eh.selector(i8* %exn, ...
1701dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // We need to turn this into:
1711dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    lpad:
1721dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_0
1731dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn0 = call i8* @llvm.eh.exception()
1741dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector0 = call @llvm.eh.selector(i8* %exn0, ...
1751dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_1
1761dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %split // from lastDominated
1771dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    nonDominated:
1781dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2 (without edge from lastDominated)
1791dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn1 = call i8* @llvm.eh.exception()
1801dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector1 = call i8* @llvm.eh.selector(i8* %exn1, ...
1811dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %split
1821dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    split:
1831dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2 (edge from lastDominated, edge from split)
1841dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn = phi ...
1851dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector = phi ...
1861dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insns_3
1871dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_4
1881dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1891dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  assert(nonDominated);
1901dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  assert(lastDominated);
1911dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1921dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // First, make clones of the intrinsics to go in lpad.
1931dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHExceptionInst *lpadExn = cast<EHExceptionInst>(exn->clone());
1941dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *lpadSelector = cast<EHSelectorInst>(selector->clone());
1951dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadSelector->setArgOperand(0, lpadExn);
1961dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadSelector->insertBefore(lpad->getFirstNonPHI());
1971dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadExn->insertBefore(lpadSelector);
1981dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1991dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Split the non-dominated block.
2001dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *split =
2011dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    nonDominated->splitBasicBlock(nonDominated->getFirstNonPHI(),
2021dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                                  nonDominated->getName() + ".lpad-fix");
2031dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2041dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Redirect the last dominated branch there.
2051dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  cast<BranchInst>(lastDominated->back()).setSuccessor(0, split);
2061dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2071dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Move the existing intrinsics to the end of the old block.
2081dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->moveBefore(&nonDominated->back());
2091dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exn->moveBefore(selector);
2101dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2111dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  Instruction *splitIP = &split->front();
2121dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2131dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // For all the phis in nonDominated, make a new phi in split to join
2141dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // that phi with the edge from lastDominated.
2151dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (BasicBlock::iterator
2161dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall         i = nonDominated->begin(), e = nonDominated->end(); i != e; ++i) {
2171dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    PHINode *phi = dyn_cast<PHINode>(i);
2181dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!phi) break;
2191dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2201dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    PHINode *splitPhi = PHINode::Create(phi->getType(), 2, phi->getName(),
2211dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                                        splitIP);
2221dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    phi->replaceAllUsesWith(splitPhi);
2231dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    splitPhi->addIncoming(phi, nonDominated);
2241dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    splitPhi->addIncoming(phi->removeIncomingValue(lastDominated),
2251dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                          lastDominated);
2261dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
2271dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2281dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Make new phis for the exception and selector.
2291dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  PHINode *exnPhi = PHINode::Create(exn->getType(), 2, "", splitIP);
2301dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exn->replaceAllUsesWith(exnPhi);
2311dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->setArgOperand(0, exn); // except for this use
2321dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exnPhi->addIncoming(exn, nonDominated);
2331dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exnPhi->addIncoming(lpadExn, lastDominated);
2341dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2351dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  PHINode *selectorPhi = PHINode::Create(selector->getType(), 2, "", splitIP);
2361dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->replaceAllUsesWith(selectorPhi);
2371dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selectorPhi->addIncoming(selector, nonDominated);
2381dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selectorPhi->addIncoming(lpadSelector, lastDominated);
2391dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2401dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return lpadSelector;
241d7c10862016939c9850cadfe5e1c35513c0adf28John McCall}
242d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
243a3de16bc8f36638d5444e3e7b0112998af54f826John McCallnamespace {
244a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  /// A class for recording information about inlining through an invoke.
245a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  class InvokeInliningInfo {
246d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *OuterUnwindDest;
247d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHSelectorInst *OuterSelector;
248d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *InnerUnwindDest;
249d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *InnerExceptionPHI;
250d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *InnerSelectorPHI;
251a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    SmallVector<Value*, 8> UnwindDestPHIValues;
252a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
253a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  public:
254d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    InvokeInliningInfo(InvokeInst *II) :
255d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      OuterUnwindDest(II->getUnwindDest()), OuterSelector(0),
256d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      InnerUnwindDest(0), InnerExceptionPHI(0), InnerSelectorPHI(0) {
257d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
258a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // If there are PHI nodes in the unwind destination block, we
259a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // need to keep track of which values came into them from the
260a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // invoke before removing the edge from this block.
261d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      llvm::BasicBlock *invokeBB = II->getParent();
262d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      for (BasicBlock::iterator I = OuterUnwindDest->begin();
263d7c10862016939c9850cadfe5e1c35513c0adf28John McCall             isa<PHINode>(I); ++I) {
264a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // Save the value to use for this edge.
265d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        PHINode *phi = cast<PHINode>(I);
266d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        UnwindDestPHIValues.push_back(phi->getIncomingValueForBlock(invokeBB));
267a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      }
268a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
269a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
270d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    /// The outer unwind destination is the target of unwind edges
271d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    /// introduced for calls within the inlined function.
272d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *getOuterUnwindDest() const {
273d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      return OuterUnwindDest;
274a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
275a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
276d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHSelectorInst *getOuterSelector() {
277d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      if (!OuterSelector)
278d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        OuterSelector = findSelectorForLandingPad(OuterUnwindDest);
279d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      return OuterSelector;
280d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
281d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
282d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *getInnerUnwindDest();
283d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
284d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    bool forwardEHResume(CallInst *call, BasicBlock *src);
285d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
286a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    /// Add incoming-PHI values to the unwind destination block for
287a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    /// the given basic block, using the values for the original
288a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    /// invoke's source block.
289a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    void addIncomingPHIValuesFor(BasicBlock *BB) const {
290d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      addIncomingPHIValuesForInto(BB, OuterUnwindDest);
291d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
292d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
293d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    void addIncomingPHIValuesForInto(BasicBlock *src, BasicBlock *dest) const {
294d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      BasicBlock::iterator I = dest->begin();
295a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
296d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        PHINode *phi = cast<PHINode>(I);
297d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        phi->addIncoming(UnwindDestPHIValues[i], src);
298a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      }
299a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
300a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  };
301a3de16bc8f36638d5444e3e7b0112998af54f826John McCall}
302a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
303d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// Get or create a target for the branch out of rewritten calls to
304d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// llvm.eh.resume.
305d7c10862016939c9850cadfe5e1c35513c0adf28John McCallBasicBlock *InvokeInliningInfo::getInnerUnwindDest() {
306d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (InnerUnwindDest) return InnerUnwindDest;
307d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
308d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Find and hoist the llvm.eh.exception and llvm.eh.selector calls
309d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // in the outer landing pad to immediately following the phis.
310d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  EHSelectorInst *selector = getOuterSelector();
311d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (!selector) return 0;
312d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
313d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // The call to llvm.eh.exception *must* be in the landing pad.
314d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Instruction *exn = cast<Instruction>(selector->getArgOperand(0));
315d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  assert(exn->getParent() == OuterUnwindDest);
316d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
317d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // TODO: recognize when we've already done this, so that we don't
318d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // get a linear number of these when inlining calls into lots of
319d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // invokes with the same landing pad.
320d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
321d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Do the hoisting.
322d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Instruction *splitPoint = exn->getParent()->getFirstNonPHI();
323d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  assert(splitPoint != selector && "selector-on-exception dominance broken!");
324d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (splitPoint == exn) {
325d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->removeFromParent();
326d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->insertAfter(exn);
327d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    splitPoint = selector->getNextNode();
328d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  } else {
329d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    exn->moveBefore(splitPoint);
330d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->moveBefore(splitPoint);
331d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
332d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
333d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Split the landing pad.
334d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerUnwindDest = OuterUnwindDest->splitBasicBlock(splitPoint,
335d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        OuterUnwindDest->getName() + ".body");
336d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
337d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // The number of incoming edges we expect to the inner landing pad.
338d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  const unsigned phiCapacity = 2;
339d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
340d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Create corresponding new phis for all the phis in the outer landing pad.
341d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock::iterator insertPoint = InnerUnwindDest->begin();
342d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock::iterator I = OuterUnwindDest->begin();
343d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
344d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *outerPhi = cast<PHINode>(I);
345d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *innerPhi = PHINode::Create(outerPhi->getType(), phiCapacity,
346d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        outerPhi->getName() + ".lpad-body",
347d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        insertPoint);
348a6d7345ec91e4b07671f62d231e7c42f054bc70dJohn McCall    outerPhi->replaceAllUsesWith(innerPhi);
349d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    innerPhi->addIncoming(outerPhi, OuterUnwindDest);
350d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
351d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
352d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Create a phi for the exception value...
353d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerExceptionPHI = PHINode::Create(exn->getType(), phiCapacity,
354d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                      "exn.lpad-body", insertPoint);
355e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall  exn->replaceAllUsesWith(InnerExceptionPHI);
356d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  selector->setArgOperand(0, exn); // restore this use
357d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerExceptionPHI->addIncoming(exn, OuterUnwindDest);
358d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
359d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // ...and the selector.
360d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerSelectorPHI = PHINode::Create(selector->getType(), phiCapacity,
361d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                     "selector.lpad-body", insertPoint);
362e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall  selector->replaceAllUsesWith(InnerSelectorPHI);
363d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerSelectorPHI->addIncoming(selector, OuterUnwindDest);
364d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
365d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // All done.
366d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  return InnerUnwindDest;
367d7c10862016939c9850cadfe5e1c35513c0adf28John McCall}
368d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
369d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// [LIBUNWIND] Try to forward the given call, which logically occurs
370d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// at the end of the given block, as a branch to the inner unwind
371d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// block.  Returns true if the call was forwarded.
372d7c10862016939c9850cadfe5e1c35513c0adf28John McCallbool InvokeInliningInfo::forwardEHResume(CallInst *call, BasicBlock *src) {
3731edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // First, check whether this is a call to the intrinsic.
374d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Function *fn = dyn_cast<Function>(call->getCalledValue());
375d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (!fn || fn->getName() != "llvm.eh.resume")
376d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return false;
3771edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
3781edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // At this point, we need to return true on all paths, because
3791edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // otherwise we'll construct an invoke of the intrinsic, which is
3801edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // not well-formed.
381d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
3821edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // Try to find or make an inner unwind dest, which will fail if we
3831edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // can't find a selector call for the outer unwind dest.
384d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock *dest = getInnerUnwindDest();
3851edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  bool hasSelector = (dest != 0);
3861edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
3871edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // If we failed, just use the outer unwind dest, dropping the
3881edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // exception and selector on the floor.
3891edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  if (!hasSelector)
3901edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    dest = OuterUnwindDest;
391d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
392d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Make a branch.
393d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BranchInst::Create(dest, src);
394d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
395d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Update the phis in the destination.  They were inserted in an
396d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // order which makes this work.
397d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  addIncomingPHIValuesForInto(src, dest);
3981edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
3991edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  if (hasSelector) {
4001edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    InnerExceptionPHI->addIncoming(call->getArgOperand(0), src);
4011edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    InnerSelectorPHI->addIncoming(call->getArgOperand(1), src);
4021edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  }
403d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
404d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  return true;
405a3de16bc8f36638d5444e3e7b0112998af54f826John McCall}
406a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
407a3de16bc8f36638d5444e3e7b0112998af54f826John McCall/// [LIBUNWIND] Check whether this selector is "only cleanups":
408a3de16bc8f36638d5444e3e7b0112998af54f826John McCall///   call i32 @llvm.eh.selector(blah, blah, i32 0)
409a3de16bc8f36638d5444e3e7b0112998af54f826John McCallstatic bool isCleanupOnlySelector(EHSelectorInst *selector) {
410a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  if (selector->getNumArgOperands() != 3) return false;
411a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  ConstantInt *val = dyn_cast<ConstantInt>(selector->getArgOperand(2));
412a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  return (val && val->isZero());
413a3de16bc8f36638d5444e3e7b0112998af54f826John McCall}
414135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
415135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into
416f61f89ae14cf332a014a598153137113af34002fEric Christopher/// an invoke, we have to turn all of the calls that can throw into
417135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// invokes.  This function analyze BB to see if there are any calls, and if so,
418135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI
41981dfb3885252fbf621b080827a080099864415f8Chris Lattner/// nodes in that block with the values specified in InvokeDestPHIValues.
420135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner///
421a3de16bc8f36638d5444e3e7b0112998af54f826John McCall/// Returns true to indicate that the next block should be skipped.
422a3de16bc8f36638d5444e3e7b0112998af54f826John McCallstatic bool HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
423a3de16bc8f36638d5444e3e7b0112998af54f826John McCall                                                   InvokeInliningInfo &Invoke) {
424135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
425135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    Instruction *I = BBI++;
426135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
427135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // We only need to check for function calls: inlined invoke
428135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // instructions require no special handling.
429135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    CallInst *CI = dyn_cast<CallInst>(I);
430135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (CI == 0) continue;
431a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
432a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    // LIBUNWIND: merge selector instructions.
433a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    if (EHSelectorInst *Inner = dyn_cast<EHSelectorInst>(CI)) {
434d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      EHSelectorInst *Outer = Invoke.getOuterSelector();
435a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      if (!Outer) continue;
436a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
437a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      bool innerIsOnlyCleanup = isCleanupOnlySelector(Inner);
438a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      bool outerIsOnlyCleanup = isCleanupOnlySelector(Outer);
439a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
440a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // If both selectors contain only cleanups, we don't need to do
441a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // anything.  TODO: this is really just a very specific instance
442a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // of a much more general optimization.
443a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      if (innerIsOnlyCleanup && outerIsOnlyCleanup) continue;
444a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
445a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // Otherwise, we just append the outer selector to the inner selector.
446a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      SmallVector<Value*, 16> NewSelector;
447a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      for (unsigned i = 0, e = Inner->getNumArgOperands(); i != e; ++i)
448a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        NewSelector.push_back(Inner->getArgOperand(i));
449a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      for (unsigned i = 2, e = Outer->getNumArgOperands(); i != e; ++i)
450a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        NewSelector.push_back(Outer->getArgOperand(i));
451a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
452c975a51ac042eb15bcb04a293cb737810ff40a00John McCall      CallInst *NewInner =
453a3efbb15ddd5aa9006564cd79086723640084878Jay Foad        IRBuilder<>(Inner).CreateCall(Inner->getCalledValue(), NewSelector);
454a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // No need to copy attributes, calling convention, etc.
455a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      NewInner->takeName(Inner);
456a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      Inner->replaceAllUsesWith(NewInner);
457a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      Inner->eraseFromParent();
458a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      continue;
459a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
460135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
461135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // If this call cannot unwind, don't convert it to an invoke.
462135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (CI->doesNotThrow())
463135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
464135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
465135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Convert this function call into an invoke instruction.
466135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // First, split the basic block.
467135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
468a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
469d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // Delete the unconditional branch inserted by splitBasicBlock
470d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BB->getInstList().pop_back();
471a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
472d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // LIBUNWIND: If this is a call to @llvm.eh.resume, just branch
473a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    // directly to the new landing pad.
474d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    if (Invoke.forwardEHResume(CI, BB)) {
475a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // TODO: 'Split' is now unreachable; clean it up.
476a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
477a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // We want to leave the original call intact so that the call
478a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // graph and other structures won't get misled.  We also have to
479a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // avoid processing the next block, or we'll iterate here forever.
480d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      return true;
481d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
482a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
483a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    // Otherwise, create the new invoke instruction.
484d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    ImmutableCallSite CS(CI);
485d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end());
486d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    InvokeInst *II =
487d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      InvokeInst::Create(CI->getCalledValue(), Split,
488d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                         Invoke.getOuterUnwindDest(),
489a3efbb15ddd5aa9006564cd79086723640084878Jay Foad                         InvokeArgs, CI->getName(), BB);
490d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    II->setCallingConv(CI->getCallingConv());
491d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    II->setAttributes(CI->getAttributes());
492135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
493d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // Make sure that anything using the call now uses the invoke!  This also
494d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // updates the CallGraph if present, because it uses a WeakVH.
495d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    CI->replaceAllUsesWith(II);
496d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
497d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    Split->getInstList().pop_front();  // Delete the original call
498a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
499135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Update any PHI nodes in the exceptional block to indicate that
500135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // there is now a new entry in them.
501a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    Invoke.addIncomingPHIValuesFor(BB);
502d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return false;
503135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  }
504a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
505a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  return false;
506135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner}
507135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
508135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
509cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
510cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// in the body of the inlined function into invokes and turn unwind
511cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// instructions into branches to the invoke unwind dest.
512cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner///
513dac5c4b10b387b55c2394cd98a64f3f1394df2e8Nick Lewycky/// II is the invoke instruction being inlined.  FirstNewBlock is the first
514cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// block of the inlined code (the last block is the end of the function),
515cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// and InlineCodeInfo is information about the code that got inlined.
516cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattnerstatic void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
51781dfb3885252fbf621b080827a080099864415f8Chris Lattner                                ClonedCodeInfo &InlinedCodeInfo) {
518cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  BasicBlock *InvokeDest = II->getUnwindDest();
519cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
520cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  Function *Caller = FirstNewBlock->getParent();
521a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
522cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // The inlined code is currently at the end of the function, scan from the
523cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // start of the inlined code to its end, checking for stuff we need to
524135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // rewrite.  If the code doesn't have calls or unwinds, we know there is
525135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // nothing to rewrite.
526135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) {
527135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Now that everything is happy, we have one final detail.  The PHI nodes in
528135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // the exception destination block still have entries due to the original
529135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // invoke instruction.  Eliminate these entries (which might even delete the
530135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // PHI node) now.
531135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    InvokeDest->removePredecessor(II->getParent());
532135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    return;
533135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  }
534a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
535a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  InvokeInliningInfo Invoke(II);
536135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
537135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){
538135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (InlinedCodeInfo.ContainsCalls)
539a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      if (HandleCallsInBlockInlinedThroughInvoke(BB, Invoke)) {
540a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // Honor a request to skip the next block.  We don't need to
541a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // consider UnwindInsts in this case either.
542a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        ++BB;
543a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        continue;
544a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      }
545135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
546135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
547135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // An UnwindInst requires special handling when it gets inlined into an
548135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // invoke site.  Once this happens, we know that the unwind would cause
549135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // a control transfer to the invoke exception destination, so we can
550135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // transform it into a direct branch to the exception destination.
551135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      BranchInst::Create(InvokeDest, UI);
552135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
553135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Delete the unwind instruction!
554135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      UI->eraseFromParent();
555135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
556135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Update any PHI nodes in the exceptional block to indicate that
557135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // there is now a new entry in them.
558a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      Invoke.addIncomingPHIValuesFor(BB);
559cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    }
560cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  }
561cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
562cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // Now that everything is happy, we have one final detail.  The PHI nodes in
563cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // the exception destination block still have entries due to the original
564cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // invoke instruction.  Eliminate these entries (which might even delete the
565cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // PHI node) now.
566cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  InvokeDest->removePredecessor(II->getParent());
567cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner}
568cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
569d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee
570d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// into the caller, update the specified callgraph to reflect the changes we
571d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// made.  Note that it's possible that not all code was copied over, so only
572d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands/// some edges of the callgraph may remain.
573d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sandsstatic void UpdateCallGraphAfterInlining(CallSite CS,
574d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner                                         Function::iterator FirstNewBlock,
5751ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola                                         ValueToValueMapTy &VMap,
576fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner                                         InlineFunctionInfo &IFI) {
577fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner  CallGraph &CG = *IFI.CG;
578d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands  const Function *Caller = CS.getInstruction()->getParent()->getParent();
579d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands  const Function *Callee = CS.getCalledFunction();
580468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallGraphNode *CalleeNode = CG[Callee];
581468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallGraphNode *CallerNode = CG[Caller];
582a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
583d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  // Since we inlined some uninlined call sites in the callee into the caller,
584468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  // add edges from the caller to all of the callees of the callee.
585c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
586c478e52bf4c12691037856ee103c66946afeab6cGabor Greif
587c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  // Consider the case where CalleeNode == CallerNode.
588125329891f97baedef21e4b464ba70182c3fb45eGabor Greif  CallGraphNode::CalledFunctionsVector CallCache;
589c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  if (CalleeNode == CallerNode) {
590c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    CallCache.assign(I, E);
591c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    I = CallCache.begin();
592c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    E = CallCache.end();
593c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  }
594c478e52bf4c12691037856ee103c66946afeab6cGabor Greif
595c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  for (; I != E; ++I) {
596a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner    const Value *OrigCall = I->first;
597a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
5981ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola    ValueToValueMapTy::iterator VMI = VMap.find(OrigCall);
599981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner    // Only copy the edge if the call was inlined!
60029d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel    if (VMI == VMap.end() || VMI->second == 0)
601135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
602135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
603135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // If the call was inlined, but then constant folded, there is no edge to
604135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // add.  Check for this case.
605b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    Instruction *NewCall = dyn_cast<Instruction>(VMI->second);
606b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    if (NewCall == 0) continue;
6070ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner
6080ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    // Remember that this call site got inlined for the client of
6090ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    // InlineFunction.
6100ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    IFI.InlinedCalls.push_back(NewCall);
6110ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner
612b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // It's possible that inlining the callsite will cause it to go from an
613b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // indirect to a direct call by resolving a function pointer.  If this
614b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // happens, set the callee of the new call site to a more precise
615b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // destination.  This can also happen if the call graph node of the caller
616b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // was just unnecessarily imprecise.
617b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    if (I->second->getFunction() == 0)
618b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner      if (Function *F = CallSite(NewCall).getCalledFunction()) {
619b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner        // Indirect call site resolved to direct call.
62086099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif        CallerNode->addCalledFunction(CallSite(NewCall), CG[F]);
62186099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif
622b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner        continue;
623b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner      }
62486099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif
62586099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif    CallerNode->addCalledFunction(CallSite(NewCall), I->second);
626d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  }
627135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
62839fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  // Update the call graph by deleting the edge from Callee to Caller.  We must
62939fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  // do this after the loop above in case Caller and Callee are the same.
63039fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  CallerNode->removeCallEdgeFor(CS);
631468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
632468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
6330b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// HandleByValArgument - When inlining a call site that has a byval argument,
6340b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// we have to make the implicit memcpy explicit by adding it.
635e7ae705c32906979a527926864345016e76867b9Chris Lattnerstatic Value *HandleByValArgument(Value *Arg, Instruction *TheCall,
636e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  const Function *CalledFunc,
637e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  InlineFunctionInfo &IFI,
638e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  unsigned ByValAlignment) {
639db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *AggTy = cast<PointerType>(Arg->getType())->getElementType();
6400b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6410b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // If the called function is readonly, then it could not mutate the caller's
6420b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // copy of the byval'd memory.  In this case, it is safe to elide the copy and
6430b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // temporary.
6440b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  if (CalledFunc->onlyReadsMemory()) {
6450b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // If the byval argument has a specified alignment that is greater than the
6460b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // passed in pointer, then we either have to round up the input pointer or
6470b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // give up on this transformation.
6480b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    if (ByValAlignment <= 1)  // 0 = unspecified, 1 = no particular alignment.
6490b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner      return Arg;
6500b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6517569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // If the pointer is already known to be sufficiently aligned, or if we can
6527569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // round it up to a larger alignment, then we don't need a temporary.
6537569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    if (getOrEnforceKnownAlignment(Arg, ByValAlignment,
6547569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner                                   IFI.TD) >= ByValAlignment)
6557569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner      return Arg;
6560b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6577569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // Otherwise, we have to make a memcpy to get a safe alignment.  This is bad
6587569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // for code quality, but rarely happens and is required for correctness.
6590b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  }
660e7ae705c32906979a527926864345016e76867b9Chris Lattner
661e7ae705c32906979a527926864345016e76867b9Chris Lattner  LLVMContext &Context = Arg->getContext();
662e7ae705c32906979a527926864345016e76867b9Chris Lattner
6635fdd6c8793462549e3593890ec61573da06e3346Jay Foad  Type *VoidPtrTy = Type::getInt8PtrTy(Context);
664e7ae705c32906979a527926864345016e76867b9Chris Lattner
665e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Create the alloca.  If we have TargetData, use nice alignment.
666e7ae705c32906979a527926864345016e76867b9Chris Lattner  unsigned Align = 1;
667e7ae705c32906979a527926864345016e76867b9Chris Lattner  if (IFI.TD)
668e7ae705c32906979a527926864345016e76867b9Chris Lattner    Align = IFI.TD->getPrefTypeAlignment(AggTy);
669e7ae705c32906979a527926864345016e76867b9Chris Lattner
670e7ae705c32906979a527926864345016e76867b9Chris Lattner  // If the byval had an alignment specified, we *must* use at least that
671e7ae705c32906979a527926864345016e76867b9Chris Lattner  // alignment, as it is required by the byval argument (and uses of the
672e7ae705c32906979a527926864345016e76867b9Chris Lattner  // pointer inside the callee).
673e7ae705c32906979a527926864345016e76867b9Chris Lattner  Align = std::max(Align, ByValAlignment);
674e7ae705c32906979a527926864345016e76867b9Chris Lattner
675e7ae705c32906979a527926864345016e76867b9Chris Lattner  Function *Caller = TheCall->getParent()->getParent();
676e7ae705c32906979a527926864345016e76867b9Chris Lattner
677e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *NewAlloca = new AllocaInst(AggTy, 0, Align, Arg->getName(),
678e7ae705c32906979a527926864345016e76867b9Chris Lattner                                    &*Caller->begin()->begin());
679e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Emit a memcpy.
6805fdd6c8793462549e3593890ec61573da06e3346Jay Foad  Type *Tys[3] = {VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context)};
681e7ae705c32906979a527926864345016e76867b9Chris Lattner  Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
682e7ae705c32906979a527926864345016e76867b9Chris Lattner                                                 Intrinsic::memcpy,
683eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin Kramer                                                 Tys);
684e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
685e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *SrcCast = new BitCastInst(Arg, VoidPtrTy, "tmp", TheCall);
686e7ae705c32906979a527926864345016e76867b9Chris Lattner
687e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *Size;
688e7ae705c32906979a527926864345016e76867b9Chris Lattner  if (IFI.TD == 0)
689e7ae705c32906979a527926864345016e76867b9Chris Lattner    Size = ConstantExpr::getSizeOf(AggTy);
690e7ae705c32906979a527926864345016e76867b9Chris Lattner  else
691e7ae705c32906979a527926864345016e76867b9Chris Lattner    Size = ConstantInt::get(Type::getInt64Ty(Context),
692e7ae705c32906979a527926864345016e76867b9Chris Lattner                            IFI.TD->getTypeStoreSize(AggTy));
693e7ae705c32906979a527926864345016e76867b9Chris Lattner
694e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Always generate a memcpy of alignment 1 here because we don't know
695e7ae705c32906979a527926864345016e76867b9Chris Lattner  // the alignment of the src pointer.  Other optimizations can infer
696e7ae705c32906979a527926864345016e76867b9Chris Lattner  // better alignment.
697e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *CallArgs[] = {
698e7ae705c32906979a527926864345016e76867b9Chris Lattner    DestCast, SrcCast, Size,
699e7ae705c32906979a527926864345016e76867b9Chris Lattner    ConstantInt::get(Type::getInt32Ty(Context), 1),
700e7ae705c32906979a527926864345016e76867b9Chris Lattner    ConstantInt::getFalse(Context) // isVolatile
701e7ae705c32906979a527926864345016e76867b9Chris Lattner  };
702a3efbb15ddd5aa9006564cd79086723640084878Jay Foad  IRBuilder<>(TheCall).CreateCall(MemCpyFn, CallArgs);
703e7ae705c32906979a527926864345016e76867b9Chris Lattner
704e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Uses of the argument in the function should use our new alloca
705e7ae705c32906979a527926864345016e76867b9Chris Lattner  // instead.
706e7ae705c32906979a527926864345016e76867b9Chris Lattner  return NewAlloca;
707e7ae705c32906979a527926864345016e76867b9Chris Lattner}
708e7ae705c32906979a527926864345016e76867b9Chris Lattner
7096d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// isUsedByLifetimeMarker - Check whether this Value is used by a lifetime
7106d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// intrinsic.
7116d55f2269e20298a1d6a683be72d9552482156a9Nick Lewyckystatic bool isUsedByLifetimeMarker(Value *V) {
7126d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;
7136d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky       ++UI) {
7146d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*UI)) {
7156d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      switch (II->getIntrinsicID()) {
7166d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      default: break;
7176d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      case Intrinsic::lifetime_start:
7186d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      case Intrinsic::lifetime_end:
7196d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        return true;
7206d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      }
7216d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    }
7226d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
7236d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  return false;
7246d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky}
7256d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
7266d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// hasLifetimeMarkers - Check whether the given alloca already has
7276d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// lifetime.start or lifetime.end intrinsics.
7286d55f2269e20298a1d6a683be72d9552482156a9Nick Lewyckystatic bool hasLifetimeMarkers(AllocaInst *AI) {
729db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *Int8PtrTy = Type::getInt8PtrTy(AI->getType()->getContext());
7306d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  if (AI->getType() == Int8PtrTy)
7316d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    return isUsedByLifetimeMarker(AI);
7326d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
733708c1ac077fbc0cb73d489b4f4df3b2718566b05Nick Lewycky  // Do a scan to find all the casts to i8*.
7346d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); I != E;
7356d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky       ++I) {
7366d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (I->getType() != Int8PtrTy) continue;
737708c1ac077fbc0cb73d489b4f4df3b2718566b05Nick Lewycky    if (I->stripPointerCasts() != AI) continue;
7386d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (isUsedByLifetimeMarker(*I))
7396d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      return true;
7406d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
7416d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  return false;
7426d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky}
7436d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
7442cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// updateInlinedAtInfo - Helper function used by fixupLineNumbers to recursively
7452cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// update InlinedAtEntry of a DebugLoc.
7462cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patelstatic DebugLoc updateInlinedAtInfo(const DebugLoc &DL,
7472cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                                    const DebugLoc &InlinedAtDL,
7482cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                                    LLVMContext &Ctx) {
7492cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  if (MDNode *IA = DL.getInlinedAt(Ctx)) {
7502cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    DebugLoc NewInlinedAtDL
7512cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel      = updateInlinedAtInfo(DebugLoc::getFromDILocation(IA), InlinedAtDL, Ctx);
7522cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx),
7532cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                         NewInlinedAtDL.getAsMDNode(Ctx));
7542cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  }
7552cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
7562cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx),
7572cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                       InlinedAtDL.getAsMDNode(Ctx));
7582cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel}
7592cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
7602cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
7612cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// fixupLineNumbers - Update inlined instructions' line numbers to
7622cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// to encode location where these instructions are inlined.
7632cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patelstatic void fixupLineNumbers(Function *Fn, Function::iterator FI,
7642cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                              Instruction *TheCall) {
7652cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  DebugLoc TheCallDL = TheCall->getDebugLoc();
7662cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  if (TheCallDL.isUnknown())
7672cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    return;
7682cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
7692cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  for (; FI != Fn->end(); ++FI) {
7702cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
7712cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel         BI != BE; ++BI) {
7722cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel      DebugLoc DL = BI->getDebugLoc();
7732cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel      if (!DL.isUnknown())
7742cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel        BI->setDebugLoc(updateInlinedAtInfo(DL, TheCallDL, BI->getContext()));
7752cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    }
7762cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  }
7772cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel}
7782cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
779ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// InlineFunction - This function inlines the called function into the basic
780ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// block of the caller.  This returns false if it is not possible to inline this
781ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// call.  The program is still in a well defined state if this occurs though.
782ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
783fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// Note that this only does one level of inlining.  For example, if the
784fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
7857a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner// exists in the instruction stream.  Similarly this will inline a recursive
786ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// function by one level.
787ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
78860915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
78980a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  Instruction *TheCall = CS.getInstruction();
790e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson  LLVMContext &Context = TheCall->getContext();
79180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
79280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner         "Instruction not in function!");
793ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
79460915146f4d35e12f10dcdaa155596fac79184daChris Lattner  // If IFI has any state in it, zap it before we fill it in.
79560915146f4d35e12f10dcdaa155596fac79184daChris Lattner  IFI.reset();
79660915146f4d35e12f10dcdaa155596fac79184daChris Lattner
79780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  const Function *CalledFunc = CS.getCalledFunction();
798ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  if (CalledFunc == 0 ||          // Can't inline external function or indirect
7995cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer      CalledFunc->isDeclaration() || // call, or call to a vararg function!
8000623e90398153be61226ad19f1b40d3817874526Eric Christopher      CalledFunc->getFunctionType()->isVarArg()) return false;
801ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
802af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner  // If the call to the callee is not a tail call, we must clear the 'tail'
8031b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  // flags on any calls that we inline.
8041b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  bool MustClearTailCallFlags =
805af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner    !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
8061b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
807f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // If the call to the callee cannot throw, set the 'nounwind' flag on any
808f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // calls that we inline.
809f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  bool MarkNoUnwind = CS.doesNotThrow();
810f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
81180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  BasicBlock *OrigBB = TheCall->getParent();
812ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  Function *Caller = OrigBB->getParent();
813ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
8140e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  // GC poses two hazards to inlining, which only occur when the callee has GC:
8150e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //  1. If the caller has no GC, then the callee's GC must be propagated to the
8160e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //     caller.
8170e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //  2. If the caller has a differing GC, it is invalid to inline.
8185eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  if (CalledFunc->hasGC()) {
8195eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    if (!Caller->hasGC())
8205eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen      Caller->setGC(CalledFunc->getGC());
8215eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    else if (CalledFunc->getGC() != Caller->getGC())
8220e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen      return false;
8230e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  }
824a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
8255052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // Get an iterator to the last basic block in the function, which will have
8265052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // the new function inlined after it.
8275052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  //
8285052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  Function::iterator LastBlock = &Caller->back();
8295052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner
8305e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Make sure to capture all of the return instructions from the cloned
8315e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // function.
832ec1bea0d94372985a0a5eb283e644c6d0dd345dcChris Lattner  SmallVector<ReturnInst*, 8> Returns;
833cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  ClonedCodeInfo InlinedFunctionInfo;
8340744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen  Function::iterator FirstNewBlock;
835f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
83629d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel  { // Scope to destroy VMap after cloning.
8371ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola    ValueToValueMapTy VMap;
8385b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner
8399614fcc640eb628cc5dfddb277ebae9f6cb61014Dan Gohman    assert(CalledFunc->arg_size() == CS.arg_size() &&
8405e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           "No varargs calls can be inlined!");
841a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
842c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    // Calculate the vector of arguments to pass into the function cloner, which
843c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    // matches up the formal to the actual argument values.
8445e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    CallSite::arg_iterator AI = CS.arg_begin();
845c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    unsigned ArgNo = 0;
846e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner    for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
847c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner         E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
848c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner      Value *ActualArg = *AI;
849a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
850d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // When byval arguments actually inlined, we need to make the copy implied
851d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // by them explicit.  However, we don't do this if the callee is readonly
852d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // or readnone, because the copy would be unneeded: the callee doesn't
853d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // modify the struct.
854e7ae705c32906979a527926864345016e76867b9Chris Lattner      if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal)) {
855e7ae705c32906979a527926864345016e76867b9Chris Lattner        ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI,
856e7ae705c32906979a527926864345016e76867b9Chris Lattner                                        CalledFunc->getParamAlignment(ArgNo+1));
857e7ae705c32906979a527926864345016e76867b9Chris Lattner
8582914ba6ec793e2bb0e9ca5891af1d29ee2fee28eDuncan Sands        // Calls that we inline may use the new alloca, so we need to clear
859e7ae705c32906979a527926864345016e76867b9Chris Lattner        // their 'tail' flags if HandleByValArgument introduced a new alloca and
860e7ae705c32906979a527926864345016e76867b9Chris Lattner        // the callee has calls.
861e7ae705c32906979a527926864345016e76867b9Chris Lattner        MustClearTailCallFlags |= ActualArg != *AI;
862c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner      }
863a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
86429d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel      VMap[I] = ActualArg;
865c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    }
866fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
8675b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // We want the inliner to prune the code as it copies.  We would LOVE to
8685b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // have no dead or constant instructions leftover after inlining occurs
8695b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // (which can happen, e.g., because an argument was constant), but we'll be
8705b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // happy with whatever the cloner can do.
8716cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman    CloneAndPruneFunctionInto(Caller, CalledFunc, VMap,
8726cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman                              /*ModuleLevelChanges=*/false, Returns, ".i",
87360915146f4d35e12f10dcdaa155596fac79184daChris Lattner                              &InlinedFunctionInfo, IFI.TD, TheCall);
874a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
875d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // Remember the first block that is newly cloned over.
876d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    FirstNewBlock = LastBlock; ++FirstNewBlock;
877a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
878d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // Update the callgraph if requested.
87960915146f4d35e12f10dcdaa155596fac79184daChris Lattner    if (IFI.CG)
88029d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel      UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI);
8812cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
8822cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    // Update inlined instructions' line number information.
8832cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    fixupLineNumbers(Caller, FirstNewBlock, TheCall);
884fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  }
885a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
886ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // If there are any alloca instructions in the block that used to be the entry
887ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // block for the callee, move them to the entry block of the caller.  First
888ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // calculate which instruction they should be inserted before.  We insert the
889ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // instructions at the end of the current alloca list.
890ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  //
89121f20558d629f7ff8f64c20746d890d29328a544Chris Lattner  {
89280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner    BasicBlock::iterator InsertPoint = Caller->begin()->begin();
8935e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    for (BasicBlock::iterator I = FirstNewBlock->begin(),
894135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner         E = FirstNewBlock->end(); I != E; ) {
895135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      AllocaInst *AI = dyn_cast<AllocaInst>(I++);
896135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (AI == 0) continue;
897135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
898135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // If the alloca is now dead, remove it.  This often occurs due to code
899135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // specialization.
900135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (AI->use_empty()) {
901135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        AI->eraseFromParent();
902135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        continue;
90333bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner      }
904135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
905135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (!isa<Constant>(AI->getArraySize()))
906135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        continue;
907135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
90839add23dc54a1580983b1901384688d6622daa3bChris Lattner      // Keep track of the static allocas that we inline into the caller.
90960915146f4d35e12f10dcdaa155596fac79184daChris Lattner      IFI.StaticAllocas.push_back(AI);
9108f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner
911135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Scan for the block of allocas that we can move over, and move them
912135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // all at once.
913135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      while (isa<AllocaInst>(I) &&
9148f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner             isa<Constant>(cast<AllocaInst>(I)->getArraySize())) {
91560915146f4d35e12f10dcdaa155596fac79184daChris Lattner        IFI.StaticAllocas.push_back(cast<AllocaInst>(I));
916135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        ++I;
9178f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner      }
918135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
919135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Transfer all of the allocas over in a block.  Using splice means
920135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // that the instructions aren't removed from the symbol table, then
921135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // reinserted.
922135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      Caller->getEntryBlock().getInstList().splice(InsertPoint,
923135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner                                                   FirstNewBlock->getInstList(),
924135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner                                                   AI, I);
925135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    }
92680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  }
92780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
9286d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  // Leave lifetime markers for the static alloca's, scoping them to the
9296d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  // function we just inlined.
9306d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  if (!IFI.StaticAllocas.empty()) {
9316d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    IRBuilder<> builder(FirstNewBlock->begin());
9326d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    for (unsigned ai = 0, ae = IFI.StaticAllocas.size(); ai != ae; ++ai) {
9336d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      AllocaInst *AI = IFI.StaticAllocas[ai];
9346d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
9356d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      // If the alloca is already scoped to something smaller than the whole
9366d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      // function then there's no need to add redundant, less accurate markers.
9376d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      if (hasLifetimeMarkers(AI))
9386d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        continue;
9396d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
940e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall      builder.CreateLifetimeStart(AI);
9416d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      for (unsigned ri = 0, re = Returns.size(); ri != re; ++ri) {
9426d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        IRBuilder<> builder(Returns[ri]);
943e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall        builder.CreateLifetimeEnd(AI);
9446d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      }
9456d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    }
9466d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
9476d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
948bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // If the inlined code contained dynamic alloca instructions, wrap the inlined
949bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // code with llvm.stacksave/llvm.stackrestore intrinsics.
950bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  if (InlinedFunctionInfo.ContainsDynamicAllocas) {
951bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    Module *M = Caller->getParent();
952bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Get the two intrinsics we care about.
9536128df525501c333a650d097703c18d7e878f5e8Chris Lattner    Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
9546128df525501c333a650d097703c18d7e878f5e8Chris Lattner    Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore);
955d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
956bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert the llvm.stacksave.
957c975a51ac042eb15bcb04a293cb737810ff40a00John McCall    CallInst *SavedPtr = IRBuilder<>(FirstNewBlock, FirstNewBlock->begin())
958c975a51ac042eb15bcb04a293cb737810ff40a00John McCall      .CreateCall(StackSave, "savedstack");
959a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
960bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert a call to llvm.stackrestore before any return instructions in the
961bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // inlined function.
962d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
963c975a51ac042eb15bcb04a293cb737810ff40a00John McCall      IRBuilder<>(Returns[i]).CreateCall(StackRestore, SavedPtr);
964d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    }
965468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
966468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    // Count the number of StackRestore calls we insert.
967468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    unsigned NumStackRestores = Returns.size();
968a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
969bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // If we are inlining an invoke instruction, insert restores before each
970bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // unwind.  These unwinds will be rewritten into branches later.
971bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
972bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner      for (Function::iterator BB = FirstNewBlock, E = Caller->end();
973bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner           BB != E; ++BB)
974468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner        if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
975c975a51ac042eb15bcb04a293cb737810ff40a00John McCall          IRBuilder<>(UI).CreateCall(StackRestore, SavedPtr);
976468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner          ++NumStackRestores;
977468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner        }
978468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    }
979bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  }
980bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
981a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands  // If we are inlining tail call instruction through a call site that isn't
9821fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // marked 'tail', we must remove the tail marker for any calls in the inlined
983f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // code.  Also, calls inlined through a 'nounwind' call site should be marked
984f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // 'nounwind'.
985f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  if (InlinedFunctionInfo.ContainsCalls &&
986f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      (MustClearTailCallFlags || MarkNoUnwind)) {
9871b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
9881b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner         BB != E; ++BB)
9891b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
990f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        if (CallInst *CI = dyn_cast<CallInst>(I)) {
991f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands          if (MustClearTailCallFlags)
992f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands            CI->setTailCall(false);
993f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands          if (MarkNoUnwind)
994f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands            CI->setDoesNotThrow();
995f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        }
9961b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  }
9971b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
998f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // If we are inlining through a 'nounwind' call site then any inlined 'unwind'
999f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // instructions are unreachable.
1000f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind)
1001f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
1002f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands         BB != E; ++BB) {
1003f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      TerminatorInst *Term = BB->getTerminator();
1004f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      if (isa<UnwindInst>(Term)) {
10051d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson        new UnreachableInst(Context, Term);
1006f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        BB->getInstList().erase(Term);
1007f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      }
1008f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands    }
1009f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
10105e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // If we are inlining for an invoke instruction, we must make sure to rewrite
10115e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any inlined 'unwind' instructions into branches to the invoke exception
10125e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // destination, and call instructions into invoke instructions.
1013cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
101481dfb3885252fbf621b080827a080099864415f8Chris Lattner    HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
10155e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
101644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // If we cloned in _exactly one_ basic block, and if that block ends in a
101744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // return instruction, we splice the body of the inlined callee directly into
101844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // the calling basic block.
101944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
102044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Move all of the instructions right before the call.
102144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
102244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                 FirstNewBlock->begin(), FirstNewBlock->end());
102344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Remove the cloned basic block.
102444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    Caller->getBasicBlockList().pop_back();
1025fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
102644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the call site was an invoke instruction, add a branch to the normal
102744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // destination.
102844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
1029051a950000e21935165db56695e35bade668193bGabor Greif      BranchInst::Create(II->getNormalDest(), TheCall);
103044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
103144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the return instruction returned a value, replace uses of the call with
103244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // uses of the returned value.
1033dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel    if (!TheCall->use_empty()) {
1034dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel      ReturnInst *R = Returns[0];
10355877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      if (TheCall == R->getReturnValue())
10369e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson        TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
10375877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      else
10385877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman        TheCall->replaceAllUsesWith(R->getReturnValue());
1039dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel    }
104044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the Call/Invoke, we can delete it.
10411adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman    TheCall->eraseFromParent();
104244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
104344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the return instruction, delete it also.
10441adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman    Returns[0]->eraseFromParent();
104544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
104644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // We are now done with the inlining.
104744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    return true;
104844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  }
104944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
105044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Otherwise, we have the normal case, of more than one block to inline or
105144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // multiple return sites.
105244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
10535e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // We want to clone the entire callee function into the hole between the
10545e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // "starter" and "ender" blocks.  How we accomplish this depends on whether
10555e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // this is an invoke instruction or a call instruction.
10565e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  BasicBlock *AfterCallBB;
10575e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
1058fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
10595e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Add an unconditional branch to make this look like the CallInst case...
1060051a950000e21935165db56695e35bade668193bGabor Greif    BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall);
1061fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
10625e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Split the basic block.  This guarantees that no PHI nodes will have to be
10635e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // updated due to new incoming edges, and make the invoke case more
10645e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // symmetric to the call case.
10655e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(NewBr,
1066284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
1067fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
10685e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  } else {  // It's a call
106944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If this is a call instruction, we need to split the basic block that
107044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // the call lives in.
10715e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    //
10725e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(TheCall,
1073284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
10745e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
10755e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
107644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Change the branch that used to go to AfterCallBB to branch to the first
107744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // basic block of the inlined function.
107844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  //
107944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  TerminatorInst *Br = OrigBB->getTerminator();
1080fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  assert(Br && Br->getOpcode() == Instruction::Br &&
108144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner         "splitBasicBlock broken!");
108244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Br->setOperand(0, FirstNewBlock);
108344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
108444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
108544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Now that the function is correct, make it a little bit nicer.  In
108644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // particular, move the basic blocks inserted from the end of the function
108744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // into the space made by splitting the source basic block.
108844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
108944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                     FirstNewBlock, Caller->end());
109044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
10915e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Handle all of the return instructions that we just cloned in, and eliminate
10925e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any users of the original call/invoke instruction.
1093db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *RTy = CalledFunc->getReturnType();
10942c31750cd0ebdc83a890ace97dbb6249b3abe44eDan Gohman
10956fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  PHINode *PHI = 0;
1096fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman  if (Returns.size() > 1) {
10975e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // The PHI node should go at the front of the new basic block to merge all
10985e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // possible incoming values.
10995e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    if (!TheCall->use_empty()) {
11003ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad      PHI = PHINode::Create(RTy, Returns.size(), TheCall->getName(),
1101fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman                            AfterCallBB->begin());
1102fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      // Anything that used the result of the function call should now use the
1103fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      // PHI node as their operand.
1104a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands      TheCall->replaceAllUsesWith(PHI);
11055e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
1106fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
1107c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    // Loop over all of the return instructions adding entries to the PHI node
1108c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    // as appropriate.
1109fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman    if (PHI) {
1110fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
1111fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        ReturnInst *RI = Returns[i];
1112fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        assert(RI->getReturnValue()->getType() == PHI->getType() &&
1113fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman               "Ret value not consistent in function!");
1114fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        PHI->addIncoming(RI->getReturnValue(), RI->getParent());
11155e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      }
111612a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel    }
1117fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
1118c581acbbba3cb1af6a08e17314b26344333f9267Chris Lattner
1119de62aeaec49ddcf4a4c61fbbb3a22d3a4dd448f0Gabor Greif    // Add a branch to the merge points and remove return instructions.
112012a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
112112a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel      ReturnInst *RI = Returns[i];
11220744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen      BranchInst::Create(AfterCallBB, RI);
1123b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel      RI->eraseFromParent();
11245e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
1125b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel  } else if (!Returns.empty()) {
1126b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Otherwise, if there is exactly one return value, just replace anything
1127b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // using the return value of the call with the computed value.
11285877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman    if (!TheCall->use_empty()) {
11295877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      if (TheCall == Returns[0]->getReturnValue())
11309e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson        TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
11315877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      else
11325877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman        TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
11335877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman    }
1134a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
113595c3e48f9557adb6064d580684bb14cacec2f826Jay Foad    // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
113695c3e48f9557adb6064d580684bb14cacec2f826Jay Foad    BasicBlock *ReturnBB = Returns[0]->getParent();
113795c3e48f9557adb6064d580684bb14cacec2f826Jay Foad    ReturnBB->replaceAllUsesWith(AfterCallBB);
113895c3e48f9557adb6064d580684bb14cacec2f826Jay Foad
1139b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Splice the code from the return block into the block that it will return
1140b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // to, which contains the code that was after the call.
1141b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    AfterCallBB->getInstList().splice(AfterCallBB->begin(),
1142b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel                                      ReturnBB->getInstList());
1143a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
1144b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Delete the return instruction now and empty ReturnBB now.
1145b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    Returns[0]->eraseFromParent();
1146b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    ReturnBB->eraseFromParent();
11473787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  } else if (!TheCall->use_empty()) {
11483787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // No returns, but something is using the return value of the call.  Just
11493787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // nuke the result.
11509e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson    TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
11515e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
1152fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
11535e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Since we are now done with the Call/Invoke, we can delete it.
11543787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  TheCall->eraseFromParent();
1155ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
11567152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // We should always be able to fold the entry block of the function into the
11577152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // single predecessor of the block...
1158cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
11597152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
116044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
1161cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Splice the code entry block into calling block, right before the
1162cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // unconditional branch.
1163e59fbc04ad343435705c28b3cf7038d65fe4af0aEric Christopher  CalleeEntry->replaceAllUsesWith(OrigBB);  // Update PHI nodes
116495c3e48f9557adb6064d580684bb14cacec2f826Jay Foad  OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
1165cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
1166cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Remove the unconditional branch.
1167cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  OrigBB->getInstList().erase(Br);
1168cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
1169cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Now we can remove the CalleeEntry block, which is now empty.
1170cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  Caller->getBasicBlockList().erase(CalleeEntry);
1171a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
11726fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // If we inserted a phi node, check to see if it has a single value (e.g. all
11736fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // the entries are the same or undef).  If so, remove the PHI so it doesn't
11746fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // block other optimizations.
11756fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  if (PHI)
11766fb881c036c32728c4a128d81b6083457e534e09Duncan Sands    if (Value *V = SimplifyInstruction(PHI, IFI.TD)) {
11776fb881c036c32728c4a128d81b6083457e534e09Duncan Sands      PHI->replaceAllUsesWith(V);
11786fb881c036c32728c4a128d81b6083457e534e09Duncan Sands      PHI->eraseFromParent();
11796fb881c036c32728c4a128d81b6083457e534e09Duncan Sands    }
11806fb881c036c32728c4a128d81b6083457e534e09Duncan Sands
1181ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  return true;
1182ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner}
1183