InlineFunction.cpp revision 675f63886944d72e05e5210c36838c797364a0aa
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
48fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling// FIXME: New EH - Remove the functions marked [LIBUNWIND] when new EH is
49fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling// turned on.
50fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
511dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Look for an llvm.eh.exception call in the given block.
521dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHExceptionInst *findExceptionInBlock(BasicBlock *bb) {
531dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (BasicBlock::iterator i = bb->begin(), e = bb->end(); i != e; i++) {
54d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHExceptionInst *exn = dyn_cast<EHExceptionInst>(i);
551dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (exn) return exn;
561dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
571dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
581dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return 0;
591dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall}
601dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
611dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Look for the 'best' llvm.eh.selector instruction for
621dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// the given llvm.eh.exception call.
631dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHSelectorInst *findSelectorForException(EHExceptionInst *exn) {
641dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *exnBlock = exn->getParent();
65d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
661dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *outOfBlockSelector = 0;
671dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (Instruction::use_iterator
681dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall         ui = exn->use_begin(), ue = exn->use_end(); ui != ue; ++ui) {
691dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    EHSelectorInst *sel = dyn_cast<EHSelectorInst>(*ui);
701dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!sel) continue;
71d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
721dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Immediately accept an eh.selector in the same block as the
731dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // excepton call.
741dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (sel->getParent() == exnBlock) return sel;
75d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
761dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Otherwise, use the first selector we see.
771dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!outOfBlockSelector) outOfBlockSelector = sel;
781dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
791dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
801dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return outOfBlockSelector;
811dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall}
821dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
831dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Find the (possibly absent) call to @llvm.eh.selector
841dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// in the given landing pad.  In principle, llvm.eh.exception is
851dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// required to be in the landing pad; in practice, SplitCriticalEdge
861dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// can break that invariant, and then inlining can break it further.
871dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// There's a real need for a reliable solution here, but until that
881dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// happens, we have some fragile workarounds here.
891dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHSelectorInst *findSelectorForLandingPad(BasicBlock *lpad) {
901dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Look for an exception call in the actual landing pad.
911dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHExceptionInst *exn = findExceptionInBlock(lpad);
921dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (exn) return findSelectorForException(exn);
931dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
941dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Okay, if that failed, look for one in an obvious successor.  If
951dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // we find one, we'll fix the IR by moving things back to the
961dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // landing pad.
971dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
981dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  bool dominates = true; // does the lpad dominate the exn call
991dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *nonDominated = 0; // if not, the first non-dominated block
1001dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *lastDominated = 0; // and the block which branched to it
1011dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1021dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *exnBlock = lpad;
1031dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1041dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // We need to protect against lpads that lead into infinite loops.
1051dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  SmallPtrSet<BasicBlock*,4> visited;
1061dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  visited.insert(exnBlock);
1071dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1081dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  do {
1091dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // We're not going to apply this hack to anything more complicated
1101dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // than a series of unconditional branches, so if the block
1111dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // doesn't terminate in an unconditional branch, just fail.  More
1121dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // complicated cases can arise when, say, sinking a call into a
1131dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // split unwind edge and then inlining it; but that can do almost
1141dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // *anything* to the CFG, including leaving the selector
1151dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // completely unreachable.  The only way to fix that properly is
1161dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // to (1) prohibit transforms which move the exception or selector
1171dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // values away from the landing pad, e.g. by producing them with
1181dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // instructions that are pinned to an edge like a phi, or
1191dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // producing them with not-really-instructions, and (2) making
1201dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // transforms which split edges deal with that.
1211dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    BranchInst *branch = dyn_cast<BranchInst>(&exnBlock->back());
1221dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!branch || branch->isConditional()) return 0;
1231dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1241dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    BasicBlock *successor = branch->getSuccessor(0);
1251dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1261dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Fail if we found an infinite loop.
1271dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!visited.insert(successor)) return 0;
1281dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1291dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // If the successor isn't dominated by exnBlock:
1301dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!successor->getSinglePredecessor()) {
1311dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // We don't want to have to deal with threading the exception
1321dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // through multiple levels of phi, so give up if we've already
1331dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // followed a non-dominating edge.
1341dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      if (!dominates) return 0;
1351dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1361dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // Otherwise, remember this as a non-dominating edge.
1371dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      dominates = false;
1381dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      nonDominated = successor;
1391dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      lastDominated = exnBlock;
140d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
141d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
1421dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exnBlock = successor;
1431dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1441dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Can we stop here?
1451dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exn = findExceptionInBlock(exnBlock);
1461dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  } while (!exn);
1471dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1481dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Look for a selector call for the exception we found.
1491dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *selector = findSelectorForException(exn);
1501dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (!selector) return 0;
1511dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1521dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // The easy case is when the landing pad still dominates the
1531dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // exception call, in which case we can just move both calls back to
1541dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // the landing pad.
1551dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (dominates) {
1561dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    selector->moveBefore(lpad->getFirstNonPHI());
1571dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exn->moveBefore(selector);
158d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return selector;
159d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
160d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
1611dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Otherwise, we have to split at the first non-dominating block.
1621dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // The CFG looks basically like this:
1631dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    lpad:
1641dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_0
1651dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_1
1661dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %nonDominated
1671dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    nonDominated:
1681dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2
1691dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insns_3
1701dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn = call i8* @llvm.eh.exception()
1711dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_4
1721dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector = call @llvm.eh.selector(i8* %exn, ...
1731dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // We need to turn this into:
1741dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    lpad:
1751dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_0
1761dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn0 = call i8* @llvm.eh.exception()
1771dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector0 = call @llvm.eh.selector(i8* %exn0, ...
1781dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_1
1791dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %split // from lastDominated
1801dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    nonDominated:
1811dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2 (without edge from lastDominated)
1821dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn1 = call i8* @llvm.eh.exception()
1831dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector1 = call i8* @llvm.eh.selector(i8* %exn1, ...
1841dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %split
1851dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    split:
1861dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2 (edge from lastDominated, edge from split)
1871dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn = phi ...
1881dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector = phi ...
1891dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insns_3
1901dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_4
1911dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1921dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  assert(nonDominated);
1931dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  assert(lastDominated);
1941dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1951dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // First, make clones of the intrinsics to go in lpad.
1961dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHExceptionInst *lpadExn = cast<EHExceptionInst>(exn->clone());
1971dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *lpadSelector = cast<EHSelectorInst>(selector->clone());
1981dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadSelector->setArgOperand(0, lpadExn);
1991dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadSelector->insertBefore(lpad->getFirstNonPHI());
2001dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadExn->insertBefore(lpadSelector);
2011dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2021dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Split the non-dominated block.
2031dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *split =
2041dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    nonDominated->splitBasicBlock(nonDominated->getFirstNonPHI(),
2051dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                                  nonDominated->getName() + ".lpad-fix");
2061dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2071dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Redirect the last dominated branch there.
2081dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  cast<BranchInst>(lastDominated->back()).setSuccessor(0, split);
2091dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2101dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Move the existing intrinsics to the end of the old block.
2111dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->moveBefore(&nonDominated->back());
2121dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exn->moveBefore(selector);
2131dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2141dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  Instruction *splitIP = &split->front();
2151dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2161dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // For all the phis in nonDominated, make a new phi in split to join
2171dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // that phi with the edge from lastDominated.
2181dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (BasicBlock::iterator
2191dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall         i = nonDominated->begin(), e = nonDominated->end(); i != e; ++i) {
2201dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    PHINode *phi = dyn_cast<PHINode>(i);
2211dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!phi) break;
2221dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2231dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    PHINode *splitPhi = PHINode::Create(phi->getType(), 2, phi->getName(),
2241dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                                        splitIP);
2251dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    phi->replaceAllUsesWith(splitPhi);
2261dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    splitPhi->addIncoming(phi, nonDominated);
2271dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    splitPhi->addIncoming(phi->removeIncomingValue(lastDominated),
2281dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                          lastDominated);
2291dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
2301dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2311dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Make new phis for the exception and selector.
2321dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  PHINode *exnPhi = PHINode::Create(exn->getType(), 2, "", splitIP);
2331dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exn->replaceAllUsesWith(exnPhi);
2341dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->setArgOperand(0, exn); // except for this use
2351dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exnPhi->addIncoming(exn, nonDominated);
2361dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exnPhi->addIncoming(lpadExn, lastDominated);
2371dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2381dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  PHINode *selectorPhi = PHINode::Create(selector->getType(), 2, "", splitIP);
2391dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->replaceAllUsesWith(selectorPhi);
2401dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selectorPhi->addIncoming(selector, nonDominated);
2411dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selectorPhi->addIncoming(lpadSelector, lastDominated);
2421dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2431dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return lpadSelector;
244d7c10862016939c9850cadfe5e1c35513c0adf28John McCall}
245d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
246a3de16bc8f36638d5444e3e7b0112998af54f826John McCallnamespace {
247a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  /// A class for recording information about inlining through an invoke.
248a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  class InvokeInliningInfo {
249d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *OuterUnwindDest;
250d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHSelectorInst *OuterSelector;
251d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *InnerUnwindDest;
252d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *InnerExceptionPHI;
253d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *InnerSelectorPHI;
254a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    SmallVector<Value*, 8> UnwindDestPHIValues;
255a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
256fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    // FIXME: New EH - These will replace the analogous ones above.
257fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    BasicBlock *OuterResumeDest; //< Destination of the invoke's unwind.
258fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    BasicBlock *InnerResumeDest; //< Destination for the callee's resume.
259fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    LandingPadInst *CallerLPad;  //< LandingPadInst associated with the invoke.
260fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    PHINode *InnerEHValuesPHI;   //< PHI for EH values from landingpad insts.
261fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
2622bf84c15d24bb373987d9dbc6308092eac1b8324Bill Wendling  public:
263fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    InvokeInliningInfo(InvokeInst *II)
264fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      : OuterUnwindDest(II->getUnwindDest()), OuterSelector(0),
265fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling        InnerUnwindDest(0), InnerExceptionPHI(0), InnerSelectorPHI(0),
266fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling        OuterResumeDest(II->getUnwindDest()), InnerResumeDest(0),
267fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling        CallerLPad(0), InnerEHValuesPHI(0) {
268fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      // If there are PHI nodes in the unwind destination block, we need to keep
269fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      // track of which values came into them from the invoke before removing
270fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      // the edge from this block.
271fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      llvm::BasicBlock *InvokeBB = II->getParent();
272fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      BasicBlock::iterator I = OuterUnwindDest->begin();
273fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      for (; isa<PHINode>(I); ++I) {
274a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // Save the value to use for this edge.
275fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling        PHINode *PHI = cast<PHINode>(I);
276fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling        UnwindDestPHIValues.push_back(PHI->getIncomingValueForBlock(InvokeBB));
277fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      }
278fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
27927b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling      CallerLPad = cast<LandingPadInst>(I);
280a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
281a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
282d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    /// The outer unwind destination is the target of unwind edges
283d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    /// introduced for calls within the inlined function.
284d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *getOuterUnwindDest() const {
285d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      return OuterUnwindDest;
286a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
287a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
288d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHSelectorInst *getOuterSelector() {
289d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      if (!OuterSelector)
290d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        OuterSelector = findSelectorForLandingPad(OuterUnwindDest);
291d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      return OuterSelector;
292d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
293d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
294d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *getInnerUnwindDest();
2952bf84c15d24bb373987d9dbc6308092eac1b8324Bill Wendling
296fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    // FIXME: New EH - Rename when new EH is turned on.
297fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    BasicBlock *getInnerUnwindDestNewEH();
298fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
299fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    LandingPadInst *getLandingPadInst() const { return CallerLPad; }
300fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
301d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    bool forwardEHResume(CallInst *call, BasicBlock *src);
302d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
303fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    /// forwardResume - Forward the 'resume' instruction to the caller's landing
304fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    /// pad block. When the landing pad block has only one predecessor, this is
305fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    /// a simple branch. When there is more than one predecessor, we need to
306fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    /// split the landing pad block after the landingpad instruction and jump
307fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    /// to there.
308fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    void forwardResume(ResumeInst *RI);
309fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
310fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    /// addIncomingPHIValuesFor - Add incoming-PHI values to the unwind
311fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    /// destination block for the given basic block, using the values for the
312fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    /// original invoke's source block.
313a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    void addIncomingPHIValuesFor(BasicBlock *BB) const {
314d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      addIncomingPHIValuesForInto(BB, OuterUnwindDest);
315d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
31610c6d12a9fd4dab411091f64db4db69670b88850Bill Wendling
317d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    void addIncomingPHIValuesForInto(BasicBlock *src, BasicBlock *dest) const {
318d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      BasicBlock::iterator I = dest->begin();
319a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
32010c6d12a9fd4dab411091f64db4db69670b88850Bill Wendling        PHINode *phi = cast<PHINode>(I);
32110c6d12a9fd4dab411091f64db4db69670b88850Bill Wendling        phi->addIncoming(UnwindDestPHIValues[i], src);
322a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      }
323a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
324a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  };
325a3de16bc8f36638d5444e3e7b0112998af54f826John McCall}
326a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
327fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// [LIBUNWIND] Get or create a target for the branch out of rewritten calls to
328d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// llvm.eh.resume.
329d7c10862016939c9850cadfe5e1c35513c0adf28John McCallBasicBlock *InvokeInliningInfo::getInnerUnwindDest() {
330d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (InnerUnwindDest) return InnerUnwindDest;
331d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
332d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Find and hoist the llvm.eh.exception and llvm.eh.selector calls
333d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // in the outer landing pad to immediately following the phis.
334d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  EHSelectorInst *selector = getOuterSelector();
335d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (!selector) return 0;
336d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
337d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // The call to llvm.eh.exception *must* be in the landing pad.
338d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Instruction *exn = cast<Instruction>(selector->getArgOperand(0));
339d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  assert(exn->getParent() == OuterUnwindDest);
340d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
341d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // TODO: recognize when we've already done this, so that we don't
342d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // get a linear number of these when inlining calls into lots of
343d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // invokes with the same landing pad.
344d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
345d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Do the hoisting.
346d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Instruction *splitPoint = exn->getParent()->getFirstNonPHI();
347d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  assert(splitPoint != selector && "selector-on-exception dominance broken!");
348d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (splitPoint == exn) {
349d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->removeFromParent();
350d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->insertAfter(exn);
351d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    splitPoint = selector->getNextNode();
352d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  } else {
353d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    exn->moveBefore(splitPoint);
354d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->moveBefore(splitPoint);
355d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
356d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
357d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Split the landing pad.
358d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerUnwindDest = OuterUnwindDest->splitBasicBlock(splitPoint,
359d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        OuterUnwindDest->getName() + ".body");
360d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
361d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // The number of incoming edges we expect to the inner landing pad.
362d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  const unsigned phiCapacity = 2;
363d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
364d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Create corresponding new phis for all the phis in the outer landing pad.
365d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock::iterator insertPoint = InnerUnwindDest->begin();
366d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock::iterator I = OuterUnwindDest->begin();
367d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
368d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *outerPhi = cast<PHINode>(I);
369d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *innerPhi = PHINode::Create(outerPhi->getType(), phiCapacity,
370d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        outerPhi->getName() + ".lpad-body",
371d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        insertPoint);
372a6d7345ec91e4b07671f62d231e7c42f054bc70dJohn McCall    outerPhi->replaceAllUsesWith(innerPhi);
373d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    innerPhi->addIncoming(outerPhi, OuterUnwindDest);
374d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
375d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
376d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Create a phi for the exception value...
377d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerExceptionPHI = PHINode::Create(exn->getType(), phiCapacity,
378d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                      "exn.lpad-body", insertPoint);
379e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall  exn->replaceAllUsesWith(InnerExceptionPHI);
380d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  selector->setArgOperand(0, exn); // restore this use
381d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerExceptionPHI->addIncoming(exn, OuterUnwindDest);
382d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
383d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // ...and the selector.
384d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerSelectorPHI = PHINode::Create(selector->getType(), phiCapacity,
385d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                     "selector.lpad-body", insertPoint);
386e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall  selector->replaceAllUsesWith(InnerSelectorPHI);
387d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerSelectorPHI->addIncoming(selector, OuterUnwindDest);
388d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
389d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // All done.
390d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  return InnerUnwindDest;
391d7c10862016939c9850cadfe5e1c35513c0adf28John McCall}
392d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
393d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// [LIBUNWIND] Try to forward the given call, which logically occurs
394d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// at the end of the given block, as a branch to the inner unwind
395d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// block.  Returns true if the call was forwarded.
396d7c10862016939c9850cadfe5e1c35513c0adf28John McCallbool InvokeInliningInfo::forwardEHResume(CallInst *call, BasicBlock *src) {
3971edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // First, check whether this is a call to the intrinsic.
398d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Function *fn = dyn_cast<Function>(call->getCalledValue());
399d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (!fn || fn->getName() != "llvm.eh.resume")
400d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return false;
4011edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
4021edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // At this point, we need to return true on all paths, because
4031edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // otherwise we'll construct an invoke of the intrinsic, which is
4041edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // not well-formed.
405d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
4061edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // Try to find or make an inner unwind dest, which will fail if we
4071edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // can't find a selector call for the outer unwind dest.
408d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock *dest = getInnerUnwindDest();
4091edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  bool hasSelector = (dest != 0);
4101edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
4111edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // If we failed, just use the outer unwind dest, dropping the
4121edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // exception and selector on the floor.
4131edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  if (!hasSelector)
4141edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    dest = OuterUnwindDest;
415d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
416d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Make a branch.
417d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BranchInst::Create(dest, src);
418d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
419d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Update the phis in the destination.  They were inserted in an
420d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // order which makes this work.
421d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  addIncomingPHIValuesForInto(src, dest);
4221edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
4231edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  if (hasSelector) {
4241edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    InnerExceptionPHI->addIncoming(call->getArgOperand(0), src);
4251edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    InnerSelectorPHI->addIncoming(call->getArgOperand(1), src);
4261edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  }
427d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
428d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  return true;
429a3de16bc8f36638d5444e3e7b0112998af54f826John McCall}
430a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
431fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// Get or create a target for the branch from ResumeInsts.
432fe7a071a19ca6781c774c392c82341bdf14df104Bill WendlingBasicBlock *InvokeInliningInfo::getInnerUnwindDestNewEH() {
433fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  // FIXME: New EH - rename this function when new EH is turned on.
434fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  if (InnerResumeDest) return InnerResumeDest;
435fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
436fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  // Split the landing pad.
437fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  BasicBlock::iterator SplitPoint = CallerLPad; ++SplitPoint;
438fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  InnerResumeDest =
439fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    OuterResumeDest->splitBasicBlock(SplitPoint,
440fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling                                     OuterResumeDest->getName() + ".body");
441fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
442fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  // The number of incoming edges we expect to the inner landing pad.
443fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  const unsigned PHICapacity = 2;
444fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
445fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  // Create corresponding new PHIs for all the PHIs in the outer landing pad.
446fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  BasicBlock::iterator InsertPoint = InnerResumeDest->begin();
447fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  BasicBlock::iterator I = OuterResumeDest->begin();
448fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
449fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    PHINode *OuterPHI = cast<PHINode>(I);
450fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    PHINode *InnerPHI = PHINode::Create(OuterPHI->getType(), PHICapacity,
451fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling                                        OuterPHI->getName() + ".lpad-body",
452fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling                                        InsertPoint);
453fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    OuterPHI->replaceAllUsesWith(InnerPHI);
454fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    InnerPHI->addIncoming(OuterPHI, OuterResumeDest);
455fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  }
456fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
457fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  // Create a PHI for the exception values.
458fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  InnerEHValuesPHI = PHINode::Create(CallerLPad->getType(), PHICapacity,
459fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling                                     "eh.lpad-body", InsertPoint);
460fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  CallerLPad->replaceAllUsesWith(InnerEHValuesPHI);
461fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  InnerEHValuesPHI->addIncoming(CallerLPad, OuterResumeDest);
462fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
463fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  // All done.
464fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  return InnerResumeDest;
465fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling}
466fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
467fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// forwardResume - Forward the 'resume' instruction to the caller's landing pad
468fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// block. When the landing pad block has only one predecessor, this is a simple
469fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// branch. When there is more than one predecessor, we need to split the
470fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling/// landing pad block after the landingpad instruction and jump to there.
471fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendlingvoid InvokeInliningInfo::forwardResume(ResumeInst *RI) {
472fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  BasicBlock *Dest = getInnerUnwindDestNewEH();
473fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  BasicBlock *Src = RI->getParent();
474fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
475fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  BranchInst::Create(Dest, Src);
476fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
477fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  // Update the PHIs in the destination. They were inserted in an order which
478fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  // makes this work.
479fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  addIncomingPHIValuesForInto(Src, Dest);
480fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
481fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  InnerEHValuesPHI->addIncoming(RI->getOperand(0), Src);
482fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  RI->eraseFromParent();
483fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling}
484fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
485135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
486135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into
487f61f89ae14cf332a014a598153137113af34002fEric Christopher/// an invoke, we have to turn all of the calls that can throw into
488135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// invokes.  This function analyze BB to see if there are any calls, and if so,
489135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI
49081dfb3885252fbf621b080827a080099864415f8Chris Lattner/// nodes in that block with the values specified in InvokeDestPHIValues.
491135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner///
492a3de16bc8f36638d5444e3e7b0112998af54f826John McCall/// Returns true to indicate that the next block should be skipped.
493a3de16bc8f36638d5444e3e7b0112998af54f826John McCallstatic bool HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
494a3de16bc8f36638d5444e3e7b0112998af54f826John McCall                                                   InvokeInliningInfo &Invoke) {
495fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling  LandingPadInst *LPI = Invoke.getLandingPadInst();
496fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
497135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
498135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    Instruction *I = BBI++;
499fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
50027b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling    if (LandingPadInst *L = dyn_cast<LandingPadInst>(I)) {
50127b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling      unsigned NumClauses = LPI->getNumClauses();
50227b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling      L->reserveClauses(NumClauses);
50327b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling      for (unsigned i = 0; i != NumClauses; ++i)
50427b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling        L->addClause(LPI->getClause(i));
50527b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling    }
506fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
507135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // We only need to check for function calls: inlined invoke
508135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // instructions require no special handling.
509135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    CallInst *CI = dyn_cast<CallInst>(I);
510675f63886944d72e05e5210c36838c797364a0aaBill Wendling
511135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // If this call cannot unwind, don't convert it to an invoke.
512675f63886944d72e05e5210c36838c797364a0aaBill Wendling    if (!CI || CI->doesNotThrow())
513135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
514675f63886944d72e05e5210c36838c797364a0aaBill Wendling
515675f63886944d72e05e5210c36838c797364a0aaBill Wendling    // Convert this function call into an invoke instruction.  First, split the
516675f63886944d72e05e5210c36838c797364a0aaBill Wendling    // basic block.
517135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
518a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
519d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // Delete the unconditional branch inserted by splitBasicBlock
520d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BB->getInstList().pop_back();
521a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
522a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    // Otherwise, create the new invoke instruction.
523d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    ImmutableCallSite CS(CI);
524d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end());
52506881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling    InvokeInst *II = InvokeInst::Create(CI->getCalledValue(), Split,
52606881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling                                        Invoke.getOuterUnwindDest(),
52706881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling                                        InvokeArgs, CI->getName(), BB);
528d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    II->setCallingConv(CI->getCallingConv());
529d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    II->setAttributes(CI->getAttributes());
530135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
531d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // Make sure that anything using the call now uses the invoke!  This also
532d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // updates the CallGraph if present, because it uses a WeakVH.
533d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    CI->replaceAllUsesWith(II);
534d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
53506881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling    // Delete the original call
53606881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling    Split->getInstList().pop_front();
537a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
53806881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling    // Update any PHI nodes in the exceptional block to indicate that there is
53906881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling    // now a new entry in them.
540a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    Invoke.addIncomingPHIValuesFor(BB);
541d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return false;
542135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  }
543a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
544a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  return false;
545135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner}
546135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
547cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
548cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// in the body of the inlined function into invokes and turn unwind
549cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// instructions into branches to the invoke unwind dest.
550cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner///
551dac5c4b10b387b55c2394cd98a64f3f1394df2e8Nick Lewycky/// II is the invoke instruction being inlined.  FirstNewBlock is the first
552cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// block of the inlined code (the last block is the end of the function),
553cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// and InlineCodeInfo is information about the code that got inlined.
554cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattnerstatic void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
55581dfb3885252fbf621b080827a080099864415f8Chris Lattner                                ClonedCodeInfo &InlinedCodeInfo) {
556cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  BasicBlock *InvokeDest = II->getUnwindDest();
557cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
558cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  Function *Caller = FirstNewBlock->getParent();
559a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
560cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // The inlined code is currently at the end of the function, scan from the
561cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // start of the inlined code to its end, checking for stuff we need to
562135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // rewrite.  If the code doesn't have calls or unwinds, we know there is
563135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // nothing to rewrite.
564135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) {
565135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Now that everything is happy, we have one final detail.  The PHI nodes in
566135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // the exception destination block still have entries due to the original
567135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // invoke instruction.  Eliminate these entries (which might even delete the
568135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // PHI node) now.
569135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    InvokeDest->removePredecessor(II->getParent());
570135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    return;
571135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  }
572a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
573a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  InvokeInliningInfo Invoke(II);
574135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
575135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){
576135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (InlinedCodeInfo.ContainsCalls)
577a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      if (HandleCallsInBlockInlinedThroughInvoke(BB, Invoke)) {
578a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // Honor a request to skip the next block.  We don't need to
579a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // consider UnwindInsts in this case either.
580a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        ++BB;
581a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        continue;
582a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      }
583135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
584135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
585135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // An UnwindInst requires special handling when it gets inlined into an
586135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // invoke site.  Once this happens, we know that the unwind would cause
587135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // a control transfer to the invoke exception destination, so we can
588135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // transform it into a direct branch to the exception destination.
589135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      BranchInst::Create(InvokeDest, UI);
590135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
591135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Delete the unwind instruction!
592135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      UI->eraseFromParent();
593135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
594135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Update any PHI nodes in the exceptional block to indicate that
595135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // there is now a new entry in them.
596a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      Invoke.addIncomingPHIValuesFor(BB);
597cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    }
598fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
599fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    if (ResumeInst *RI = dyn_cast<ResumeInst>(BB->getTerminator())) {
600fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      Invoke.forwardResume(RI);
601fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    }
602cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  }
603cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
604cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // Now that everything is happy, we have one final detail.  The PHI nodes in
605cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // the exception destination block still have entries due to the original
606cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // invoke instruction.  Eliminate these entries (which might even delete the
607cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // PHI node) now.
608cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  InvokeDest->removePredecessor(II->getParent());
609cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner}
610cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
611d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee
612d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// into the caller, update the specified callgraph to reflect the changes we
613d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// made.  Note that it's possible that not all code was copied over, so only
614d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands/// some edges of the callgraph may remain.
615d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sandsstatic void UpdateCallGraphAfterInlining(CallSite CS,
616d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner                                         Function::iterator FirstNewBlock,
6171ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola                                         ValueToValueMapTy &VMap,
618fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner                                         InlineFunctionInfo &IFI) {
619fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner  CallGraph &CG = *IFI.CG;
620d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands  const Function *Caller = CS.getInstruction()->getParent()->getParent();
621d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands  const Function *Callee = CS.getCalledFunction();
622468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallGraphNode *CalleeNode = CG[Callee];
623468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallGraphNode *CallerNode = CG[Caller];
624a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
625d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  // Since we inlined some uninlined call sites in the callee into the caller,
626468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  // add edges from the caller to all of the callees of the callee.
627c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
628c478e52bf4c12691037856ee103c66946afeab6cGabor Greif
629c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  // Consider the case where CalleeNode == CallerNode.
630125329891f97baedef21e4b464ba70182c3fb45eGabor Greif  CallGraphNode::CalledFunctionsVector CallCache;
631c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  if (CalleeNode == CallerNode) {
632c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    CallCache.assign(I, E);
633c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    I = CallCache.begin();
634c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    E = CallCache.end();
635c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  }
636c478e52bf4c12691037856ee103c66946afeab6cGabor Greif
637c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  for (; I != E; ++I) {
638a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner    const Value *OrigCall = I->first;
639a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
6401ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola    ValueToValueMapTy::iterator VMI = VMap.find(OrigCall);
641981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner    // Only copy the edge if the call was inlined!
64229d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel    if (VMI == VMap.end() || VMI->second == 0)
643135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
644135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
645135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // If the call was inlined, but then constant folded, there is no edge to
646135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // add.  Check for this case.
647b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    Instruction *NewCall = dyn_cast<Instruction>(VMI->second);
648b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    if (NewCall == 0) continue;
6490ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner
6500ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    // Remember that this call site got inlined for the client of
6510ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    // InlineFunction.
6520ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    IFI.InlinedCalls.push_back(NewCall);
6530ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner
654b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // It's possible that inlining the callsite will cause it to go from an
655b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // indirect to a direct call by resolving a function pointer.  If this
656b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // happens, set the callee of the new call site to a more precise
657b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // destination.  This can also happen if the call graph node of the caller
658b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // was just unnecessarily imprecise.
659b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    if (I->second->getFunction() == 0)
660b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner      if (Function *F = CallSite(NewCall).getCalledFunction()) {
661b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner        // Indirect call site resolved to direct call.
66286099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif        CallerNode->addCalledFunction(CallSite(NewCall), CG[F]);
66386099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif
664b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner        continue;
665b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner      }
66686099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif
66786099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif    CallerNode->addCalledFunction(CallSite(NewCall), I->second);
668d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  }
669135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
67039fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  // Update the call graph by deleting the edge from Callee to Caller.  We must
67139fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  // do this after the loop above in case Caller and Callee are the same.
67239fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  CallerNode->removeCallEdgeFor(CS);
673468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
674468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
6750b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// HandleByValArgument - When inlining a call site that has a byval argument,
6760b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// we have to make the implicit memcpy explicit by adding it.
677e7ae705c32906979a527926864345016e76867b9Chris Lattnerstatic Value *HandleByValArgument(Value *Arg, Instruction *TheCall,
678e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  const Function *CalledFunc,
679e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  InlineFunctionInfo &IFI,
680e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  unsigned ByValAlignment) {
681db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *AggTy = cast<PointerType>(Arg->getType())->getElementType();
6820b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6830b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // If the called function is readonly, then it could not mutate the caller's
6840b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // copy of the byval'd memory.  In this case, it is safe to elide the copy and
6850b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // temporary.
6860b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  if (CalledFunc->onlyReadsMemory()) {
6870b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // If the byval argument has a specified alignment that is greater than the
6880b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // passed in pointer, then we either have to round up the input pointer or
6890b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // give up on this transformation.
6900b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    if (ByValAlignment <= 1)  // 0 = unspecified, 1 = no particular alignment.
6910b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner      return Arg;
6920b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6937569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // If the pointer is already known to be sufficiently aligned, or if we can
6947569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // round it up to a larger alignment, then we don't need a temporary.
6957569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    if (getOrEnforceKnownAlignment(Arg, ByValAlignment,
6967569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner                                   IFI.TD) >= ByValAlignment)
6977569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner      return Arg;
6980b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6997569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // Otherwise, we have to make a memcpy to get a safe alignment.  This is bad
7007569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // for code quality, but rarely happens and is required for correctness.
7010b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  }
702e7ae705c32906979a527926864345016e76867b9Chris Lattner
703e7ae705c32906979a527926864345016e76867b9Chris Lattner  LLVMContext &Context = Arg->getContext();
704e7ae705c32906979a527926864345016e76867b9Chris Lattner
7055fdd6c8793462549e3593890ec61573da06e3346Jay Foad  Type *VoidPtrTy = Type::getInt8PtrTy(Context);
706e7ae705c32906979a527926864345016e76867b9Chris Lattner
707e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Create the alloca.  If we have TargetData, use nice alignment.
708e7ae705c32906979a527926864345016e76867b9Chris Lattner  unsigned Align = 1;
709e7ae705c32906979a527926864345016e76867b9Chris Lattner  if (IFI.TD)
710e7ae705c32906979a527926864345016e76867b9Chris Lattner    Align = IFI.TD->getPrefTypeAlignment(AggTy);
711e7ae705c32906979a527926864345016e76867b9Chris Lattner
712e7ae705c32906979a527926864345016e76867b9Chris Lattner  // If the byval had an alignment specified, we *must* use at least that
713e7ae705c32906979a527926864345016e76867b9Chris Lattner  // alignment, as it is required by the byval argument (and uses of the
714e7ae705c32906979a527926864345016e76867b9Chris Lattner  // pointer inside the callee).
715e7ae705c32906979a527926864345016e76867b9Chris Lattner  Align = std::max(Align, ByValAlignment);
716e7ae705c32906979a527926864345016e76867b9Chris Lattner
717e7ae705c32906979a527926864345016e76867b9Chris Lattner  Function *Caller = TheCall->getParent()->getParent();
718e7ae705c32906979a527926864345016e76867b9Chris Lattner
719e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *NewAlloca = new AllocaInst(AggTy, 0, Align, Arg->getName(),
720e7ae705c32906979a527926864345016e76867b9Chris Lattner                                    &*Caller->begin()->begin());
721e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Emit a memcpy.
7225fdd6c8793462549e3593890ec61573da06e3346Jay Foad  Type *Tys[3] = {VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context)};
723e7ae705c32906979a527926864345016e76867b9Chris Lattner  Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
724e7ae705c32906979a527926864345016e76867b9Chris Lattner                                                 Intrinsic::memcpy,
725eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin Kramer                                                 Tys);
726e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
727e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *SrcCast = new BitCastInst(Arg, VoidPtrTy, "tmp", TheCall);
728e7ae705c32906979a527926864345016e76867b9Chris Lattner
729e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *Size;
730e7ae705c32906979a527926864345016e76867b9Chris Lattner  if (IFI.TD == 0)
731e7ae705c32906979a527926864345016e76867b9Chris Lattner    Size = ConstantExpr::getSizeOf(AggTy);
732e7ae705c32906979a527926864345016e76867b9Chris Lattner  else
733e7ae705c32906979a527926864345016e76867b9Chris Lattner    Size = ConstantInt::get(Type::getInt64Ty(Context),
734e7ae705c32906979a527926864345016e76867b9Chris Lattner                            IFI.TD->getTypeStoreSize(AggTy));
735e7ae705c32906979a527926864345016e76867b9Chris Lattner
736e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Always generate a memcpy of alignment 1 here because we don't know
737e7ae705c32906979a527926864345016e76867b9Chris Lattner  // the alignment of the src pointer.  Other optimizations can infer
738e7ae705c32906979a527926864345016e76867b9Chris Lattner  // better alignment.
739e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *CallArgs[] = {
740e7ae705c32906979a527926864345016e76867b9Chris Lattner    DestCast, SrcCast, Size,
741e7ae705c32906979a527926864345016e76867b9Chris Lattner    ConstantInt::get(Type::getInt32Ty(Context), 1),
742e7ae705c32906979a527926864345016e76867b9Chris Lattner    ConstantInt::getFalse(Context) // isVolatile
743e7ae705c32906979a527926864345016e76867b9Chris Lattner  };
744a3efbb15ddd5aa9006564cd79086723640084878Jay Foad  IRBuilder<>(TheCall).CreateCall(MemCpyFn, CallArgs);
745e7ae705c32906979a527926864345016e76867b9Chris Lattner
746e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Uses of the argument in the function should use our new alloca
747e7ae705c32906979a527926864345016e76867b9Chris Lattner  // instead.
748e7ae705c32906979a527926864345016e76867b9Chris Lattner  return NewAlloca;
749e7ae705c32906979a527926864345016e76867b9Chris Lattner}
750e7ae705c32906979a527926864345016e76867b9Chris Lattner
7516d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// isUsedByLifetimeMarker - Check whether this Value is used by a lifetime
7526d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// intrinsic.
7536d55f2269e20298a1d6a683be72d9552482156a9Nick Lewyckystatic bool isUsedByLifetimeMarker(Value *V) {
7546d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;
7556d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky       ++UI) {
7566d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*UI)) {
7576d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      switch (II->getIntrinsicID()) {
7586d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      default: break;
7596d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      case Intrinsic::lifetime_start:
7606d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      case Intrinsic::lifetime_end:
7616d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        return true;
7626d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      }
7636d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    }
7646d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
7656d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  return false;
7666d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky}
7676d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
7686d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// hasLifetimeMarkers - Check whether the given alloca already has
7696d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// lifetime.start or lifetime.end intrinsics.
7706d55f2269e20298a1d6a683be72d9552482156a9Nick Lewyckystatic bool hasLifetimeMarkers(AllocaInst *AI) {
771db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *Int8PtrTy = Type::getInt8PtrTy(AI->getType()->getContext());
7726d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  if (AI->getType() == Int8PtrTy)
7736d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    return isUsedByLifetimeMarker(AI);
7746d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
775708c1ac077fbc0cb73d489b4f4df3b2718566b05Nick Lewycky  // Do a scan to find all the casts to i8*.
7766d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); I != E;
7776d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky       ++I) {
7786d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (I->getType() != Int8PtrTy) continue;
779708c1ac077fbc0cb73d489b4f4df3b2718566b05Nick Lewycky    if (I->stripPointerCasts() != AI) continue;
7806d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (isUsedByLifetimeMarker(*I))
7816d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      return true;
7826d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
7836d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  return false;
7846d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky}
7856d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
7862cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// updateInlinedAtInfo - Helper function used by fixupLineNumbers to recursively
7872cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// update InlinedAtEntry of a DebugLoc.
7882cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patelstatic DebugLoc updateInlinedAtInfo(const DebugLoc &DL,
7892cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                                    const DebugLoc &InlinedAtDL,
7902cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                                    LLVMContext &Ctx) {
7912cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  if (MDNode *IA = DL.getInlinedAt(Ctx)) {
7922cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    DebugLoc NewInlinedAtDL
7932cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel      = updateInlinedAtInfo(DebugLoc::getFromDILocation(IA), InlinedAtDL, Ctx);
7942cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx),
7952cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                         NewInlinedAtDL.getAsMDNode(Ctx));
7962cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  }
7972cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
7982cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx),
7992cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                       InlinedAtDL.getAsMDNode(Ctx));
8002cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel}
8012cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
8022cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
8032cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// fixupLineNumbers - Update inlined instructions' line numbers to
8042cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel/// to encode location where these instructions are inlined.
8052cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patelstatic void fixupLineNumbers(Function *Fn, Function::iterator FI,
8062cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel                              Instruction *TheCall) {
8072cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  DebugLoc TheCallDL = TheCall->getDebugLoc();
8082cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  if (TheCallDL.isUnknown())
8092cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    return;
8102cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
8112cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  for (; FI != Fn->end(); ++FI) {
8122cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
8132cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel         BI != BE; ++BI) {
8142cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel      DebugLoc DL = BI->getDebugLoc();
815b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel      if (!DL.isUnknown()) {
8162cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel        BI->setDebugLoc(updateInlinedAtInfo(DL, TheCallDL, BI->getContext()));
817b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel        if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
818b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel          LLVMContext &Ctx = BI->getContext();
819b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel          MDNode *InlinedAt = BI->getDebugLoc().getInlinedAt(Ctx);
820b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel          DVI->setOperand(2, createInlinedVariable(DVI->getVariable(),
821b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel                                                   InlinedAt, Ctx));
822b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel        }
823b549bcfe6c19dbb24162c75bbcc06d4a5fa90cb8Devang Patel      }
8242cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    }
8252cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel  }
8262cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel}
8272cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
82806881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// InlineFunction - This function inlines the called function into the basic
82906881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// block of the caller.  This returns false if it is not possible to inline
83006881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// this call.  The program is still in a well defined state if this occurs
83106881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// though.
83206881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling///
83306881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// Note that this only does one level of inlining.  For example, if the
83406881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
83506881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// exists in the instruction stream.  Similarly this will inline a recursive
83606881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling/// function by one level.
83760915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
83880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  Instruction *TheCall = CS.getInstruction();
839e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson  LLVMContext &Context = TheCall->getContext();
84080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
84180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner         "Instruction not in function!");
842ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
84360915146f4d35e12f10dcdaa155596fac79184daChris Lattner  // If IFI has any state in it, zap it before we fill it in.
84460915146f4d35e12f10dcdaa155596fac79184daChris Lattner  IFI.reset();
84560915146f4d35e12f10dcdaa155596fac79184daChris Lattner
84680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  const Function *CalledFunc = CS.getCalledFunction();
847ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  if (CalledFunc == 0 ||          // Can't inline external function or indirect
8485cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer      CalledFunc->isDeclaration() || // call, or call to a vararg function!
8490623e90398153be61226ad19f1b40d3817874526Eric Christopher      CalledFunc->getFunctionType()->isVarArg()) return false;
850ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
851af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner  // If the call to the callee is not a tail call, we must clear the 'tail'
8521b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  // flags on any calls that we inline.
8531b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  bool MustClearTailCallFlags =
854af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner    !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
8551b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
856f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // If the call to the callee cannot throw, set the 'nounwind' flag on any
857f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // calls that we inline.
858f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  bool MarkNoUnwind = CS.doesNotThrow();
859f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
86080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  BasicBlock *OrigBB = TheCall->getParent();
861ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  Function *Caller = OrigBB->getParent();
862ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
8630e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  // GC poses two hazards to inlining, which only occur when the callee has GC:
8640e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //  1. If the caller has no GC, then the callee's GC must be propagated to the
8650e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //     caller.
8660e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //  2. If the caller has a differing GC, it is invalid to inline.
8675eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  if (CalledFunc->hasGC()) {
8685eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    if (!Caller->hasGC())
8695eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen      Caller->setGC(CalledFunc->getGC());
8705eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    else if (CalledFunc->getGC() != Caller->getGC())
8710e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen      return false;
8720e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  }
873a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
87430fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer  // Get the personality function from the callee if it contains a landing pad.
87530fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer  Value *CalleePersonality = 0;
87630fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer  for (Function::const_iterator I = CalledFunc->begin(), E = CalledFunc->end();
87730fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer       I != E; ++I)
878fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) {
879fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      const BasicBlock *BB = II->getUnwindDest();
88027b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling      const LandingPadInst *LP = BB->getLandingPadInst();
88127b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling      CalleePersonality = LP->getPersonalityFn();
882fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling      break;
883fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling    }
884fe7a071a19ca6781c774c392c82341bdf14df104Bill Wendling
88530fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer  // Find the personality function used by the landing pads of the caller. If it
88630fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer  // exists, then check to see that it matches the personality function used in
88730fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer  // the callee.
88806881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling  if (CalleePersonality) {
88930fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer    for (Function::const_iterator I = Caller->begin(), E = Caller->end();
89030fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer         I != E; ++I)
89130fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer      if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) {
89230fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer        const BasicBlock *BB = II->getUnwindDest();
89327b5658affba5b12b396048d2cc598c70719bfc5Bill Wendling        const LandingPadInst *LP = BB->getLandingPadInst();
89430fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer
89530fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer        // If the personality functions match, then we can perform the
89630fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer        // inlining. Otherwise, we can't inline.
89730fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer        // TODO: This isn't 100% true. Some personality functions are proper
89830fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer        //       supersets of others and can be used in place of the other.
89930fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer        if (LP->getPersonalityFn() != CalleePersonality)
90030fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer          return false;
90130fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer
90230fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer        break;
90330fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer      }
90406881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling  }
90530fe1ae20d02ac8e12cec9d767d855946546a030Benjamin Kramer
9065052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // Get an iterator to the last basic block in the function, which will have
9075052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // the new function inlined after it.
9085052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  //
9095052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  Function::iterator LastBlock = &Caller->back();
9105052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner
9115e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Make sure to capture all of the return instructions from the cloned
9125e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // function.
913ec1bea0d94372985a0a5eb283e644c6d0dd345dcChris Lattner  SmallVector<ReturnInst*, 8> Returns;
914cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  ClonedCodeInfo InlinedFunctionInfo;
9150744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen  Function::iterator FirstNewBlock;
916f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
91729d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel  { // Scope to destroy VMap after cloning.
9181ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola    ValueToValueMapTy VMap;
9195b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner
9209614fcc640eb628cc5dfddb277ebae9f6cb61014Dan Gohman    assert(CalledFunc->arg_size() == CS.arg_size() &&
9215e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           "No varargs calls can be inlined!");
922a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
923c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    // Calculate the vector of arguments to pass into the function cloner, which
924c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    // matches up the formal to the actual argument values.
9255e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    CallSite::arg_iterator AI = CS.arg_begin();
926c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    unsigned ArgNo = 0;
927e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner    for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
928c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner         E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
929c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner      Value *ActualArg = *AI;
930a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
931d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // When byval arguments actually inlined, we need to make the copy implied
932d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // by them explicit.  However, we don't do this if the callee is readonly
933d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // or readnone, because the copy would be unneeded: the callee doesn't
934d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // modify the struct.
935173862e5468fbcf4b022b9088d2c81b25c2d60c5Nick Lewycky      if (CS.isByValArgument(ArgNo)) {
936e7ae705c32906979a527926864345016e76867b9Chris Lattner        ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI,
937e7ae705c32906979a527926864345016e76867b9Chris Lattner                                        CalledFunc->getParamAlignment(ArgNo+1));
938e7ae705c32906979a527926864345016e76867b9Chris Lattner
9392914ba6ec793e2bb0e9ca5891af1d29ee2fee28eDuncan Sands        // Calls that we inline may use the new alloca, so we need to clear
940e7ae705c32906979a527926864345016e76867b9Chris Lattner        // their 'tail' flags if HandleByValArgument introduced a new alloca and
941e7ae705c32906979a527926864345016e76867b9Chris Lattner        // the callee has calls.
942e7ae705c32906979a527926864345016e76867b9Chris Lattner        MustClearTailCallFlags |= ActualArg != *AI;
943c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner      }
944a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
94529d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel      VMap[I] = ActualArg;
946c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    }
947fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
9485b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // We want the inliner to prune the code as it copies.  We would LOVE to
9495b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // have no dead or constant instructions leftover after inlining occurs
9505b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // (which can happen, e.g., because an argument was constant), but we'll be
9515b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // happy with whatever the cloner can do.
9526cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman    CloneAndPruneFunctionInto(Caller, CalledFunc, VMap,
9536cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman                              /*ModuleLevelChanges=*/false, Returns, ".i",
95460915146f4d35e12f10dcdaa155596fac79184daChris Lattner                              &InlinedFunctionInfo, IFI.TD, TheCall);
955a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
956d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // Remember the first block that is newly cloned over.
957d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    FirstNewBlock = LastBlock; ++FirstNewBlock;
958a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
959d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // Update the callgraph if requested.
96060915146f4d35e12f10dcdaa155596fac79184daChris Lattner    if (IFI.CG)
96129d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel      UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI);
9622cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel
9632cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    // Update inlined instructions' line number information.
9642cf158ec4bf78f236976f07e09d7b1fe6be13994Devang Patel    fixupLineNumbers(Caller, FirstNewBlock, TheCall);
965fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  }
966a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
967ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // If there are any alloca instructions in the block that used to be the entry
968ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // block for the callee, move them to the entry block of the caller.  First
969ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // calculate which instruction they should be inserted before.  We insert the
970ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // instructions at the end of the current alloca list.
97121f20558d629f7ff8f64c20746d890d29328a544Chris Lattner  {
97280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner    BasicBlock::iterator InsertPoint = Caller->begin()->begin();
9735e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    for (BasicBlock::iterator I = FirstNewBlock->begin(),
974135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner         E = FirstNewBlock->end(); I != E; ) {
975135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      AllocaInst *AI = dyn_cast<AllocaInst>(I++);
976135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (AI == 0) continue;
977135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
978135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // If the alloca is now dead, remove it.  This often occurs due to code
979135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // specialization.
980135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (AI->use_empty()) {
981135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        AI->eraseFromParent();
982135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        continue;
98333bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner      }
984135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
985135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (!isa<Constant>(AI->getArraySize()))
986135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        continue;
987135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
98839add23dc54a1580983b1901384688d6622daa3bChris Lattner      // Keep track of the static allocas that we inline into the caller.
98960915146f4d35e12f10dcdaa155596fac79184daChris Lattner      IFI.StaticAllocas.push_back(AI);
9908f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner
991135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Scan for the block of allocas that we can move over, and move them
992135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // all at once.
993135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      while (isa<AllocaInst>(I) &&
9948f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner             isa<Constant>(cast<AllocaInst>(I)->getArraySize())) {
99560915146f4d35e12f10dcdaa155596fac79184daChris Lattner        IFI.StaticAllocas.push_back(cast<AllocaInst>(I));
996135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        ++I;
9978f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner      }
998135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
999135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Transfer all of the allocas over in a block.  Using splice means
1000135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // that the instructions aren't removed from the symbol table, then
1001135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // reinserted.
1002135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      Caller->getEntryBlock().getInstList().splice(InsertPoint,
1003135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner                                                   FirstNewBlock->getInstList(),
1004135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner                                                   AI, I);
1005135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    }
100680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  }
100780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
10086d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  // Leave lifetime markers for the static alloca's, scoping them to the
10096d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  // function we just inlined.
10106d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  if (!IFI.StaticAllocas.empty()) {
10116d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    IRBuilder<> builder(FirstNewBlock->begin());
10126d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    for (unsigned ai = 0, ae = IFI.StaticAllocas.size(); ai != ae; ++ai) {
10136d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      AllocaInst *AI = IFI.StaticAllocas[ai];
10146d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
10156d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      // If the alloca is already scoped to something smaller than the whole
10166d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      // function then there's no need to add redundant, less accurate markers.
10176d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      if (hasLifetimeMarkers(AI))
10186d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        continue;
10196d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
1020e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall      builder.CreateLifetimeStart(AI);
10216d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      for (unsigned ri = 0, re = Returns.size(); ri != re; ++ri) {
10226d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        IRBuilder<> builder(Returns[ri]);
1023e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall        builder.CreateLifetimeEnd(AI);
10246d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      }
10256d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    }
10266d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
10276d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
1028bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // If the inlined code contained dynamic alloca instructions, wrap the inlined
1029bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // code with llvm.stacksave/llvm.stackrestore intrinsics.
1030bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  if (InlinedFunctionInfo.ContainsDynamicAllocas) {
1031bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    Module *M = Caller->getParent();
1032bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Get the two intrinsics we care about.
10336128df525501c333a650d097703c18d7e878f5e8Chris Lattner    Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
10346128df525501c333a650d097703c18d7e878f5e8Chris Lattner    Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore);
1035d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
1036bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert the llvm.stacksave.
1037c975a51ac042eb15bcb04a293cb737810ff40a00John McCall    CallInst *SavedPtr = IRBuilder<>(FirstNewBlock, FirstNewBlock->begin())
1038c975a51ac042eb15bcb04a293cb737810ff40a00John McCall      .CreateCall(StackSave, "savedstack");
1039a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
1040bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert a call to llvm.stackrestore before any return instructions in the
1041bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // inlined function.
1042d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
1043c975a51ac042eb15bcb04a293cb737810ff40a00John McCall      IRBuilder<>(Returns[i]).CreateCall(StackRestore, SavedPtr);
1044d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    }
1045468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
1046468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    // Count the number of StackRestore calls we insert.
1047468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    unsigned NumStackRestores = Returns.size();
1048a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
1049bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // If we are inlining an invoke instruction, insert restores before each
1050bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // unwind.  These unwinds will be rewritten into branches later.
1051bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
1052bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner      for (Function::iterator BB = FirstNewBlock, E = Caller->end();
1053bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner           BB != E; ++BB)
1054468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner        if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
1055c975a51ac042eb15bcb04a293cb737810ff40a00John McCall          IRBuilder<>(UI).CreateCall(StackRestore, SavedPtr);
1056468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner          ++NumStackRestores;
1057468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner        }
1058468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    }
1059bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  }
1060bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
1061a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands  // If we are inlining tail call instruction through a call site that isn't
10621fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // marked 'tail', we must remove the tail marker for any calls in the inlined
1063f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // code.  Also, calls inlined through a 'nounwind' call site should be marked
1064f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // 'nounwind'.
1065f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  if (InlinedFunctionInfo.ContainsCalls &&
1066f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      (MustClearTailCallFlags || MarkNoUnwind)) {
10671b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
10681b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner         BB != E; ++BB)
10691b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1070f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        if (CallInst *CI = dyn_cast<CallInst>(I)) {
1071f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands          if (MustClearTailCallFlags)
1072f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands            CI->setTailCall(false);
1073f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands          if (MarkNoUnwind)
1074f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands            CI->setDoesNotThrow();
1075f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        }
10761b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  }
10771b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
1078f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // If we are inlining through a 'nounwind' call site then any inlined 'unwind'
1079f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // instructions are unreachable.
1080f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind)
1081f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
1082f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands         BB != E; ++BB) {
1083f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      TerminatorInst *Term = BB->getTerminator();
1084f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      if (isa<UnwindInst>(Term)) {
10851d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson        new UnreachableInst(Context, Term);
1086f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        BB->getInstList().erase(Term);
1087f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      }
1088f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands    }
1089f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
10905e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // If we are inlining for an invoke instruction, we must make sure to rewrite
10915e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any inlined 'unwind' instructions into branches to the invoke exception
10925e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // destination, and call instructions into invoke instructions.
1093cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
109481dfb3885252fbf621b080827a080099864415f8Chris Lattner    HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
10955e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
109644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // If we cloned in _exactly one_ basic block, and if that block ends in a
109744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // return instruction, we splice the body of the inlined callee directly into
109844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // the calling basic block.
109944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
110044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Move all of the instructions right before the call.
110144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
110244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                 FirstNewBlock->begin(), FirstNewBlock->end());
110344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Remove the cloned basic block.
110444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    Caller->getBasicBlockList().pop_back();
1105fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
110644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the call site was an invoke instruction, add a branch to the normal
110744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // destination.
110844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
1109051a950000e21935165db56695e35bade668193bGabor Greif      BranchInst::Create(II->getNormalDest(), TheCall);
111044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
111144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the return instruction returned a value, replace uses of the call with
111244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // uses of the returned value.
1113dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel    if (!TheCall->use_empty()) {
1114dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel      ReturnInst *R = Returns[0];
11155877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      if (TheCall == R->getReturnValue())
11169e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson        TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
11175877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      else
11185877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman        TheCall->replaceAllUsesWith(R->getReturnValue());
1119dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel    }
112044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the Call/Invoke, we can delete it.
11211adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman    TheCall->eraseFromParent();
112244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
112344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the return instruction, delete it also.
11241adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman    Returns[0]->eraseFromParent();
112544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
112644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // We are now done with the inlining.
112744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    return true;
112844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  }
112944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
113044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Otherwise, we have the normal case, of more than one block to inline or
113144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // multiple return sites.
113244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
11335e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // We want to clone the entire callee function into the hole between the
11345e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // "starter" and "ender" blocks.  How we accomplish this depends on whether
11355e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // this is an invoke instruction or a call instruction.
11365e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  BasicBlock *AfterCallBB;
11375e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
1138fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
11395e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Add an unconditional branch to make this look like the CallInst case...
1140051a950000e21935165db56695e35bade668193bGabor Greif    BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall);
1141fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
11425e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Split the basic block.  This guarantees that no PHI nodes will have to be
11435e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // updated due to new incoming edges, and make the invoke case more
11445e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // symmetric to the call case.
11455e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(NewBr,
1146284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
1147fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
11485e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  } else {  // It's a call
114944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If this is a call instruction, we need to split the basic block that
115044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // the call lives in.
11515e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    //
11525e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(TheCall,
1153284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
11545e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
11555e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
115644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Change the branch that used to go to AfterCallBB to branch to the first
115744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // basic block of the inlined function.
115844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  //
115944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  TerminatorInst *Br = OrigBB->getTerminator();
1160fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  assert(Br && Br->getOpcode() == Instruction::Br &&
116144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner         "splitBasicBlock broken!");
116244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Br->setOperand(0, FirstNewBlock);
116344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
116444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
116544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Now that the function is correct, make it a little bit nicer.  In
116644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // particular, move the basic blocks inserted from the end of the function
116744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // into the space made by splitting the source basic block.
116844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
116944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                     FirstNewBlock, Caller->end());
117044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
11715e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Handle all of the return instructions that we just cloned in, and eliminate
11725e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any users of the original call/invoke instruction.
1173db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *RTy = CalledFunc->getReturnType();
11742c31750cd0ebdc83a890ace97dbb6249b3abe44eDan Gohman
11756fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  PHINode *PHI = 0;
1176fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman  if (Returns.size() > 1) {
11775e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // The PHI node should go at the front of the new basic block to merge all
11785e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // possible incoming values.
11795e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    if (!TheCall->use_empty()) {
11803ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad      PHI = PHINode::Create(RTy, Returns.size(), TheCall->getName(),
1181fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman                            AfterCallBB->begin());
1182fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      // Anything that used the result of the function call should now use the
1183fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      // PHI node as their operand.
1184a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands      TheCall->replaceAllUsesWith(PHI);
11855e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
1186fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
1187c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    // Loop over all of the return instructions adding entries to the PHI node
1188c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    // as appropriate.
1189fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman    if (PHI) {
1190fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
1191fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        ReturnInst *RI = Returns[i];
1192fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        assert(RI->getReturnValue()->getType() == PHI->getType() &&
1193fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman               "Ret value not consistent in function!");
1194fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        PHI->addIncoming(RI->getReturnValue(), RI->getParent());
11955e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      }
119612a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel    }
1197fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
1198c581acbbba3cb1af6a08e17314b26344333f9267Chris Lattner
1199de62aeaec49ddcf4a4c61fbbb3a22d3a4dd448f0Gabor Greif    // Add a branch to the merge points and remove return instructions.
120012a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
120112a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel      ReturnInst *RI = Returns[i];
12020744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen      BranchInst::Create(AfterCallBB, RI);
1203b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel      RI->eraseFromParent();
12045e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
1205b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel  } else if (!Returns.empty()) {
1206b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Otherwise, if there is exactly one return value, just replace anything
1207b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // using the return value of the call with the computed value.
12085877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman    if (!TheCall->use_empty()) {
12095877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      if (TheCall == Returns[0]->getReturnValue())
12109e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson        TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
12115877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      else
12125877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman        TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
12135877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman    }
1214a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
121595c3e48f9557adb6064d580684bb14cacec2f826Jay Foad    // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
121695c3e48f9557adb6064d580684bb14cacec2f826Jay Foad    BasicBlock *ReturnBB = Returns[0]->getParent();
121795c3e48f9557adb6064d580684bb14cacec2f826Jay Foad    ReturnBB->replaceAllUsesWith(AfterCallBB);
121895c3e48f9557adb6064d580684bb14cacec2f826Jay Foad
1219b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Splice the code from the return block into the block that it will return
1220b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // to, which contains the code that was after the call.
1221b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    AfterCallBB->getInstList().splice(AfterCallBB->begin(),
1222b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel                                      ReturnBB->getInstList());
1223a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
1224b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Delete the return instruction now and empty ReturnBB now.
1225b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    Returns[0]->eraseFromParent();
1226b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    ReturnBB->eraseFromParent();
12273787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  } else if (!TheCall->use_empty()) {
12283787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // No returns, but something is using the return value of the call.  Just
12293787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // nuke the result.
12309e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson    TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
12315e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
1232fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
12335e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Since we are now done with the Call/Invoke, we can delete it.
12343787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  TheCall->eraseFromParent();
1235ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
12367152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // We should always be able to fold the entry block of the function into the
12377152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // single predecessor of the block...
1238cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
12397152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
124044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
1241cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Splice the code entry block into calling block, right before the
1242cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // unconditional branch.
1243e59fbc04ad343435705c28b3cf7038d65fe4af0aEric Christopher  CalleeEntry->replaceAllUsesWith(OrigBB);  // Update PHI nodes
124495c3e48f9557adb6064d580684bb14cacec2f826Jay Foad  OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
1245cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
1246cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Remove the unconditional branch.
1247cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  OrigBB->getInstList().erase(Br);
1248cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
1249cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Now we can remove the CalleeEntry block, which is now empty.
1250cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  Caller->getBasicBlockList().erase(CalleeEntry);
1251a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
12526fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // If we inserted a phi node, check to see if it has a single value (e.g. all
12536fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // the entries are the same or undef).  If so, remove the PHI so it doesn't
12546fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // block other optimizations.
125506881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling  if (PHI) {
12566fb881c036c32728c4a128d81b6083457e534e09Duncan Sands    if (Value *V = SimplifyInstruction(PHI, IFI.TD)) {
12576fb881c036c32728c4a128d81b6083457e534e09Duncan Sands      PHI->replaceAllUsesWith(V);
12586fb881c036c32728c4a128d81b6083457e534e09Duncan Sands      PHI->eraseFromParent();
12596fb881c036c32728c4a128d81b6083457e534e09Duncan Sands    }
126006881e8734b1758fb0666f4e47a91bc58c6383beBill Wendling  }
12616fb881c036c32728c4a128d81b6083457e534e09Duncan Sands
1262ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  return true;
1263ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner}
1264