InlineFunction.cpp revision 708c1ac077fbc0cb73d489b4f4df3b2718566b05
1ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===- InlineFunction.cpp - Code to perform function inlining -------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
10ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// This file implements inlining of a function into a call site, resolving
11ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// parameters and the return value as appropriate.
12ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
13a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// The code in this file for handling inlines through invoke
14a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// instructions preserves semantics only under some assumptions about
15a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// the behavior of unwinders which correspond to gcc-style libUnwind
16a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// exception personality functions.  Eventually the IR will be
17a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// improved to make this unnecessary, but until then, this code is
18a3de16bc8f36638d5444e3e7b0112998af54f826John McCall// marked [LIBUNWIND].
19a3de16bc8f36638d5444e3e7b0112998af54f826John McCall//
20ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//===----------------------------------------------------------------------===//
21ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
22ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/Transforms/Utils/Cloning.h"
233787e765facfad5ea62753922d940bcdd52afd57Chris Lattner#include "llvm/Constants.h"
247152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner#include "llvm/DerivedTypes.h"
25ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/Module.h"
2680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Instructions.h"
27517576d6f96a0acde9bab79553d89f4ceba20cf6Devang Patel#include "llvm/IntrinsicInst.h"
2880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Intrinsics.h"
29eaf42abab6d465c38891345d999255871cf03943Devang Patel#include "llvm/Attributes.h"
30468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner#include "llvm/Analysis/CallGraph.h"
31517576d6f96a0acde9bab79553d89f4ceba20cf6Devang Patel#include "llvm/Analysis/DebugInfo.h"
326fb881c036c32728c4a128d81b6083457e534e09Duncan Sands#include "llvm/Analysis/InstructionSimplify.h"
33c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner#include "llvm/Target/TargetData.h"
347569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner#include "llvm/Transforms/Utils/Local.h"
3593e985f1b17aef62d58e3198a4604f9f6cfe8d19Chris Lattner#include "llvm/ADT/SmallVector.h"
36641ca93cff0f957fc5fb9dfb05d2a4a340aa8af7Devang Patel#include "llvm/ADT/StringExtras.h"
3780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner#include "llvm/Support/CallSite.h"
386d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky#include "llvm/Support/IRBuilder.h"
39f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm;
40ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
4160915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI) {
4260915146f4d35e12f10dcdaa155596fac79184daChris Lattner  return InlineFunction(CallSite(CI), IFI);
43468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
4460915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI) {
4560915146f4d35e12f10dcdaa155596fac79184daChris Lattner  return InlineFunction(CallSite(II), IFI);
46468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
4780a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
481dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Look for an llvm.eh.exception call in the given block.
491dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHExceptionInst *findExceptionInBlock(BasicBlock *bb) {
501dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (BasicBlock::iterator i = bb->begin(), e = bb->end(); i != e; i++) {
51d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHExceptionInst *exn = dyn_cast<EHExceptionInst>(i);
521dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (exn) return exn;
531dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
541dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
551dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return 0;
561dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall}
571dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
581dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Look for the 'best' llvm.eh.selector instruction for
591dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// the given llvm.eh.exception call.
601dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHSelectorInst *findSelectorForException(EHExceptionInst *exn) {
611dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *exnBlock = exn->getParent();
62d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
631dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *outOfBlockSelector = 0;
641dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (Instruction::use_iterator
651dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall         ui = exn->use_begin(), ue = exn->use_end(); ui != ue; ++ui) {
661dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    EHSelectorInst *sel = dyn_cast<EHSelectorInst>(*ui);
671dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!sel) continue;
68d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
691dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Immediately accept an eh.selector in the same block as the
701dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // excepton call.
711dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (sel->getParent() == exnBlock) return sel;
72d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
731dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Otherwise, use the first selector we see.
741dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!outOfBlockSelector) outOfBlockSelector = sel;
751dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
761dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
771dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return outOfBlockSelector;
781dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall}
791dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
801dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// [LIBUNWIND] Find the (possibly absent) call to @llvm.eh.selector
811dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// in the given landing pad.  In principle, llvm.eh.exception is
821dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// required to be in the landing pad; in practice, SplitCriticalEdge
831dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// can break that invariant, and then inlining can break it further.
841dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// There's a real need for a reliable solution here, but until that
851dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall/// happens, we have some fragile workarounds here.
861dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCallstatic EHSelectorInst *findSelectorForLandingPad(BasicBlock *lpad) {
871dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Look for an exception call in the actual landing pad.
881dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHExceptionInst *exn = findExceptionInBlock(lpad);
891dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (exn) return findSelectorForException(exn);
901dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
911dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Okay, if that failed, look for one in an obvious successor.  If
921dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // we find one, we'll fix the IR by moving things back to the
931dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // landing pad.
941dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
951dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  bool dominates = true; // does the lpad dominate the exn call
961dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *nonDominated = 0; // if not, the first non-dominated block
971dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *lastDominated = 0; // and the block which branched to it
981dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
991dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *exnBlock = lpad;
1001dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1011dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // We need to protect against lpads that lead into infinite loops.
1021dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  SmallPtrSet<BasicBlock*,4> visited;
1031dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  visited.insert(exnBlock);
1041dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1051dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  do {
1061dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // We're not going to apply this hack to anything more complicated
1071dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // than a series of unconditional branches, so if the block
1081dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // doesn't terminate in an unconditional branch, just fail.  More
1091dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // complicated cases can arise when, say, sinking a call into a
1101dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // split unwind edge and then inlining it; but that can do almost
1111dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // *anything* to the CFG, including leaving the selector
1121dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // completely unreachable.  The only way to fix that properly is
1131dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // to (1) prohibit transforms which move the exception or selector
1141dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // values away from the landing pad, e.g. by producing them with
1151dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // instructions that are pinned to an edge like a phi, or
1161dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // producing them with not-really-instructions, and (2) making
1171dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // transforms which split edges deal with that.
1181dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    BranchInst *branch = dyn_cast<BranchInst>(&exnBlock->back());
1191dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!branch || branch->isConditional()) return 0;
1201dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1211dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    BasicBlock *successor = branch->getSuccessor(0);
1221dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1231dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Fail if we found an infinite loop.
1241dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!visited.insert(successor)) return 0;
1251dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1261dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // If the successor isn't dominated by exnBlock:
1271dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!successor->getSinglePredecessor()) {
1281dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // We don't want to have to deal with threading the exception
1291dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // through multiple levels of phi, so give up if we've already
1301dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // followed a non-dominating edge.
1311dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      if (!dominates) return 0;
1321dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1331dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      // Otherwise, remember this as a non-dominating edge.
1341dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      dominates = false;
1351dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      nonDominated = successor;
1361dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall      lastDominated = exnBlock;
137d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
138d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
1391dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exnBlock = successor;
1401dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1411dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    // Can we stop here?
1421dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exn = findExceptionInBlock(exnBlock);
1431dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  } while (!exn);
1441dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1451dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Look for a selector call for the exception we found.
1461dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *selector = findSelectorForException(exn);
1471dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (!selector) return 0;
1481dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1491dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // The easy case is when the landing pad still dominates the
1501dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // exception call, in which case we can just move both calls back to
1511dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // the landing pad.
1521dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  if (dominates) {
1531dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    selector->moveBefore(lpad->getFirstNonPHI());
1541dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    exn->moveBefore(selector);
155d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return selector;
156d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
157d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
1581dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Otherwise, we have to split at the first non-dominating block.
1591dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // The CFG looks basically like this:
1601dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    lpad:
1611dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_0
1621dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_1
1631dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %nonDominated
1641dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    nonDominated:
1651dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2
1661dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insns_3
1671dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn = call i8* @llvm.eh.exception()
1681dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_4
1691dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector = call @llvm.eh.selector(i8* %exn, ...
1701dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // We need to turn this into:
1711dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    lpad:
1721dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_0
1731dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn0 = call i8* @llvm.eh.exception()
1741dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector0 = call @llvm.eh.selector(i8* %exn0, ...
1751dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_1
1761dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %split // from lastDominated
1771dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    nonDominated:
1781dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2 (without edge from lastDominated)
1791dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn1 = call i8* @llvm.eh.exception()
1801dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector1 = call i8* @llvm.eh.selector(i8* %exn1, ...
1811dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      br label %split
1821dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //    split:
1831dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      phis_2 (edge from lastDominated, edge from split)
1841dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %exn = phi ...
1851dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      %selector = phi ...
1861dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insns_3
1871dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  //      insnsAndBranches_4
1881dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1891dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  assert(nonDominated);
1901dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  assert(lastDominated);
1911dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1921dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // First, make clones of the intrinsics to go in lpad.
1931dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHExceptionInst *lpadExn = cast<EHExceptionInst>(exn->clone());
1941dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  EHSelectorInst *lpadSelector = cast<EHSelectorInst>(selector->clone());
1951dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadSelector->setArgOperand(0, lpadExn);
1961dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadSelector->insertBefore(lpad->getFirstNonPHI());
1971dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  lpadExn->insertBefore(lpadSelector);
1981dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
1991dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Split the non-dominated block.
2001dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  BasicBlock *split =
2011dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    nonDominated->splitBasicBlock(nonDominated->getFirstNonPHI(),
2021dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                                  nonDominated->getName() + ".lpad-fix");
2031dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2041dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Redirect the last dominated branch there.
2051dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  cast<BranchInst>(lastDominated->back()).setSuccessor(0, split);
2061dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2071dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Move the existing intrinsics to the end of the old block.
2081dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->moveBefore(&nonDominated->back());
2091dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exn->moveBefore(selector);
2101dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2111dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  Instruction *splitIP = &split->front();
2121dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2131dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // For all the phis in nonDominated, make a new phi in split to join
2141dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // that phi with the edge from lastDominated.
2151dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  for (BasicBlock::iterator
2161dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall         i = nonDominated->begin(), e = nonDominated->end(); i != e; ++i) {
2171dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    PHINode *phi = dyn_cast<PHINode>(i);
2181dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    if (!phi) break;
2191dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2201dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    PHINode *splitPhi = PHINode::Create(phi->getType(), 2, phi->getName(),
2211dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                                        splitIP);
2221dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    phi->replaceAllUsesWith(splitPhi);
2231dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    splitPhi->addIncoming(phi, nonDominated);
2241dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall    splitPhi->addIncoming(phi->removeIncomingValue(lastDominated),
2251dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall                          lastDominated);
2261dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  }
2271dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2281dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  // Make new phis for the exception and selector.
2291dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  PHINode *exnPhi = PHINode::Create(exn->getType(), 2, "", splitIP);
2301dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exn->replaceAllUsesWith(exnPhi);
2311dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->setArgOperand(0, exn); // except for this use
2321dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exnPhi->addIncoming(exn, nonDominated);
2331dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  exnPhi->addIncoming(lpadExn, lastDominated);
2341dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2351dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  PHINode *selectorPhi = PHINode::Create(selector->getType(), 2, "", splitIP);
2361dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selector->replaceAllUsesWith(selectorPhi);
2371dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selectorPhi->addIncoming(selector, nonDominated);
2381dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  selectorPhi->addIncoming(lpadSelector, lastDominated);
2391dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall
2401dd94bbfa1269b1144a87f5fe9dbc04869f858b4John McCall  return lpadSelector;
241d7c10862016939c9850cadfe5e1c35513c0adf28John McCall}
242d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
243a3de16bc8f36638d5444e3e7b0112998af54f826John McCallnamespace {
244a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  /// A class for recording information about inlining through an invoke.
245a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  class InvokeInliningInfo {
246d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *OuterUnwindDest;
247d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHSelectorInst *OuterSelector;
248d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *InnerUnwindDest;
249d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *InnerExceptionPHI;
250d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *InnerSelectorPHI;
251a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    SmallVector<Value*, 8> UnwindDestPHIValues;
252a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
253a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  public:
254d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    InvokeInliningInfo(InvokeInst *II) :
255d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      OuterUnwindDest(II->getUnwindDest()), OuterSelector(0),
256d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      InnerUnwindDest(0), InnerExceptionPHI(0), InnerSelectorPHI(0) {
257d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
258a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // If there are PHI nodes in the unwind destination block, we
259a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // need to keep track of which values came into them from the
260a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // invoke before removing the edge from this block.
261d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      llvm::BasicBlock *invokeBB = II->getParent();
262d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      for (BasicBlock::iterator I = OuterUnwindDest->begin();
263d7c10862016939c9850cadfe5e1c35513c0adf28John McCall             isa<PHINode>(I); ++I) {
264a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // Save the value to use for this edge.
265d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        PHINode *phi = cast<PHINode>(I);
266d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        UnwindDestPHIValues.push_back(phi->getIncomingValueForBlock(invokeBB));
267a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      }
268a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
269a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
270d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    /// The outer unwind destination is the target of unwind edges
271d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    /// introduced for calls within the inlined function.
272d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *getOuterUnwindDest() const {
273d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      return OuterUnwindDest;
274a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
275a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
276d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    EHSelectorInst *getOuterSelector() {
277d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      if (!OuterSelector)
278d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        OuterSelector = findSelectorForLandingPad(OuterUnwindDest);
279d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      return OuterSelector;
280d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
281d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
282d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BasicBlock *getInnerUnwindDest();
283d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
284d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    bool forwardEHResume(CallInst *call, BasicBlock *src);
285d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
286a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    /// Add incoming-PHI values to the unwind destination block for
287a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    /// the given basic block, using the values for the original
288a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    /// invoke's source block.
289a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    void addIncomingPHIValuesFor(BasicBlock *BB) const {
290d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      addIncomingPHIValuesForInto(BB, OuterUnwindDest);
291d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
292d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
293d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    void addIncomingPHIValuesForInto(BasicBlock *src, BasicBlock *dest) const {
294d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      BasicBlock::iterator I = dest->begin();
295a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
296d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        PHINode *phi = cast<PHINode>(I);
297d7c10862016939c9850cadfe5e1c35513c0adf28John McCall        phi->addIncoming(UnwindDestPHIValues[i], src);
298a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      }
299a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
300a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  };
301a3de16bc8f36638d5444e3e7b0112998af54f826John McCall}
302a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
303d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// Get or create a target for the branch out of rewritten calls to
304d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// llvm.eh.resume.
305d7c10862016939c9850cadfe5e1c35513c0adf28John McCallBasicBlock *InvokeInliningInfo::getInnerUnwindDest() {
306d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (InnerUnwindDest) return InnerUnwindDest;
307d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
308d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Find and hoist the llvm.eh.exception and llvm.eh.selector calls
309d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // in the outer landing pad to immediately following the phis.
310d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  EHSelectorInst *selector = getOuterSelector();
311d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (!selector) return 0;
312d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
313d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // The call to llvm.eh.exception *must* be in the landing pad.
314d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Instruction *exn = cast<Instruction>(selector->getArgOperand(0));
315d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  assert(exn->getParent() == OuterUnwindDest);
316d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
317d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // TODO: recognize when we've already done this, so that we don't
318d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // get a linear number of these when inlining calls into lots of
319d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // invokes with the same landing pad.
320d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
321d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Do the hoisting.
322d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Instruction *splitPoint = exn->getParent()->getFirstNonPHI();
323d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  assert(splitPoint != selector && "selector-on-exception dominance broken!");
324d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (splitPoint == exn) {
325d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->removeFromParent();
326d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->insertAfter(exn);
327d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    splitPoint = selector->getNextNode();
328d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  } else {
329d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    exn->moveBefore(splitPoint);
330d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    selector->moveBefore(splitPoint);
331d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
332d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
333d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Split the landing pad.
334d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerUnwindDest = OuterUnwindDest->splitBasicBlock(splitPoint,
335d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        OuterUnwindDest->getName() + ".body");
336d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
337d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // The number of incoming edges we expect to the inner landing pad.
338d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  const unsigned phiCapacity = 2;
339d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
340d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Create corresponding new phis for all the phis in the outer landing pad.
341d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock::iterator insertPoint = InnerUnwindDest->begin();
342d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock::iterator I = OuterUnwindDest->begin();
343d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
344d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *outerPhi = cast<PHINode>(I);
345d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    PHINode *innerPhi = PHINode::Create(outerPhi->getType(), phiCapacity,
346d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        outerPhi->getName() + ".lpad-body",
347d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                        insertPoint);
348a6d7345ec91e4b07671f62d231e7c42f054bc70dJohn McCall    outerPhi->replaceAllUsesWith(innerPhi);
349d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    innerPhi->addIncoming(outerPhi, OuterUnwindDest);
350d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  }
351d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
352d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Create a phi for the exception value...
353d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerExceptionPHI = PHINode::Create(exn->getType(), phiCapacity,
354d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                      "exn.lpad-body", insertPoint);
355e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall  exn->replaceAllUsesWith(InnerExceptionPHI);
356d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  selector->setArgOperand(0, exn); // restore this use
357d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerExceptionPHI->addIncoming(exn, OuterUnwindDest);
358d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
359d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // ...and the selector.
360d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerSelectorPHI = PHINode::Create(selector->getType(), phiCapacity,
361d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                                     "selector.lpad-body", insertPoint);
362e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall  selector->replaceAllUsesWith(InnerSelectorPHI);
363d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  InnerSelectorPHI->addIncoming(selector, OuterUnwindDest);
364d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
365d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // All done.
366d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  return InnerUnwindDest;
367d7c10862016939c9850cadfe5e1c35513c0adf28John McCall}
368d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
369d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// [LIBUNWIND] Try to forward the given call, which logically occurs
370d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// at the end of the given block, as a branch to the inner unwind
371d7c10862016939c9850cadfe5e1c35513c0adf28John McCall/// block.  Returns true if the call was forwarded.
372d7c10862016939c9850cadfe5e1c35513c0adf28John McCallbool InvokeInliningInfo::forwardEHResume(CallInst *call, BasicBlock *src) {
3731edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // First, check whether this is a call to the intrinsic.
374d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  Function *fn = dyn_cast<Function>(call->getCalledValue());
375d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  if (!fn || fn->getName() != "llvm.eh.resume")
376d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return false;
3771edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
3781edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // At this point, we need to return true on all paths, because
3791edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // otherwise we'll construct an invoke of the intrinsic, which is
3801edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // not well-formed.
381d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
3821edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // Try to find or make an inner unwind dest, which will fail if we
3831edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // can't find a selector call for the outer unwind dest.
384d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BasicBlock *dest = getInnerUnwindDest();
3851edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  bool hasSelector = (dest != 0);
3861edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
3871edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // If we failed, just use the outer unwind dest, dropping the
3881edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  // exception and selector on the floor.
3891edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  if (!hasSelector)
3901edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    dest = OuterUnwindDest;
391d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
392d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Make a branch.
393d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  BranchInst::Create(dest, src);
394d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
395d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // Update the phis in the destination.  They were inserted in an
396d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  // order which makes this work.
397d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  addIncomingPHIValuesForInto(src, dest);
3981edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall
3991edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  if (hasSelector) {
4001edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    InnerExceptionPHI->addIncoming(call->getArgOperand(0), src);
4011edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall    InnerSelectorPHI->addIncoming(call->getArgOperand(1), src);
4021edbd6f3f07176851cb03f7932ff50b9e9619dfbJohn McCall  }
403d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
404d7c10862016939c9850cadfe5e1c35513c0adf28John McCall  return true;
405a3de16bc8f36638d5444e3e7b0112998af54f826John McCall}
406a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
407a3de16bc8f36638d5444e3e7b0112998af54f826John McCall/// [LIBUNWIND] Check whether this selector is "only cleanups":
408a3de16bc8f36638d5444e3e7b0112998af54f826John McCall///   call i32 @llvm.eh.selector(blah, blah, i32 0)
409a3de16bc8f36638d5444e3e7b0112998af54f826John McCallstatic bool isCleanupOnlySelector(EHSelectorInst *selector) {
410a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  if (selector->getNumArgOperands() != 3) return false;
411a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  ConstantInt *val = dyn_cast<ConstantInt>(selector->getArgOperand(2));
412a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  return (val && val->isZero());
413a3de16bc8f36638d5444e3e7b0112998af54f826John McCall}
414135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
415135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into
416f61f89ae14cf332a014a598153137113af34002fEric Christopher/// an invoke, we have to turn all of the calls that can throw into
417135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// invokes.  This function analyze BB to see if there are any calls, and if so,
418135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI
41981dfb3885252fbf621b080827a080099864415f8Chris Lattner/// nodes in that block with the values specified in InvokeDestPHIValues.
420135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner///
421a3de16bc8f36638d5444e3e7b0112998af54f826John McCall/// Returns true to indicate that the next block should be skipped.
422a3de16bc8f36638d5444e3e7b0112998af54f826John McCallstatic bool HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
423a3de16bc8f36638d5444e3e7b0112998af54f826John McCall                                                   InvokeInliningInfo &Invoke) {
424135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
425135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    Instruction *I = BBI++;
426135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
427135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // We only need to check for function calls: inlined invoke
428135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // instructions require no special handling.
429135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    CallInst *CI = dyn_cast<CallInst>(I);
430135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (CI == 0) continue;
431a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
432a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    // LIBUNWIND: merge selector instructions.
433a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    if (EHSelectorInst *Inner = dyn_cast<EHSelectorInst>(CI)) {
434d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      EHSelectorInst *Outer = Invoke.getOuterSelector();
435a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      if (!Outer) continue;
436a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
437a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      bool innerIsOnlyCleanup = isCleanupOnlySelector(Inner);
438a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      bool outerIsOnlyCleanup = isCleanupOnlySelector(Outer);
439a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
440a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // If both selectors contain only cleanups, we don't need to do
441a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // anything.  TODO: this is really just a very specific instance
442a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // of a much more general optimization.
443a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      if (innerIsOnlyCleanup && outerIsOnlyCleanup) continue;
444a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
445a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // Otherwise, we just append the outer selector to the inner selector.
446a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      SmallVector<Value*, 16> NewSelector;
447a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      for (unsigned i = 0, e = Inner->getNumArgOperands(); i != e; ++i)
448a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        NewSelector.push_back(Inner->getArgOperand(i));
449a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      for (unsigned i = 2, e = Outer->getNumArgOperands(); i != e; ++i)
450a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        NewSelector.push_back(Outer->getArgOperand(i));
451a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
452a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      CallInst *NewInner = CallInst::Create(Inner->getCalledValue(),
453a3de16bc8f36638d5444e3e7b0112998af54f826John McCall                                            NewSelector.begin(),
454a3de16bc8f36638d5444e3e7b0112998af54f826John McCall                                            NewSelector.end(),
455a3de16bc8f36638d5444e3e7b0112998af54f826John McCall                                            "",
456a3de16bc8f36638d5444e3e7b0112998af54f826John McCall                                            Inner);
457a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // No need to copy attributes, calling convention, etc.
458a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      NewInner->takeName(Inner);
459a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      Inner->replaceAllUsesWith(NewInner);
460a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      Inner->eraseFromParent();
461a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      continue;
462a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    }
463135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
464135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // If this call cannot unwind, don't convert it to an invoke.
465135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (CI->doesNotThrow())
466135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
467135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
468135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Convert this function call into an invoke instruction.
469135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // First, split the basic block.
470135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
471a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
472d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // Delete the unconditional branch inserted by splitBasicBlock
473d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    BB->getInstList().pop_back();
474a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
475d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // LIBUNWIND: If this is a call to @llvm.eh.resume, just branch
476a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    // directly to the new landing pad.
477d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    if (Invoke.forwardEHResume(CI, BB)) {
478a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // TODO: 'Split' is now unreachable; clean it up.
479a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
480a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // We want to leave the original call intact so that the call
481a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // graph and other structures won't get misled.  We also have to
482a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      // avoid processing the next block, or we'll iterate here forever.
483d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      return true;
484d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    }
485a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
486a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    // Otherwise, create the new invoke instruction.
487d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    ImmutableCallSite CS(CI);
488d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end());
489d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    InvokeInst *II =
490d7c10862016939c9850cadfe5e1c35513c0adf28John McCall      InvokeInst::Create(CI->getCalledValue(), Split,
491d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                         Invoke.getOuterUnwindDest(),
492d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                         InvokeArgs.begin(), InvokeArgs.end(),
493d7c10862016939c9850cadfe5e1c35513c0adf28John McCall                         CI->getName(), BB);
494d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    II->setCallingConv(CI->getCallingConv());
495d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    II->setAttributes(CI->getAttributes());
496135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
497d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // Make sure that anything using the call now uses the invoke!  This also
498d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    // updates the CallGraph if present, because it uses a WeakVH.
499d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    CI->replaceAllUsesWith(II);
500d7c10862016939c9850cadfe5e1c35513c0adf28John McCall
501d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    Split->getInstList().pop_front();  // Delete the original call
502a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
503135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Update any PHI nodes in the exceptional block to indicate that
504135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // there is now a new entry in them.
505a3de16bc8f36638d5444e3e7b0112998af54f826John McCall    Invoke.addIncomingPHIValuesFor(BB);
506d7c10862016939c9850cadfe5e1c35513c0adf28John McCall    return false;
507135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  }
508a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
509a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  return false;
510135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner}
511135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
512135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
513cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
514cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// in the body of the inlined function into invokes and turn unwind
515cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// instructions into branches to the invoke unwind dest.
516cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner///
517dac5c4b10b387b55c2394cd98a64f3f1394df2e8Nick Lewycky/// II is the invoke instruction being inlined.  FirstNewBlock is the first
518cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// block of the inlined code (the last block is the end of the function),
519cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner/// and InlineCodeInfo is information about the code that got inlined.
520cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattnerstatic void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
52181dfb3885252fbf621b080827a080099864415f8Chris Lattner                                ClonedCodeInfo &InlinedCodeInfo) {
522cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  BasicBlock *InvokeDest = II->getUnwindDest();
523cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
524cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  Function *Caller = FirstNewBlock->getParent();
525a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
526cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // The inlined code is currently at the end of the function, scan from the
527cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // start of the inlined code to its end, checking for stuff we need to
528135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // rewrite.  If the code doesn't have calls or unwinds, we know there is
529135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // nothing to rewrite.
530135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) {
531135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Now that everything is happy, we have one final detail.  The PHI nodes in
532135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // the exception destination block still have entries due to the original
533135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // invoke instruction.  Eliminate these entries (which might even delete the
534135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // PHI node) now.
535135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    InvokeDest->removePredecessor(II->getParent());
536135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    return;
537135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  }
538a3de16bc8f36638d5444e3e7b0112998af54f826John McCall
539a3de16bc8f36638d5444e3e7b0112998af54f826John McCall  InvokeInliningInfo Invoke(II);
540135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
541135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){
542135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (InlinedCodeInfo.ContainsCalls)
543a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      if (HandleCallsInBlockInlinedThroughInvoke(BB, Invoke)) {
544a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // Honor a request to skip the next block.  We don't need to
545a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        // consider UnwindInsts in this case either.
546a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        ++BB;
547a3de16bc8f36638d5444e3e7b0112998af54f826John McCall        continue;
548a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      }
549135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
550135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
551135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // An UnwindInst requires special handling when it gets inlined into an
552135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // invoke site.  Once this happens, we know that the unwind would cause
553135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // a control transfer to the invoke exception destination, so we can
554135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // transform it into a direct branch to the exception destination.
555135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      BranchInst::Create(InvokeDest, UI);
556135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
557135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Delete the unwind instruction!
558135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      UI->eraseFromParent();
559135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
560135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Update any PHI nodes in the exceptional block to indicate that
561135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // there is now a new entry in them.
562a3de16bc8f36638d5444e3e7b0112998af54f826John McCall      Invoke.addIncomingPHIValuesFor(BB);
563cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner    }
564cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  }
565cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
566cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // Now that everything is happy, we have one final detail.  The PHI nodes in
567cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // the exception destination block still have entries due to the original
568cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // invoke instruction.  Eliminate these entries (which might even delete the
569cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  // PHI node) now.
570cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  InvokeDest->removePredecessor(II->getParent());
571cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner}
572cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner
573d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee
574d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// into the caller, update the specified callgraph to reflect the changes we
575d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner/// made.  Note that it's possible that not all code was copied over, so only
576d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands/// some edges of the callgraph may remain.
577d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sandsstatic void UpdateCallGraphAfterInlining(CallSite CS,
578d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner                                         Function::iterator FirstNewBlock,
5791ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola                                         ValueToValueMapTy &VMap,
580fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner                                         InlineFunctionInfo &IFI) {
581fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner  CallGraph &CG = *IFI.CG;
582d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands  const Function *Caller = CS.getInstruction()->getParent()->getParent();
583d7b9851c4e634ed3599b1a4c70b1c76c90a11686Duncan Sands  const Function *Callee = CS.getCalledFunction();
584468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallGraphNode *CalleeNode = CG[Callee];
585468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  CallGraphNode *CallerNode = CG[Caller];
586a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
587d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  // Since we inlined some uninlined call sites in the callee into the caller,
588468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner  // add edges from the caller to all of the callees of the callee.
589c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
590c478e52bf4c12691037856ee103c66946afeab6cGabor Greif
591c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  // Consider the case where CalleeNode == CallerNode.
592125329891f97baedef21e4b464ba70182c3fb45eGabor Greif  CallGraphNode::CalledFunctionsVector CallCache;
593c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  if (CalleeNode == CallerNode) {
594c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    CallCache.assign(I, E);
595c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    I = CallCache.begin();
596c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    E = CallCache.end();
597c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  }
598c478e52bf4c12691037856ee103c66946afeab6cGabor Greif
599c478e52bf4c12691037856ee103c66946afeab6cGabor Greif  for (; I != E; ++I) {
600a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner    const Value *OrigCall = I->first;
601a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
6021ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola    ValueToValueMapTy::iterator VMI = VMap.find(OrigCall);
603981418bf1562d0b5b470ddc7d0034c9f3297b893Chris Lattner    // Only copy the edge if the call was inlined!
60429d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel    if (VMI == VMap.end() || VMI->second == 0)
605135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
606135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
607135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // If the call was inlined, but then constant folded, there is no edge to
608135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // add.  Check for this case.
609b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    Instruction *NewCall = dyn_cast<Instruction>(VMI->second);
610b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    if (NewCall == 0) continue;
6110ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner
6120ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    // Remember that this call site got inlined for the client of
6130ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    // InlineFunction.
6140ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner    IFI.InlinedCalls.push_back(NewCall);
6150ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner
616b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // It's possible that inlining the callsite will cause it to go from an
617b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // indirect to a direct call by resolving a function pointer.  If this
618b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // happens, set the callee of the new call site to a more precise
619b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // destination.  This can also happen if the call graph node of the caller
620b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    // was just unnecessarily imprecise.
621b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner    if (I->second->getFunction() == 0)
622b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner      if (Function *F = CallSite(NewCall).getCalledFunction()) {
623b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner        // Indirect call site resolved to direct call.
62486099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif        CallerNode->addCalledFunction(CallSite(NewCall), CG[F]);
62586099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif
626b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner        continue;
627b957a5e41ed6288f4d033167f6413621a09655eeChris Lattner      }
62886099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif
62986099345db95fdce6960ab62fbd9cb0cf96875f7Gabor Greif    CallerNode->addCalledFunction(CallSite(NewCall), I->second);
630d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner  }
631135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
63239fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  // Update the call graph by deleting the edge from Callee to Caller.  We must
63339fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  // do this after the loop above in case Caller and Callee are the same.
63439fa32403e0b5e163f4f05566d6cde65e6c11095Dale Johannesen  CallerNode->removeCallEdgeFor(CS);
635468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner}
636468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
6370b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// HandleByValArgument - When inlining a call site that has a byval argument,
6380b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner/// we have to make the implicit memcpy explicit by adding it.
639e7ae705c32906979a527926864345016e76867b9Chris Lattnerstatic Value *HandleByValArgument(Value *Arg, Instruction *TheCall,
640e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  const Function *CalledFunc,
641e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  InlineFunctionInfo &IFI,
642e7ae705c32906979a527926864345016e76867b9Chris Lattner                                  unsigned ByValAlignment) {
6430b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  const Type *AggTy = cast<PointerType>(Arg->getType())->getElementType();
6440b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6450b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // If the called function is readonly, then it could not mutate the caller's
6460b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // copy of the byval'd memory.  In this case, it is safe to elide the copy and
6470b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  // temporary.
6480b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  if (CalledFunc->onlyReadsMemory()) {
6490b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // If the byval argument has a specified alignment that is greater than the
6500b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // passed in pointer, then we either have to round up the input pointer or
6510b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    // give up on this transformation.
6520b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner    if (ByValAlignment <= 1)  // 0 = unspecified, 1 = no particular alignment.
6530b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner      return Arg;
6540b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6557569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // If the pointer is already known to be sufficiently aligned, or if we can
6567569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // round it up to a larger alignment, then we don't need a temporary.
6577569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    if (getOrEnforceKnownAlignment(Arg, ByValAlignment,
6587569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner                                   IFI.TD) >= ByValAlignment)
6597569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner      return Arg;
6600b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner
6617569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // Otherwise, we have to make a memcpy to get a safe alignment.  This is bad
6627569d79de1bd97a6a17b81340ea6ce97d8a3c279Chris Lattner    // for code quality, but rarely happens and is required for correctness.
6630b66f63a26387f5c0360a4324fc3c31e0599a6e0Chris Lattner  }
664e7ae705c32906979a527926864345016e76867b9Chris Lattner
665e7ae705c32906979a527926864345016e76867b9Chris Lattner  LLVMContext &Context = Arg->getContext();
666e7ae705c32906979a527926864345016e76867b9Chris Lattner
667e7ae705c32906979a527926864345016e76867b9Chris Lattner  const Type *VoidPtrTy = Type::getInt8PtrTy(Context);
668e7ae705c32906979a527926864345016e76867b9Chris Lattner
669e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Create the alloca.  If we have TargetData, use nice alignment.
670e7ae705c32906979a527926864345016e76867b9Chris Lattner  unsigned Align = 1;
671e7ae705c32906979a527926864345016e76867b9Chris Lattner  if (IFI.TD)
672e7ae705c32906979a527926864345016e76867b9Chris Lattner    Align = IFI.TD->getPrefTypeAlignment(AggTy);
673e7ae705c32906979a527926864345016e76867b9Chris Lattner
674e7ae705c32906979a527926864345016e76867b9Chris Lattner  // If the byval had an alignment specified, we *must* use at least that
675e7ae705c32906979a527926864345016e76867b9Chris Lattner  // alignment, as it is required by the byval argument (and uses of the
676e7ae705c32906979a527926864345016e76867b9Chris Lattner  // pointer inside the callee).
677e7ae705c32906979a527926864345016e76867b9Chris Lattner  Align = std::max(Align, ByValAlignment);
678e7ae705c32906979a527926864345016e76867b9Chris Lattner
679e7ae705c32906979a527926864345016e76867b9Chris Lattner  Function *Caller = TheCall->getParent()->getParent();
680e7ae705c32906979a527926864345016e76867b9Chris Lattner
681e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *NewAlloca = new AllocaInst(AggTy, 0, Align, Arg->getName(),
682e7ae705c32906979a527926864345016e76867b9Chris Lattner                                    &*Caller->begin()->begin());
683e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Emit a memcpy.
684e7ae705c32906979a527926864345016e76867b9Chris Lattner  const Type *Tys[3] = {VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context)};
685e7ae705c32906979a527926864345016e76867b9Chris Lattner  Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
686e7ae705c32906979a527926864345016e76867b9Chris Lattner                                                 Intrinsic::memcpy,
687e7ae705c32906979a527926864345016e76867b9Chris Lattner                                                 Tys, 3);
688e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
689e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *SrcCast = new BitCastInst(Arg, VoidPtrTy, "tmp", TheCall);
690e7ae705c32906979a527926864345016e76867b9Chris Lattner
691e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *Size;
692e7ae705c32906979a527926864345016e76867b9Chris Lattner  if (IFI.TD == 0)
693e7ae705c32906979a527926864345016e76867b9Chris Lattner    Size = ConstantExpr::getSizeOf(AggTy);
694e7ae705c32906979a527926864345016e76867b9Chris Lattner  else
695e7ae705c32906979a527926864345016e76867b9Chris Lattner    Size = ConstantInt::get(Type::getInt64Ty(Context),
696e7ae705c32906979a527926864345016e76867b9Chris Lattner                            IFI.TD->getTypeStoreSize(AggTy));
697e7ae705c32906979a527926864345016e76867b9Chris Lattner
698e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Always generate a memcpy of alignment 1 here because we don't know
699e7ae705c32906979a527926864345016e76867b9Chris Lattner  // the alignment of the src pointer.  Other optimizations can infer
700e7ae705c32906979a527926864345016e76867b9Chris Lattner  // better alignment.
701e7ae705c32906979a527926864345016e76867b9Chris Lattner  Value *CallArgs[] = {
702e7ae705c32906979a527926864345016e76867b9Chris Lattner    DestCast, SrcCast, Size,
703e7ae705c32906979a527926864345016e76867b9Chris Lattner    ConstantInt::get(Type::getInt32Ty(Context), 1),
704e7ae705c32906979a527926864345016e76867b9Chris Lattner    ConstantInt::getFalse(Context) // isVolatile
705e7ae705c32906979a527926864345016e76867b9Chris Lattner  };
706e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall  CallInst::Create(MemCpyFn, CallArgs, CallArgs+5, "", TheCall);
707e7ae705c32906979a527926864345016e76867b9Chris Lattner
708e7ae705c32906979a527926864345016e76867b9Chris Lattner  // Uses of the argument in the function should use our new alloca
709e7ae705c32906979a527926864345016e76867b9Chris Lattner  // instead.
710e7ae705c32906979a527926864345016e76867b9Chris Lattner  return NewAlloca;
711e7ae705c32906979a527926864345016e76867b9Chris Lattner}
712e7ae705c32906979a527926864345016e76867b9Chris Lattner
7136d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// isUsedByLifetimeMarker - Check whether this Value is used by a lifetime
7146d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// intrinsic.
7156d55f2269e20298a1d6a683be72d9552482156a9Nick Lewyckystatic bool isUsedByLifetimeMarker(Value *V) {
7166d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;
7176d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky       ++UI) {
7186d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*UI)) {
7196d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      switch (II->getIntrinsicID()) {
7206d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      default: break;
7216d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      case Intrinsic::lifetime_start:
7226d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      case Intrinsic::lifetime_end:
7236d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        return true;
7246d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      }
7256d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    }
7266d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
7276d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  return false;
7286d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky}
7296d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
7306d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// hasLifetimeMarkers - Check whether the given alloca already has
7316d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky// lifetime.start or lifetime.end intrinsics.
7326d55f2269e20298a1d6a683be72d9552482156a9Nick Lewyckystatic bool hasLifetimeMarkers(AllocaInst *AI) {
7336d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  const Type *Int8PtrTy = Type::getInt8PtrTy(AI->getType()->getContext());
7346d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  if (AI->getType() == Int8PtrTy)
7356d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    return isUsedByLifetimeMarker(AI);
7366d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
737708c1ac077fbc0cb73d489b4f4df3b2718566b05Nick Lewycky  // Do a scan to find all the casts to i8*.
7386d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); I != E;
7396d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky       ++I) {
7406d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (I->getType() != Int8PtrTy) continue;
741708c1ac077fbc0cb73d489b4f4df3b2718566b05Nick Lewycky    if (I->stripPointerCasts() != AI) continue;
7426d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    if (isUsedByLifetimeMarker(*I))
7436d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      return true;
7446d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
7456d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  return false;
7466d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky}
7476d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
748ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// InlineFunction - This function inlines the called function into the basic
749ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// block of the caller.  This returns false if it is not possible to inline this
750ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// call.  The program is still in a well defined state if this occurs though.
751ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
752fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// Note that this only does one level of inlining.  For example, if the
753fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
7547a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner// exists in the instruction stream.  Similarly this will inline a recursive
755ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// function by one level.
756ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner//
75760915146f4d35e12f10dcdaa155596fac79184daChris Lattnerbool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
75880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  Instruction *TheCall = CS.getInstruction();
759e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson  LLVMContext &Context = TheCall->getContext();
76080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
76180a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner         "Instruction not in function!");
762ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
76360915146f4d35e12f10dcdaa155596fac79184daChris Lattner  // If IFI has any state in it, zap it before we fill it in.
76460915146f4d35e12f10dcdaa155596fac79184daChris Lattner  IFI.reset();
76560915146f4d35e12f10dcdaa155596fac79184daChris Lattner
76680a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  const Function *CalledFunc = CS.getCalledFunction();
767ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  if (CalledFunc == 0 ||          // Can't inline external function or indirect
7685cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer      CalledFunc->isDeclaration() || // call, or call to a vararg function!
7690623e90398153be61226ad19f1b40d3817874526Eric Christopher      CalledFunc->getFunctionType()->isVarArg()) return false;
770ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
771af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner  // If the call to the callee is not a tail call, we must clear the 'tail'
7721b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  // flags on any calls that we inline.
7731b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  bool MustClearTailCallFlags =
774af9985c6b9d066cb70a0363fed699022d0ec196cChris Lattner    !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
7751b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
776f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // If the call to the callee cannot throw, set the 'nounwind' flag on any
777f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // calls that we inline.
778f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  bool MarkNoUnwind = CS.doesNotThrow();
779f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
78080a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  BasicBlock *OrigBB = TheCall->getParent();
781ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  Function *Caller = OrigBB->getParent();
782ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
7830e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  // GC poses two hazards to inlining, which only occur when the callee has GC:
7840e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //  1. If the caller has no GC, then the callee's GC must be propagated to the
7850e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //     caller.
7860e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  //  2. If the caller has a differing GC, it is invalid to inline.
7875eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  if (CalledFunc->hasGC()) {
7885eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    if (!Caller->hasGC())
7895eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen      Caller->setGC(CalledFunc->getGC());
7905eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    else if (CalledFunc->getGC() != Caller->getGC())
7910e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen      return false;
7920e13821c96937830ec817f08095c3cef1fdcac8dGordon Henriksen  }
793a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
7945052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // Get an iterator to the last basic block in the function, which will have
7955052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  // the new function inlined after it.
7965052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  //
7975052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner  Function::iterator LastBlock = &Caller->back();
7985052c911ec1be51ecb36e7f025c26412e9f1bfacChris Lattner
7995e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Make sure to capture all of the return instructions from the cloned
8005e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // function.
801ec1bea0d94372985a0a5eb283e644c6d0dd345dcChris Lattner  SmallVector<ReturnInst*, 8> Returns;
802cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  ClonedCodeInfo InlinedFunctionInfo;
8030744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen  Function::iterator FirstNewBlock;
804f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
80529d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel  { // Scope to destroy VMap after cloning.
8061ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola    ValueToValueMapTy VMap;
8075b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner
8089614fcc640eb628cc5dfddb277ebae9f6cb61014Dan Gohman    assert(CalledFunc->arg_size() == CS.arg_size() &&
8095e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner           "No varargs calls can be inlined!");
810a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
811c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    // Calculate the vector of arguments to pass into the function cloner, which
812c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    // matches up the formal to the actual argument values.
8135e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    CallSite::arg_iterator AI = CS.arg_begin();
814c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    unsigned ArgNo = 0;
815e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner    for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
816c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner         E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
817c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner      Value *ActualArg = *AI;
818a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
819d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // When byval arguments actually inlined, we need to make the copy implied
820d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // by them explicit.  However, we don't do this if the callee is readonly
821d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // or readnone, because the copy would be unneeded: the callee doesn't
822d82375c1c43db7f823dd4660d3495e76566699e3Duncan Sands      // modify the struct.
823e7ae705c32906979a527926864345016e76867b9Chris Lattner      if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal)) {
824e7ae705c32906979a527926864345016e76867b9Chris Lattner        ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI,
825e7ae705c32906979a527926864345016e76867b9Chris Lattner                                        CalledFunc->getParamAlignment(ArgNo+1));
826e7ae705c32906979a527926864345016e76867b9Chris Lattner
8272914ba6ec793e2bb0e9ca5891af1d29ee2fee28eDuncan Sands        // Calls that we inline may use the new alloca, so we need to clear
828e7ae705c32906979a527926864345016e76867b9Chris Lattner        // their 'tail' flags if HandleByValArgument introduced a new alloca and
829e7ae705c32906979a527926864345016e76867b9Chris Lattner        // the callee has calls.
830e7ae705c32906979a527926864345016e76867b9Chris Lattner        MustClearTailCallFlags |= ActualArg != *AI;
831c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner      }
832a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
83329d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel      VMap[I] = ActualArg;
834c93adca35856d0eaae088e52ba30cfefbd7ad910Chris Lattner    }
835fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
8365b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // We want the inliner to prune the code as it copies.  We would LOVE to
8375b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // have no dead or constant instructions leftover after inlining occurs
8385b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // (which can happen, e.g., because an argument was constant), but we'll be
8395b5bc3032f97cfa7bfa3e22282d3a9c1ed05eec6Chris Lattner    // happy with whatever the cloner can do.
8406cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman    CloneAndPruneFunctionInto(Caller, CalledFunc, VMap,
8416cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman                              /*ModuleLevelChanges=*/false, Returns, ".i",
84260915146f4d35e12f10dcdaa155596fac79184daChris Lattner                              &InlinedFunctionInfo, IFI.TD, TheCall);
843a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
844d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // Remember the first block that is newly cloned over.
845d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    FirstNewBlock = LastBlock; ++FirstNewBlock;
846a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
847d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    // Update the callgraph if requested.
84860915146f4d35e12f10dcdaa155596fac79184daChris Lattner    if (IFI.CG)
84929d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel      UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI);
850fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  }
851a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
852ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // If there are any alloca instructions in the block that used to be the entry
853ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // block for the callee, move them to the entry block of the caller.  First
854ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // calculate which instruction they should be inserted before.  We insert the
855ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  // instructions at the end of the current alloca list.
856ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  //
85721f20558d629f7ff8f64c20746d890d29328a544Chris Lattner  {
85880a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner    BasicBlock::iterator InsertPoint = Caller->begin()->begin();
8595e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    for (BasicBlock::iterator I = FirstNewBlock->begin(),
860135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner         E = FirstNewBlock->end(); I != E; ) {
861135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      AllocaInst *AI = dyn_cast<AllocaInst>(I++);
862135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (AI == 0) continue;
863135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
864135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // If the alloca is now dead, remove it.  This often occurs due to code
865135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // specialization.
866135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (AI->use_empty()) {
867135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        AI->eraseFromParent();
868135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        continue;
86933bb3c8be355d179ece8e751f6e0f0978d0dd038Chris Lattner      }
870135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
871135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (!isa<Constant>(AI->getArraySize()))
872135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        continue;
873135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
87439add23dc54a1580983b1901384688d6622daa3bChris Lattner      // Keep track of the static allocas that we inline into the caller.
87560915146f4d35e12f10dcdaa155596fac79184daChris Lattner      IFI.StaticAllocas.push_back(AI);
8768f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner
877135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Scan for the block of allocas that we can move over, and move them
878135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // all at once.
879135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      while (isa<AllocaInst>(I) &&
8808f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner             isa<Constant>(cast<AllocaInst>(I)->getArraySize())) {
88160915146f4d35e12f10dcdaa155596fac79184daChris Lattner        IFI.StaticAllocas.push_back(cast<AllocaInst>(I));
882135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        ++I;
8838f2718fbef6177966ff807af0732eb2431bd9a5fChris Lattner      }
884135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
885135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Transfer all of the allocas over in a block.  Using splice means
886135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // that the instructions aren't removed from the symbol table, then
887135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // reinserted.
888135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      Caller->getEntryBlock().getInstList().splice(InsertPoint,
889135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner                                                   FirstNewBlock->getInstList(),
890135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner                                                   AI, I);
891135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    }
89280a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner  }
89380a38d245359cb0a3be8f78f5d7d911232886b9aChris Lattner
8946d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  // Leave lifetime markers for the static alloca's, scoping them to the
8956d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  // function we just inlined.
8966d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  if (!IFI.StaticAllocas.empty()) {
8976d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    IRBuilder<> builder(FirstNewBlock->begin());
8986d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    for (unsigned ai = 0, ae = IFI.StaticAllocas.size(); ai != ae; ++ai) {
8996d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      AllocaInst *AI = IFI.StaticAllocas[ai];
9006d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
9016d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      // If the alloca is already scoped to something smaller than the whole
9026d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      // function then there's no need to add redundant, less accurate markers.
9036d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      if (hasLifetimeMarkers(AI))
9046d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        continue;
9056d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
906e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall      builder.CreateLifetimeStart(AI);
9076d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      for (unsigned ri = 0, re = Returns.size(); ri != re; ++ri) {
9086d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky        IRBuilder<> builder(Returns[ri]);
909e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall        builder.CreateLifetimeEnd(AI);
9106d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky      }
9116d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky    }
9126d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky  }
9136d55f2269e20298a1d6a683be72d9552482156a9Nick Lewycky
914bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // If the inlined code contained dynamic alloca instructions, wrap the inlined
915bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  // code with llvm.stacksave/llvm.stackrestore intrinsics.
916bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  if (InlinedFunctionInfo.ContainsDynamicAllocas) {
917bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    Module *M = Caller->getParent();
918bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Get the two intrinsics we care about.
9196128df525501c333a650d097703c18d7e878f5e8Chris Lattner    Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
9206128df525501c333a650d097703c18d7e878f5e8Chris Lattner    Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore);
921d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner
922bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert the llvm.stacksave.
923a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands    CallInst *SavedPtr = CallInst::Create(StackSave, "savedstack",
924051a950000e21935165db56695e35bade668193bGabor Greif                                          FirstNewBlock->begin());
925a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
926bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // Insert a call to llvm.stackrestore before any return instructions in the
927bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // inlined function.
928d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
929e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall      CallInst::Create(StackRestore, SavedPtr, "", Returns[i]);
930d85340f4ec587e22b0239617f3b747a6df113894Chris Lattner    }
931468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner
932468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    // Count the number of StackRestore calls we insert.
933468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    unsigned NumStackRestores = Returns.size();
934a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
935bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // If we are inlining an invoke instruction, insert restores before each
936bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    // unwind.  These unwinds will be rewritten into branches later.
937bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner    if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
938bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner      for (Function::iterator BB = FirstNewBlock, E = Caller->end();
939bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner           BB != E; ++BB)
940468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner        if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
941e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall          CallInst::Create(StackRestore, SavedPtr, "", UI);
942468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner          ++NumStackRestores;
943468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner        }
944468fb1df7db466e5682ee44341c3990b599e8d6aChris Lattner    }
945bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner  }
946bf229f488a7541abd979cc3fbe9c3ce1c100e5c0Chris Lattner
947a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands  // If we are inlining tail call instruction through a call site that isn't
9481fdf4a859f03ce5aa4ce9acba29ce321c847388eChris Lattner  // marked 'tail', we must remove the tail marker for any calls in the inlined
949f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // code.  Also, calls inlined through a 'nounwind' call site should be marked
950f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // 'nounwind'.
951f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  if (InlinedFunctionInfo.ContainsCalls &&
952f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      (MustClearTailCallFlags || MarkNoUnwind)) {
9531b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
9541b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner         BB != E; ++BB)
9551b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
956f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        if (CallInst *CI = dyn_cast<CallInst>(I)) {
957f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands          if (MustClearTailCallFlags)
958f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands            CI->setTailCall(false);
959f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands          if (MarkNoUnwind)
960f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands            CI->setDoesNotThrow();
961f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        }
9621b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner  }
9631b49141821e98e4321bfe6234c7001c836b2a289Chris Lattner
964f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // If we are inlining through a 'nounwind' call site then any inlined 'unwind'
965f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  // instructions are unreachable.
966f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands  if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind)
967f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands    for (Function::iterator BB = FirstNewBlock, E = Caller->end();
968f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands         BB != E; ++BB) {
969f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      TerminatorInst *Term = BB->getTerminator();
970f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      if (isa<UnwindInst>(Term)) {
9711d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson        new UnreachableInst(Context, Term);
972f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands        BB->getInstList().erase(Term);
973f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands      }
974f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands    }
975f0c3354d998507515ab39e26b5292ea0ceb06aefDuncan Sands
9765e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // If we are inlining for an invoke instruction, we must make sure to rewrite
9775e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any inlined 'unwind' instructions into branches to the invoke exception
9785e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // destination, and call instructions into invoke instructions.
979cd4d339ec187182c9abc22b80560349f8ba5010fChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
98081dfb3885252fbf621b080827a080099864415f8Chris Lattner    HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
9815e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
98244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // If we cloned in _exactly one_ basic block, and if that block ends in a
98344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // return instruction, we splice the body of the inlined callee directly into
98444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // the calling basic block.
98544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
98644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Move all of the instructions right before the call.
98744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
98844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                 FirstNewBlock->begin(), FirstNewBlock->end());
98944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Remove the cloned basic block.
99044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    Caller->getBasicBlockList().pop_back();
991fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
99244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the call site was an invoke instruction, add a branch to the normal
99344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // destination.
99444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
995051a950000e21935165db56695e35bade668193bGabor Greif      BranchInst::Create(II->getNormalDest(), TheCall);
99644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
99744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If the return instruction returned a value, replace uses of the call with
99844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // uses of the returned value.
999dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel    if (!TheCall->use_empty()) {
1000dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel      ReturnInst *R = Returns[0];
10015877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      if (TheCall == R->getReturnValue())
10029e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson        TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
10035877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      else
10045877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman        TheCall->replaceAllUsesWith(R->getReturnValue());
1005dc00d42bb18a6748f43c365d9bd30c1ed0e800acDevang Patel    }
100644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the Call/Invoke, we can delete it.
10071adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman    TheCall->eraseFromParent();
100844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
100944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // Since we are now done with the return instruction, delete it also.
10101adec83ae84031bfa9f0bf209c5ee6c64906a1ffDan Gohman    Returns[0]->eraseFromParent();
101144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
101244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // We are now done with the inlining.
101344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    return true;
101444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  }
101544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
101644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Otherwise, we have the normal case, of more than one block to inline or
101744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // multiple return sites.
101844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
10195e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // We want to clone the entire callee function into the hole between the
10205e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // "starter" and "ender" blocks.  How we accomplish this depends on whether
10215e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // this is an invoke instruction or a call instruction.
10225e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  BasicBlock *AfterCallBB;
10235e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
1024fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
10255e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Add an unconditional branch to make this look like the CallInst case...
1026051a950000e21935165db56695e35bade668193bGabor Greif    BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall);
1027fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
10285e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // Split the basic block.  This guarantees that no PHI nodes will have to be
10295e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // updated due to new incoming edges, and make the invoke case more
10305e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // symmetric to the call case.
10315e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(NewBr,
1032284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
1033fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
10345e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  } else {  // It's a call
103544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // If this is a call instruction, we need to split the basic block that
103644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner    // the call lives in.
10375e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    //
10385e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    AfterCallBB = OrigBB->splitBasicBlock(TheCall,
1039284d1b88273eb1967c8faef407f1167791c760e0Chris Lattner                                          CalledFunc->getName()+".exit");
10405e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
10415e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner
104244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Change the branch that used to go to AfterCallBB to branch to the first
104344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // basic block of the inlined function.
104444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  //
104544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  TerminatorInst *Br = OrigBB->getTerminator();
1046fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  assert(Br && Br->getOpcode() == Instruction::Br &&
104744a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner         "splitBasicBlock broken!");
104844a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Br->setOperand(0, FirstNewBlock);
104944a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
105044a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
105144a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // Now that the function is correct, make it a little bit nicer.  In
105244a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // particular, move the basic blocks inserted from the end of the function
105344a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  // into the space made by splitting the source basic block.
105444a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner  Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
105544a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner                                     FirstNewBlock, Caller->end());
105644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
10575e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Handle all of the return instructions that we just cloned in, and eliminate
10585e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // any users of the original call/invoke instruction.
1059b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel  const Type *RTy = CalledFunc->getReturnType();
10602c31750cd0ebdc83a890ace97dbb6249b3abe44eDan Gohman
10616fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  PHINode *PHI = 0;
1062fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman  if (Returns.size() > 1) {
10635e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // The PHI node should go at the front of the new basic block to merge all
10645e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    // possible incoming values.
10655e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    if (!TheCall->use_empty()) {
10663ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad      PHI = PHINode::Create(RTy, Returns.size(), TheCall->getName(),
1067fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman                            AfterCallBB->begin());
1068fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      // Anything that used the result of the function call should now use the
1069fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      // PHI node as their operand.
1070a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands      TheCall->replaceAllUsesWith(PHI);
10715e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
1072fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
1073c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    // Loop over all of the return instructions adding entries to the PHI node
1074c478e52bf4c12691037856ee103c66946afeab6cGabor Greif    // as appropriate.
1075fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman    if (PHI) {
1076fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman      for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
1077fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        ReturnInst *RI = Returns[i];
1078fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        assert(RI->getReturnValue()->getType() == PHI->getType() &&
1079fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman               "Ret value not consistent in function!");
1080fc74abfba5128544a750fce22fdf13eb0403e3ceDan Gohman        PHI->addIncoming(RI->getReturnValue(), RI->getParent());
10815e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner      }
108212a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel    }
1083fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
1084c581acbbba3cb1af6a08e17314b26344333f9267Chris Lattner
1085de62aeaec49ddcf4a4c61fbbb3a22d3a4dd448f0Gabor Greif    // Add a branch to the merge points and remove return instructions.
108612a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel    for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
108712a466b9d0e27be453cfa05cff18acc9d7c1cfbaDevang Patel      ReturnInst *RI = Returns[i];
10880744f09efc53d3352ac1caffc61f6e8239201c3bDale Johannesen      BranchInst::Create(AfterCallBB, RI);
1089b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel      RI->eraseFromParent();
10905e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner    }
1091b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel  } else if (!Returns.empty()) {
1092b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Otherwise, if there is exactly one return value, just replace anything
1093b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // using the return value of the call with the computed value.
10945877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman    if (!TheCall->use_empty()) {
10955877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      if (TheCall == Returns[0]->getReturnValue())
10969e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson        TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
10975877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman      else
10985877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman        TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
10995877ad7e90de2179c15914ff9e8f1c72152cac30Eli Friedman    }
1100a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
1101b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Splice the code from the return block into the block that it will return
1102b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // to, which contains the code that was after the call.
1103b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    BasicBlock *ReturnBB = Returns[0]->getParent();
1104b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    AfterCallBB->getInstList().splice(AfterCallBB->begin(),
1105b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel                                      ReturnBB->getInstList());
1106a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
1107b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
1108b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    ReturnBB->replaceAllUsesWith(AfterCallBB);
1109a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
1110b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    // Delete the return instruction now and empty ReturnBB now.
1111b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    Returns[0]->eraseFromParent();
1112b8f198af1b62f2ed48dcb914973a9d211dcba38bDevang Patel    ReturnBB->eraseFromParent();
11133787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  } else if (!TheCall->use_empty()) {
11143787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // No returns, but something is using the return value of the call.  Just
11153787e765facfad5ea62753922d940bcdd52afd57Chris Lattner    // nuke the result.
11169e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson    TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
11175e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  }
1118fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
11195e923dee6024248f58fa83a7c7299437f44f0d1aChris Lattner  // Since we are now done with the Call/Invoke, we can delete it.
11203787e765facfad5ea62753922d940bcdd52afd57Chris Lattner  TheCall->eraseFromParent();
1121ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner
11227152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // We should always be able to fold the entry block of the function into the
11237152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  // single predecessor of the block...
1124cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
11257152c237b46a920b29d5605af934766b8f9a07a1Chris Lattner  BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
112644a6807f4f785ecdd96b3aa67dad056677131b98Chris Lattner
1127cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Splice the code entry block into calling block, right before the
1128cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // unconditional branch.
1129cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
1130cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  CalleeEntry->replaceAllUsesWith(OrigBB);  // Update PHI nodes
1131cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
1132cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Remove the unconditional branch.
1133cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  OrigBB->getInstList().erase(Br);
1134cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner
1135cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  // Now we can remove the CalleeEntry block, which is now empty.
1136cd01ae5c7071fb99a665b2bbea7428d769792ab8Chris Lattner  Caller->getBasicBlockList().erase(CalleeEntry);
1137a7212e58260f6d1ead0c4eec7af400cf6c0d289eDuncan Sands
11386fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // If we inserted a phi node, check to see if it has a single value (e.g. all
11396fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // the entries are the same or undef).  If so, remove the PHI so it doesn't
11406fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  // block other optimizations.
11416fb881c036c32728c4a128d81b6083457e534e09Duncan Sands  if (PHI)
11426fb881c036c32728c4a128d81b6083457e534e09Duncan Sands    if (Value *V = SimplifyInstruction(PHI, IFI.TD)) {
11436fb881c036c32728c4a128d81b6083457e534e09Duncan Sands      PHI->replaceAllUsesWith(V);
11446fb881c036c32728c4a128d81b6083457e534e09Duncan Sands      PHI->eraseFromParent();
11456fb881c036c32728c4a128d81b6083457e534e09Duncan Sands    }
11466fb881c036c32728c4a128d81b6083457e534e09Duncan Sands
1147ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner  return true;
1148ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner}
1149