[WinEH] Update catchrets with cloned successors
[oota-llvm.git] / lib / CodeGen / WinEHPrepare.cpp
index 957064891d7189d44dc35a9e8e801b86bce11618..3d1c38031946a2c7bcfcdc9f24d604f8e59df41a 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/Passes.h"
+#include "llvm/ADT/MapVector.h"
 #include "llvm/Analysis/CFG.h"
-#include "llvm/Analysis/LibCallSemantics.h"
+#include "llvm/Analysis/EHPersonalities.h"
+#include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/WinEHFuncInfo.h"
+#include "llvm/IR/Verifier.h"
+#include "llvm/MC/MCSymbol.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
@@ -44,7 +48,7 @@ static cl::opt<bool> DisableCleanups(
     cl::init(false));
 
 namespace {
-
+  
 class WinEHPrepare : public FunctionPass {
 public:
   static char ID; // Pass identification, replacement for typeid.
@@ -68,27 +72,20 @@ private:
   AllocaInst *insertPHILoads(PHINode *PN, Function &F);
   void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
                           DenseMap<BasicBlock *, Value *> &Loads, Function &F);
-  void demoteNonlocalUses(Value *V, std::set<BasicBlock *> &ColorsForBB,
-                          Function &F);
-  bool prepareExplicitEH(Function &F,
-                         SmallVectorImpl<BasicBlock *> &EntryBlocks);
-  void replaceTerminatePadWithCleanup(Function &F);
-  void colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks);
+  bool prepareExplicitEH(Function &F);
+  void colorFunclets(Function &F);
+
   void demotePHIsOnFunclets(Function &F);
-  void demoteUsesBetweenFunclets(Function &F);
-  void demoteArgumentUses(Function &F);
-  void cloneCommonBlocks(Function &F,
-                         SmallVectorImpl<BasicBlock *> &EntryBlocks);
-  void removeImplausibleTerminators(Function &F);
+  void cloneCommonBlocks(Function &F);
+  void removeImplausibleInstructions(Function &F);
   void cleanupPreparedFunclets(Function &F);
   void verifyPreparedFunclets(Function &F);
 
   // All fields are reset by runOnFunction.
   EHPersonality Personality = EHPersonality::Unknown;
 
-  std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
-  std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
-  std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
+  DenseMap<BasicBlock *, ColorVector> BlockColors;
+  MapVector<BasicBlock *, std::vector<BasicBlock *>> FuncletBlocks;
 };
 
 } // end anonymous namespace
@@ -101,21 +98,6 @@ FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
   return new WinEHPrepare(TM);
 }
 
-static void findFuncletEntryPoints(Function &Fn,
-                                   SmallVectorImpl<BasicBlock *> &EntryBlocks) {
-  EntryBlocks.push_back(&Fn.getEntryBlock());
-  for (BasicBlock &BB : Fn) {
-    Instruction *First = BB.getFirstNonPHI();
-    if (!First->isEHPad())
-      continue;
-    assert(!isa<LandingPadInst>(First) &&
-           "landingpad cannot be used with funclet EH personality");
-    // Find EH pad blocks that represent funclet start points.
-    if (!isa<CatchEndPadInst>(First) && !isa<CleanupEndPadInst>(First))
-      EntryBlocks.push_back(&BB);
-  }
-}
-
 bool WinEHPrepare::runOnFunction(Function &Fn) {
   if (!Fn.hasPersonalityFn())
     return false;
@@ -127,14 +109,7 @@ bool WinEHPrepare::runOnFunction(Function &Fn) {
   if (!isFuncletEHPersonality(Personality))
     return false;
 
-  // Remove unreachable blocks.  It is not valuable to assign them a color and
-  // their existence can trick us into thinking values are alive when they are
-  // not.
-  removeUnreachableBlocks(Fn);
-
-  SmallVector<BasicBlock *, 4> EntryBlocks;
-  findFuncletEntryPoints(Fn, EntryBlocks);
-  return prepareExplicitEH(Fn, EntryBlocks);
+  return prepareExplicitEH(Fn);
 }
 
 bool WinEHPrepare::doFinalization(Module &M) { return false; }
@@ -176,117 +151,146 @@ static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow,
   FuncInfo.TryBlockMap.push_back(TBME);
 }
 
-static const CatchPadInst *getSingleCatchPadPredecessor(const BasicBlock *BB) {
-  for (const BasicBlock *PredBlock : predecessors(BB))
-    if (auto *CPI = dyn_cast<CatchPadInst>(PredBlock->getFirstNonPHI()))
-      return CPI;
+static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad) {
+  for (const User *U : CleanupPad->users())
+    if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
+      return CRI->getUnwindDest();
   return nullptr;
 }
 
-/// Find all the catchpads that feed directly into the catchendpad. Frontends
-/// using this personality should ensure that each catchendpad and catchpad has
-/// one or zero catchpad predecessors.
-///
-/// The following C++ generates the IR after it:
-///   try {
-///   } catch (A) {
-///   } catch (B) {
-///   }
-///
-/// IR:
-///   %catchpad.A
-///     catchpad [i8* A typeinfo]
-///         to label %catch.A unwind label %catchpad.B
-///   %catchpad.B
-///     catchpad [i8* B typeinfo]
-///         to label %catch.B unwind label %endcatches
-///   %endcatches
-///     catchendblock unwind to caller
-static void
-findCatchPadsForCatchEndPad(const BasicBlock *CatchEndBB,
-                            SmallVectorImpl<const CatchPadInst *> &Handlers) {
-  const CatchPadInst *CPI = getSingleCatchPadPredecessor(CatchEndBB);
-  while (CPI) {
-    Handlers.push_back(CPI);
-    CPI = getSingleCatchPadPredecessor(CPI->getParent());
+static void calculateStateNumbersForInvokes(const Function *Fn,
+                                            WinEHFuncInfo &FuncInfo) {
+  auto *F = const_cast<Function *>(Fn);
+  DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(*F);
+  for (BasicBlock &BB : *F) {
+    auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
+    if (!II)
+      continue;
+
+    auto &BBColors = BlockColors[&BB];
+    assert(BBColors.size() == 1 && "multi-color BB not removed by preparation");
+    BasicBlock *FuncletEntryBB = BBColors.front();
+
+    BasicBlock *FuncletUnwindDest;
+    auto *FuncletPad =
+        dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHI());
+    assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock());
+    if (!FuncletPad)
+      FuncletUnwindDest = nullptr;
+    else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad))
+      FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest();
+    else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(FuncletPad))
+      FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad);
+    else
+      llvm_unreachable("unexpected funclet pad!");
+
+    BasicBlock *InvokeUnwindDest = II->getUnwindDest();
+    int BaseState = -1;
+    if (FuncletUnwindDest == InvokeUnwindDest) {
+      auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad);
+      if (BaseStateI != FuncInfo.FuncletBaseStateMap.end())
+        BaseState = BaseStateI->second;
+    }
+
+    if (BaseState != -1) {
+      FuncInfo.InvokeStateMap[II] = BaseState;
+    } else {
+      Instruction *PadInst = InvokeUnwindDest->getFirstNonPHI();
+      assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!");
+      FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst];
+    }
   }
-  // We've pushed these back into reverse source order.  Reverse them to get
-  // the list back into source order.
-  std::reverse(Handlers.begin(), Handlers.end());
 }
 
 // Given BB which ends in an unwind edge, return the EHPad that this BB belongs
 // to. If the unwind edge came from an invoke, return null.
-static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB) {
+static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB,
+                                                 Value *ParentPad) {
   const TerminatorInst *TI = BB->getTerminator();
   if (isa<InvokeInst>(TI))
     return nullptr;
-  if (TI->isEHPad())
+  if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
+    if (CatchSwitch->getParentPad() != ParentPad)
+      return nullptr;
     return BB;
-  return cast<CleanupReturnInst>(TI)->getCleanupPad()->getParent();
+  }
+  assert(!TI->isEHPad() && "unexpected EHPad!");
+  auto *CleanupPad = cast<CleanupReturnInst>(TI)->getCleanupPad();
+  if (CleanupPad->getParentPad() != ParentPad)
+    return nullptr;
+  return CleanupPad->getParent();
 }
 
-static void calculateExplicitCXXStateNumbers(WinEHFuncInfo &FuncInfo,
-                                             const BasicBlock &BB,
-                                             int ParentState) {
-  assert(BB.isEHPad());
-  const Instruction *FirstNonPHI = BB.getFirstNonPHI();
-  // All catchpad instructions will be handled when we process their
-  // respective catchendpad instruction.
-  if (isa<CatchPadInst>(FirstNonPHI))
-    return;
+static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo,
+                                     const Instruction *FirstNonPHI,
+                                     int ParentState) {
+  const BasicBlock *BB = FirstNonPHI->getParent();
+  assert(BB->isEHPad() && "not a funclet!");
+
+  if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
+    assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
+           "shouldn't revist catch funclets!");
 
-  if (isa<CatchEndPadInst>(FirstNonPHI)) {
     SmallVector<const CatchPadInst *, 2> Handlers;
-    findCatchPadsForCatchEndPad(&BB, Handlers);
-    const BasicBlock *FirstTryPad = Handlers.front()->getParent();
+    for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
+      auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI());
+      Handlers.push_back(CatchPad);
+    }
     int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
-    FuncInfo.EHPadStateMap[Handlers.front()] = TryLow;
-    for (const BasicBlock *PredBlock : predecessors(FirstTryPad))
-      if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
-        calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, TryLow);
+    FuncInfo.EHPadStateMap[CatchSwitch] = TryLow;
+    for (const BasicBlock *PredBlock : predecessors(BB))
+      if ((PredBlock = getEHPadFromPredecessor(PredBlock,
+                                               CatchSwitch->getParentPad())))
+        calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
+                                 TryLow);
     int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr);
 
     // catchpads are separate funclets in C++ EH due to the way rethrow works.
-    // In SEH, they aren't, so no invokes will unwind to the catchendpad.
-    FuncInfo.EHPadStateMap[FirstNonPHI] = CatchLow;
     int TryHigh = CatchLow - 1;
-    for (const BasicBlock *PredBlock : predecessors(&BB))
-      if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
-        calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CatchLow);
+    for (const auto *CatchPad : Handlers) {
+      FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow;
+      for (const User *U : CatchPad->users()) {
+        const auto *UserI = cast<Instruction>(U);
+        if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI))
+          if (InnerCatchSwitch->getUnwindDest() == CatchSwitch->getUnwindDest())
+            calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
+        if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI))
+          if (getCleanupRetUnwindDest(InnerCleanupPad) ==
+              CatchSwitch->getUnwindDest())
+            calculateCXXStateNumbers(FuncInfo, UserI, CatchLow);
+      }
+    }
     int CatchHigh = FuncInfo.getLastStateNumber();
     addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers);
-    DEBUG(dbgs() << "TryLow[" << FirstTryPad->getName() << "]: " << TryLow
+    DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n');
+    DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHigh << '\n');
+    DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHigh
                  << '\n');
-    DEBUG(dbgs() << "TryHigh[" << FirstTryPad->getName() << "]: " << TryHigh
-                 << '\n');
-    DEBUG(dbgs() << "CatchHigh[" << FirstTryPad->getName() << "]: " << CatchHigh
-                 << '\n');
-  } else if (isa<CleanupPadInst>(FirstNonPHI)) {
-    // A cleanup can have multiple exits; don't re-process after the first.
-    if (FuncInfo.EHPadStateMap.count(FirstNonPHI))
+  } else {
+    auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
+
+    // It's possible for a cleanup to be visited twice: it might have multiple
+    // cleanupret instructions.
+    if (FuncInfo.EHPadStateMap.count(CleanupPad))
       return;
-    int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, &BB);
-    FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
+
+    int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, BB);
+    FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
     DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
-                 << BB.getName() << '\n');
-    for (const BasicBlock *PredBlock : predecessors(&BB))
-      if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
-        calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, CleanupState);
-  } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) {
-    // Propagate ParentState to the cleanuppad in case it doesn't have
-    // any cleanuprets.
-    BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent();
-    calculateExplicitCXXStateNumbers(FuncInfo, *CleanupBlock, ParentState);
-    // Anything unwinding through CleanupEndPadInst is in ParentState.
-    FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
-    for (const BasicBlock *PredBlock : predecessors(&BB))
-      if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
-        calculateExplicitCXXStateNumbers(FuncInfo, *PredBlock, ParentState);
-  } else if (isa<TerminatePadInst>(FirstNonPHI)) {
-    report_fatal_error("Not yet implemented!");
-  } else {
-    llvm_unreachable("unexpected EH Pad!");
+                 << BB->getName() << '\n');
+    for (const BasicBlock *PredBlock : predecessors(BB)) {
+      if ((PredBlock = getEHPadFromPredecessor(PredBlock,
+                                               CleanupPad->getParentPad()))) {
+        calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
+                                 CleanupState);
+      }
+    }
+    for (const User *U : CleanupPad->users()) {
+      const auto *UserI = cast<Instruction>(U);
+      if (UserI->isEHPad())
+        report_fatal_error("Cleanup funclets for the MSVC++ personality cannot "
+                           "contain exceptional actions");
+    }
   }
 }
 
@@ -312,94 +316,88 @@ static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState,
   return FuncInfo.SEHUnwindMap.size() - 1;
 }
 
-static void calculateExplicitSEHStateNumbers(WinEHFuncInfo &FuncInfo,
-                                             const BasicBlock &BB,
-                                             int ParentState) {
-  assert(BB.isEHPad());
-  const Instruction *FirstNonPHI = BB.getFirstNonPHI();
-  // All catchpad instructions will be handled when we process their
-  // respective catchendpad instruction.
-  if (isa<CatchPadInst>(FirstNonPHI))
-    return;
+static void calculateSEHStateNumbers(WinEHFuncInfo &FuncInfo,
+                                     const Instruction *FirstNonPHI,
+                                     int ParentState) {
+  const BasicBlock *BB = FirstNonPHI->getParent();
+  assert(BB->isEHPad() && "no a funclet!");
+
+  if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) {
+    assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 &&
+           "shouldn't revist catch funclets!");
 
-  if (isa<CatchEndPadInst>(FirstNonPHI)) {
     // Extract the filter function and the __except basic block and create a
     // state for them.
-    SmallVector<const CatchPadInst *, 1> Handlers;
-    findCatchPadsForCatchEndPad(&BB, Handlers);
-    assert(Handlers.size() == 1 &&
+    assert(CatchSwitch->getNumHandlers() == 1 &&
            "SEH doesn't have multiple handlers per __try");
-    const CatchPadInst *CPI = Handlers.front();
-    const BasicBlock *CatchPadBB = CPI->getParent();
+    const auto *CatchPad =
+        cast<CatchPadInst>((*CatchSwitch->handler_begin())->getFirstNonPHI());
+    const BasicBlock *CatchPadBB = CatchPad->getParent();
     const Constant *FilterOrNull =
-        cast<Constant>(CPI->getArgOperand(0)->stripPointerCasts());
+        cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts());
     const Function *Filter = dyn_cast<Function>(FilterOrNull);
     assert((Filter || FilterOrNull->isNullValue()) &&
            "unexpected filter value");
     int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB);
 
     // Everything in the __try block uses TryState as its parent state.
-    FuncInfo.EHPadStateMap[CPI] = TryState;
+    FuncInfo.EHPadStateMap[CatchSwitch] = TryState;
     DEBUG(dbgs() << "Assigning state #" << TryState << " to BB "
                  << CatchPadBB->getName() << '\n');
-    for (const BasicBlock *PredBlock : predecessors(CatchPadBB))
-      if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
-        calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, TryState);
+    for (const BasicBlock *PredBlock : predecessors(BB))
+      if ((PredBlock = getEHPadFromPredecessor(PredBlock,
+                                               CatchSwitch->getParentPad())))
+        calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
+                                 TryState);
 
     // Everything in the __except block unwinds to ParentState, just like code
     // outside the __try.
-    FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
-    DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
-                 << BB.getName() << '\n');
-    for (const BasicBlock *PredBlock : predecessors(&BB))
-      if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
-        calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
-  } else if (isa<CleanupPadInst>(FirstNonPHI)) {
-    // A cleanup can have multiple exits; don't re-process after the first.
-    if (FuncInfo.EHPadStateMap.count(FirstNonPHI))
+    for (const User *U : CatchPad->users()) {
+      const auto *UserI = cast<Instruction>(U);
+      if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI))
+        if (InnerCatchSwitch->getUnwindDest() == CatchSwitch->getUnwindDest())
+          calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
+      if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI))
+        if (getCleanupRetUnwindDest(InnerCleanupPad) ==
+            CatchSwitch->getUnwindDest())
+          calculateSEHStateNumbers(FuncInfo, UserI, ParentState);
+    }
+  } else {
+    auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI);
+
+    // It's possible for a cleanup to be visited twice: it might have multiple
+    // cleanupret instructions.
+    if (FuncInfo.EHPadStateMap.count(CleanupPad))
       return;
-    int CleanupState = addSEHFinally(FuncInfo, ParentState, &BB);
-    FuncInfo.EHPadStateMap[FirstNonPHI] = CleanupState;
+
+    int CleanupState = addSEHFinally(FuncInfo, ParentState, BB);
+    FuncInfo.EHPadStateMap[CleanupPad] = CleanupState;
     DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB "
-                 << BB.getName() << '\n');
-    for (const BasicBlock *PredBlock : predecessors(&BB))
-      if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
-        calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, CleanupState);
-  } else if (auto *CEPI = dyn_cast<CleanupEndPadInst>(FirstNonPHI)) {
-    // Propagate ParentState to the cleanuppad in case it doesn't have
-    // any cleanuprets.
-    BasicBlock *CleanupBlock = CEPI->getCleanupPad()->getParent();
-    calculateExplicitSEHStateNumbers(FuncInfo, *CleanupBlock, ParentState);
-    // Anything unwinding through CleanupEndPadInst is in ParentState.
-    FuncInfo.EHPadStateMap[FirstNonPHI] = ParentState;
-    DEBUG(dbgs() << "Assigning state #" << ParentState << " to BB "
-                 << BB.getName() << '\n');
-    for (const BasicBlock *PredBlock : predecessors(&BB))
-      if ((PredBlock = getEHPadFromPredecessor(PredBlock)))
-        calculateExplicitSEHStateNumbers(FuncInfo, *PredBlock, ParentState);
-  } else if (isa<TerminatePadInst>(FirstNonPHI)) {
-    report_fatal_error("Not yet implemented!");
-  } else {
-    llvm_unreachable("unexpected EH Pad!");
+                 << BB->getName() << '\n');
+    for (const BasicBlock *PredBlock : predecessors(BB))
+      if ((PredBlock =
+               getEHPadFromPredecessor(PredBlock, CleanupPad->getParentPad())))
+        calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(),
+                                 CleanupState);
+    for (const User *U : CleanupPad->users()) {
+      const auto *UserI = cast<Instruction>(U);
+      if (UserI->isEHPad())
+        report_fatal_error("Cleanup funclets for the SEH personality cannot "
+                           "contain exceptional actions");
+    }
   }
 }
 
-/// Check if the EH Pad unwinds to caller.  Cleanups are a little bit of a
-/// special case because we have to look at the cleanupret instruction that uses
-/// the cleanuppad.
-static bool doesEHPadUnwindToCaller(const Instruction *EHPad) {
-  auto *CPI = dyn_cast<CleanupPadInst>(EHPad);
-  if (!CPI)
-    return EHPad->mayThrow();
-
-  // This cleanup does not return or unwind, so we say it unwinds to caller.
-  if (CPI->use_empty())
-    return true;
-
-  const Instruction *User = CPI->user_back();
-  if (auto *CRI = dyn_cast<CleanupReturnInst>(User))
-    return CRI->unwindsToCaller();
-  return cast<CleanupEndPadInst>(User)->unwindsToCaller();
+static bool isTopLevelPadForMSVC(const Instruction *EHPad) {
+  if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(EHPad))
+    return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) &&
+           CatchSwitch->unwindsToCaller();
+  if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad))
+    return isa<ConstantTokenNone>(CleanupPad->getParentPad()) &&
+           getCleanupRetUnwindDest(CleanupPad) == nullptr;
+  if (isa<CatchPadInst>(EHPad))
+    return false;
+  llvm_unreachable("unexpected EHPad!");
 }
 
 void llvm::calculateSEHStateNumbers(const Function *Fn,
@@ -409,10 +407,15 @@ void llvm::calculateSEHStateNumbers(const Function *Fn,
     return;
 
   for (const BasicBlock &BB : *Fn) {
-    if (!BB.isEHPad() || !doesEHPadUnwindToCaller(BB.getFirstNonPHI()))
+    if (!BB.isEHPad())
+      continue;
+    const Instruction *FirstNonPHI = BB.getFirstNonPHI();
+    if (!isTopLevelPadForMSVC(FirstNonPHI))
       continue;
-    calculateExplicitSEHStateNumbers(FuncInfo, BB, -1);
+    ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1);
   }
+
+  calculateStateNumbersForInvokes(Fn, FuncInfo);
 }
 
 void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
@@ -424,13 +427,13 @@ void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,
   for (const BasicBlock &BB : *Fn) {
     if (!BB.isEHPad())
       continue;
-    if (BB.isLandingPad())
-      report_fatal_error("MSVC C++ EH cannot use landingpads");
     const Instruction *FirstNonPHI = BB.getFirstNonPHI();
-    if (!doesEHPadUnwindToCaller(FirstNonPHI))
+    if (!isTopLevelPadForMSVC(FirstNonPHI))
       continue;
-    calculateExplicitCXXStateNumbers(FuncInfo, BB, -1);
+    calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1);
   }
+
+  calculateStateNumbersForInvokes(Fn, FuncInfo);
 }
 
 static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int ParentState,
@@ -461,7 +464,7 @@ void llvm::calculateClrEHStateNumbers(const Function *Fn,
     if (BB.isLandingPad())
       report_fatal_error("CoreCLR EH cannot use landingpads");
     const Instruction *FirstNonPHI = BB.getFirstNonPHI();
-    if (!doesEHPadUnwindToCaller(FirstNonPHI))
+    if (!isTopLevelPadForMSVC(FirstNonPHI))
       continue;
     // queue this with sentinel parent state -1 to mean unwind to caller.
     Worklist.emplace_back(FirstNonPHI, -1);
@@ -472,16 +475,11 @@ void llvm::calculateClrEHStateNumbers(const Function *Fn,
     int ParentState;
     std::tie(Pad, ParentState) = Worklist.pop_back_val();
 
+    Value *ParentPad;
     int PredState;
-    if (const CleanupEndPadInst *EndPad = dyn_cast<CleanupEndPadInst>(Pad)) {
-      FuncInfo.EHPadStateMap[EndPad] = ParentState;
-      // Queue the cleanuppad, in case it doesn't have a cleanupret.
-      Worklist.emplace_back(EndPad->getCleanupPad(), ParentState);
-      // Preds of the endpad should get the parent state.
-      PredState = ParentState;
-    } else if (const CleanupPadInst *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
+    if (const CleanupPadInst *Cleanup = dyn_cast<CleanupPadInst>(Pad)) {
       // A cleanup can have multiple exits; don't re-process after the first.
-      if (FuncInfo.EHPadStateMap.count(Pad))
+      if (FuncInfo.EHPadStateMap.count(Cleanup))
         continue;
       // CoreCLR personality uses arity to distinguish faults from finallies.
       const BasicBlock *PadBlock = Cleanup->getParent();
@@ -492,222 +490,75 @@ void llvm::calculateClrEHStateNumbers(const Function *Fn,
           addClrEHHandler(FuncInfo, ParentState, HandlerType, 0, PadBlock);
       FuncInfo.EHPadStateMap[Cleanup] = NewState;
       // Propagate the new state to all preds of the cleanup
+      ParentPad = Cleanup->getParentPad();
       PredState = NewState;
-    } else if (const CatchEndPadInst *EndPad = dyn_cast<CatchEndPadInst>(Pad)) {
-      FuncInfo.EHPadStateMap[EndPad] = ParentState;
-      // Preds of the endpad should get the parent state.
-      PredState = ParentState;
-    } else if (const CatchPadInst *Catch = dyn_cast<CatchPadInst>(Pad)) {
-      const BasicBlock *PadBlock = Catch->getParent();
-      uint32_t TypeToken = static_cast<uint32_t>(
-          cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
-      int NewState = addClrEHHandler(FuncInfo, ParentState,
-                                     ClrHandlerType::Catch, TypeToken, PadBlock);
-      FuncInfo.EHPadStateMap[Catch] = NewState;
-      // Preds of the catch get its state
+    } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
+      SmallVector<const CatchPadInst *, 1> Handlers;
+      for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
+        const auto *Catch = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI());
+        Handlers.push_back(Catch);
+      }
+      FuncInfo.EHPadStateMap[CatchSwitch] = ParentState;
+      int NewState = ParentState;
+      for (auto HandlerI = Handlers.rbegin(), HandlerE = Handlers.rend();
+           HandlerI != HandlerE; ++HandlerI) {
+        const CatchPadInst *Catch = *HandlerI;
+        const BasicBlock *PadBlock = Catch->getParent();
+        uint32_t TypeToken = static_cast<uint32_t>(
+            cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue());
+        NewState = addClrEHHandler(FuncInfo, NewState, ClrHandlerType::Catch,
+                                   TypeToken, PadBlock);
+        FuncInfo.EHPadStateMap[Catch] = NewState;
+      }
+      for (const auto *CatchPad : Handlers) {
+        for (const User *U : CatchPad->users()) {
+          const auto *UserI = cast<Instruction>(U);
+          if (UserI->isEHPad())
+            Worklist.emplace_back(UserI, ParentState);
+        }
+      }
       PredState = NewState;
+      ParentPad = CatchSwitch->getParentPad();
     } else {
       llvm_unreachable("Unexpected EH pad");
     }
 
     // Queue all predecessors with the given state
     for (const BasicBlock *Pred : predecessors(Pad->getParent())) {
-      if ((Pred = getEHPadFromPredecessor(Pred)))
+      if ((Pred = getEHPadFromPredecessor(Pred, ParentPad)))
         Worklist.emplace_back(Pred->getFirstNonPHI(), PredState);
     }
   }
-}
 
-void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) {
-  if (Personality != EHPersonality::MSVC_CXX)
-    return;
-  for (BasicBlock &BB : F) {
-    Instruction *First = BB.getFirstNonPHI();
-    auto *TPI = dyn_cast<TerminatePadInst>(First);
-    if (!TPI)
-      continue;
-
-    if (TPI->getNumArgOperands() != 1)
-      report_fatal_error(
-          "Expected a unary terminatepad for MSVC C++ personalities!");
-
-    auto *TerminateFn = dyn_cast<Function>(TPI->getArgOperand(0));
-    if (!TerminateFn)
-      report_fatal_error("Function operand expected in terminatepad for MSVC "
-                         "C++ personalities!");
-
-    // Insert the cleanuppad instruction.
-    auto *CPI = CleanupPadInst::Create(
-        BB.getContext(), {}, Twine("terminatepad.for.", BB.getName()), &BB);
-
-    // Insert the call to the terminate instruction.
-    auto *CallTerminate = CallInst::Create(TerminateFn, {}, &BB);
-    CallTerminate->setDoesNotThrow();
-    CallTerminate->setDoesNotReturn();
-    CallTerminate->setCallingConv(TerminateFn->getCallingConv());
-
-    // Insert a new terminator for the cleanuppad using the same successor as
-    // the terminatepad.
-    CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB);
-
-    // Let's remove the terminatepad now that we've inserted the new
-    // instructions.
-    TPI->eraseFromParent();
-  }
+  calculateStateNumbersForInvokes(Fn, FuncInfo);
 }
 
-static void
-colorFunclets(Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks,
-              std::map<BasicBlock *, std::set<BasicBlock *>> &BlockColors,
-              std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletBlocks,
-              std::map<BasicBlock *, std::set<BasicBlock *>> &FuncletChildren) {
-  SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
-  BasicBlock *EntryBlock = &F.getEntryBlock();
-
-  // Build up the color map, which maps each block to its set of 'colors'.
-  // For any block B, the "colors" of B are the set of funclets F (possibly
-  // including a root "funclet" representing the main function), such that
-  // F will need to directly contain B or a copy of B (where the term "directly
-  // contain" is used to distinguish from being "transitively contained" in
-  // a nested funclet).
-  // Use a CFG walk driven by a worklist of (block, color) pairs.  The "color"
-  // sets attached during this processing to a block which is the entry of some
-  // funclet F is actually the set of F's parents -- i.e. the union of colors
-  // of all predecessors of F's entry.  For all other blocks, the color sets
-  // are as defined above.  A post-pass fixes up the block color map to reflect
-  // the same sense of "color" for funclet entries as for other blocks.
-
-  Worklist.push_back({EntryBlock, EntryBlock});
+void WinEHPrepare::colorFunclets(Function &F) {
+  BlockColors = colorEHFunclets(F);
 
-  while (!Worklist.empty()) {
-    BasicBlock *Visiting;
-    BasicBlock *Color;
-    std::tie(Visiting, Color) = Worklist.pop_back_val();
-    Instruction *VisitingHead = Visiting->getFirstNonPHI();
-    if (VisitingHead->isEHPad() && !isa<CatchEndPadInst>(VisitingHead) &&
-        !isa<CleanupEndPadInst>(VisitingHead)) {
-      // Mark this as a funclet head as a member of itself.
-      FuncletBlocks[Visiting].insert(Visiting);
-      // Queue exits (i.e. successors of rets/endpads) with the parent color.
-      // Skip any exits that are catchendpads, since the parent color must then
-      // represent one of the catches chained to that catchendpad, but the
-      // catchendpad should get the color of the common parent of all its
-      // chained catches (i.e. the grandparent color of the current pad).
-      // We don't need to worry abou catchendpads going unvisited, since the
-      // catches chained to them must have unwind edges to them by which we will
-      // visit them.
-      for (User *U : VisitingHead->users()) {
-        if (auto *Exit = dyn_cast<TerminatorInst>(U)) {
-          for (BasicBlock *Succ : successors(Exit->getParent()))
-            if (!isa<CatchEndPadInst>(*Succ->getFirstNonPHI()))
-              if (BlockColors[Succ].insert(Color).second)
-                Worklist.push_back({Succ, Color});
-        }
-      }
-      // Handle CatchPad specially since its successors need different colors.
-      if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(VisitingHead)) {
-        // Visit the normal successor with the color of the new EH pad, and
-        // visit the unwind successor with the color of the parent.
-        BasicBlock *NormalSucc = CatchPad->getNormalDest();
-        if (BlockColors[NormalSucc].insert(Visiting).second) {
-          Worklist.push_back({NormalSucc, Visiting});
-        }
-        BasicBlock *UnwindSucc = CatchPad->getUnwindDest();
-        if (BlockColors[UnwindSucc].insert(Color).second) {
-          Worklist.push_back({UnwindSucc, Color});
-        }
-        continue;
-      }
-      // Switch color to the current node, except for terminate pads which
-      // have no bodies and only unwind successors and so need their successors
-      // visited with the color of the parent.
-      if (!isa<TerminatePadInst>(VisitingHead))
-        Color = Visiting;
-    } else {
-      // Note that this is a member of the given color.
-      FuncletBlocks[Color].insert(Visiting);
-    }
-
-    TerminatorInst *Terminator = Visiting->getTerminator();
-    if (isa<CleanupReturnInst>(Terminator) ||
-        isa<CatchReturnInst>(Terminator) ||
-        isa<CleanupEndPadInst>(Terminator)) {
-      // These blocks' successors have already been queued with the parent
-      // color.
-      continue;
-    }
-    for (BasicBlock *Succ : successors(Visiting)) {
-      if (isa<CatchEndPadInst>(Succ->getFirstNonPHI())) {
-        // The catchendpad needs to be visited with the parent's color, not
-        // the current color.  This will happen in the code above that visits
-        // any catchpad unwind successor with the parent color, so we can
-        // safely skip this successor here.
-        continue;
-      }
-      if (BlockColors[Succ].insert(Color).second) {
-        Worklist.push_back({Succ, Color});
-      }
-    }
-  }
-
-  // The processing above actually accumulated the parent set for this
-  // funclet into the color set for its entry; use the parent set to
-  // populate the children map, and reset the color set to include just
-  // the funclet itself (no instruction can target a funclet entry except on
-  // that transitions to the child funclet).
-  for (BasicBlock *FuncletEntry : EntryBlocks) {
-    std::set<BasicBlock *> &ColorMapItem = BlockColors[FuncletEntry];
-    for (BasicBlock *Parent : ColorMapItem)
-      FuncletChildren[Parent].insert(FuncletEntry);
-    ColorMapItem.clear();
-    ColorMapItem.insert(FuncletEntry);
+  // Invert the map from BB to colors to color to BBs.
+  for (BasicBlock &BB : F) {
+    ColorVector &Colors = BlockColors[&BB];
+    for (BasicBlock *Color : Colors)
+      FuncletBlocks[Color].push_back(&BB);
   }
 }
 
-void WinEHPrepare::colorFunclets(Function &F,
-                                 SmallVectorImpl<BasicBlock *> &EntryBlocks) {
-  ::colorFunclets(F, EntryBlocks, BlockColors, FuncletBlocks, FuncletChildren);
-}
-
 void llvm::calculateCatchReturnSuccessorColors(const Function *Fn,
                                                WinEHFuncInfo &FuncInfo) {
-  SmallVector<BasicBlock *, 4> EntryBlocks;
-  // colorFunclets needs the set of EntryBlocks, get them using
-  // findFuncletEntryPoints.
-  findFuncletEntryPoints(const_cast<Function &>(*Fn), EntryBlocks);
-
-  std::map<BasicBlock *, std::set<BasicBlock *>> BlockColors;
-  std::map<BasicBlock *, std::set<BasicBlock *>> FuncletBlocks;
-  std::map<BasicBlock *, std::set<BasicBlock *>> FuncletChildren;
-  // Figure out which basic blocks belong to which funclets.
-  colorFunclets(const_cast<Function &>(*Fn), EntryBlocks, BlockColors,
-                FuncletBlocks, FuncletChildren);
-
-  // We need to find the catchret successors.  To do this, we must first find
-  // all the catchpad funclets.
-  for (auto &Funclet : FuncletBlocks) {
-    // Figure out what kind of funclet we are looking at; We only care about
-    // catchpads.
-    BasicBlock *FuncletPadBB = Funclet.first;
-    Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
-    auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
-    if (!CatchPad)
+  for (const BasicBlock &BB : *Fn) {
+    const auto *CatchRet = dyn_cast<CatchReturnInst>(BB.getTerminator());
+    if (!CatchRet)
       continue;
-
-    // The users of a catchpad are always catchrets.
-    for (User *Exit : CatchPad->users()) {
-      auto *CatchReturn = dyn_cast<CatchReturnInst>(Exit);
-      if (!CatchReturn)
-        continue;
-      BasicBlock *CatchRetSuccessor = CatchReturn->getSuccessor();
-      std::set<BasicBlock *> &SuccessorColors = BlockColors[CatchRetSuccessor];
-      assert(SuccessorColors.size() == 1 && "Expected BB to be monochrome!");
-      BasicBlock *Color = *SuccessorColors.begin();
-      if (auto *CPI = dyn_cast<CatchPadInst>(Color->getFirstNonPHI()))
-        Color = CPI->getNormalDest();
-      // Record the catchret successor's funclet membership.
-      FuncInfo.CatchRetSuccessorColorMap[CatchReturn] = Color;
-    }
+    // A 'catchret' returns to the outer scope's color.
+    Value *ParentPad = CatchRet->getParentPad();
+    const BasicBlock *Color;
+    if (isa<ConstantTokenNone>(ParentPad))
+      Color = &Fn->getEntryBlock();
+    else
+      Color = cast<Instruction>(ParentPad)->getParent();
+    // Record the catchret successor's funclet membership.
+    FuncInfo.CatchRetSuccessorColorMap[CatchRet] = Color;
   }
 }
 
@@ -740,47 +591,33 @@ void WinEHPrepare::demotePHIsOnFunclets(Function &F) {
   }
 }
 
-void WinEHPrepare::demoteUsesBetweenFunclets(Function &F) {
-  // Turn all inter-funclet uses of a Value into loads and stores.
-  for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) {
-    BasicBlock *BB = &*FI++;
-    std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
-    for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
-      Instruction *I = &*BI++;
-      // Funclets are permitted to use static allocas.
-      if (auto *AI = dyn_cast<AllocaInst>(I))
-        if (AI->isStaticAlloca())
-          continue;
-
-      demoteNonlocalUses(I, ColorsForBB, F);
-    }
-  }
-}
-
-void WinEHPrepare::demoteArgumentUses(Function &F) {
-  // Also demote function parameters used in funclets.
-  std::set<BasicBlock *> &ColorsForEntry = BlockColors[&F.getEntryBlock()];
-  for (Argument &Arg : F.args())
-    demoteNonlocalUses(&Arg, ColorsForEntry, F);
-}
-
-void WinEHPrepare::cloneCommonBlocks(
-    Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
+void WinEHPrepare::cloneCommonBlocks(Function &F) {
   // We need to clone all blocks which belong to multiple funclets.  Values are
   // remapped throughout the funclet to propogate both the new instructions
   // *and* the new basic blocks themselves.
-  for (BasicBlock *FuncletPadBB : EntryBlocks) {
-    std::set<BasicBlock *> &BlocksInFunclet = FuncletBlocks[FuncletPadBB];
+  for (auto &Funclets : FuncletBlocks) {
+    BasicBlock *FuncletPadBB = Funclets.first;
+    std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second;
+    Value *FuncletToken;
+    if (FuncletPadBB == &F.getEntryBlock())
+      FuncletToken = ConstantTokenNone::get(F.getContext());
+    else
+      FuncletToken = FuncletPadBB->getFirstNonPHI();
 
-    std::map<BasicBlock *, BasicBlock *> Orig2Clone;
+    std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone;
     ValueToValueMapTy VMap;
     for (BasicBlock *BB : BlocksInFunclet) {
-      std::set<BasicBlock *> &ColorsForBB = BlockColors[BB];
+      ColorVector &ColorsForBB = BlockColors[BB];
       // We don't need to do anything if the block is monochromatic.
       size_t NumColorsForBB = ColorsForBB.size();
       if (NumColorsForBB == 1)
         continue;
 
+      DEBUG_WITH_TYPE("winehprepare-coloring",
+                      dbgs() << "  Cloning block \'" << BB->getName()
+                              << "\' for funclet \'" << FuncletPadBB->getName()
+                              << "\'.\n");
+
       // Create a new basic block and copy instructions into it!
       BasicBlock *CBB =
           CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName()));
@@ -792,7 +629,7 @@ void WinEHPrepare::cloneCommonBlocks(
       VMap[BB] = CBB;
 
       // Record delta operations that we need to perform to our color mappings.
-      Orig2Clone[BB] = CBB;
+      Orig2Clone.emplace_back(BB, CBB);
     }
 
     // If nothing was cloned, we're done cloning in this funclet.
@@ -805,11 +642,28 @@ void WinEHPrepare::cloneCommonBlocks(
       BasicBlock *OldBlock = BBMapping.first;
       BasicBlock *NewBlock = BBMapping.second;
 
-      BlocksInFunclet.insert(NewBlock);
-      BlockColors[NewBlock].insert(FuncletPadBB);
-
-      BlocksInFunclet.erase(OldBlock);
-      BlockColors[OldBlock].erase(FuncletPadBB);
+      BlocksInFunclet.push_back(NewBlock);
+      ColorVector &NewColors = BlockColors[NewBlock];
+      assert(NewColors.empty() && "A new block should only have one color!");
+      NewColors.push_back(FuncletPadBB);
+
+      DEBUG_WITH_TYPE("winehprepare-coloring",
+                      dbgs() << "  Assigned color \'" << FuncletPadBB->getName()
+                              << "\' to block \'" << NewBlock->getName()
+                              << "\'.\n");
+
+      BlocksInFunclet.erase(
+          std::remove(BlocksInFunclet.begin(), BlocksInFunclet.end(), OldBlock),
+          BlocksInFunclet.end());
+      ColorVector &OldColors = BlockColors[OldBlock];
+      OldColors.erase(
+          std::remove(OldColors.begin(), OldColors.end(), FuncletPadBB),
+          OldColors.end());
+
+      DEBUG_WITH_TYPE("winehprepare-coloring",
+                      dbgs() << "  Removed color \'" << FuncletPadBB->getName()
+                              << "\' from block \'" << OldBlock->getName()
+                              << "\'.\n");
     }
 
     // Loop over all of the instructions in this funclet, fixing up operand
@@ -820,6 +674,69 @@ void WinEHPrepare::cloneCommonBlocks(
         RemapInstruction(&I, VMap,
                          RF_IgnoreMissingEntries | RF_NoModuleLevelChanges);
 
+    // Catchrets targeting cloned blocks need to be updated separately from
+    // the loop above because they are not in the current funclet.
+    SmallVector<CatchReturnInst *, 2> FixupCatchrets;
+    for (auto &BBMapping : Orig2Clone) {
+      BasicBlock *OldBlock = BBMapping.first;
+      BasicBlock *NewBlock = BBMapping.second;
+
+      FixupCatchrets.clear();
+      for (BasicBlock *Pred : predecessors(OldBlock))
+        if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator()))
+          if (CatchRet->getParentPad() == FuncletToken)
+            FixupCatchrets.push_back(CatchRet);
+
+      for (CatchReturnInst *CatchRet : FixupCatchrets)
+        CatchRet->setSuccessor(NewBlock);
+    }
+
+    auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) {
+      unsigned NumPreds = PN->getNumIncomingValues();
+      for (unsigned PredIdx = 0, PredEnd = NumPreds; PredIdx != PredEnd;
+           ++PredIdx) {
+        BasicBlock *IncomingBlock = PN->getIncomingBlock(PredIdx);
+        bool EdgeTargetsFunclet;
+        if (auto *CRI =
+                dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) {
+          EdgeTargetsFunclet = (CRI->getParentPad() == FuncletToken);
+        } else {
+          ColorVector &IncomingColors = BlockColors[IncomingBlock];
+          assert(!IncomingColors.empty() && "Block not colored!");
+          assert((IncomingColors.size() == 1 ||
+                  llvm::all_of(IncomingColors,
+                               [&](BasicBlock *Color) {
+                                 return Color != FuncletPadBB;
+                               })) &&
+                 "Cloning should leave this funclet's blocks monochromatic");
+          EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB);
+        }
+        if (IsForOldBlock != EdgeTargetsFunclet)
+          continue;
+        PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false);
+        // Revisit the next entry.
+        --PredIdx;
+        --PredEnd;
+      }
+    };
+
+    for (auto &BBMapping : Orig2Clone) {
+      BasicBlock *OldBlock = BBMapping.first;
+      BasicBlock *NewBlock = BBMapping.second;
+      for (Instruction &OldI : *OldBlock) {
+        auto *OldPN = dyn_cast<PHINode>(&OldI);
+        if (!OldPN)
+          break;
+        UpdatePHIOnClonedBlock(OldPN, /*IsForOldBlock=*/true);
+      }
+      for (Instruction &NewI : *NewBlock) {
+        auto *NewPN = dyn_cast<PHINode>(&NewI);
+        if (!NewPN)
+          break;
+        UpdatePHIOnClonedBlock(NewPN, /*IsForOldBlock=*/false);
+      }
+    }
+
     // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
     // the PHI nodes for NewBB now.
     for (auto &BBMapping : Orig2Clone) {
@@ -867,7 +784,7 @@ void WinEHPrepare::cloneCommonBlocks(
       for (Use &U : OldI->uses()) {
         Instruction *UserI = cast<Instruction>(U.getUser());
         BasicBlock *UserBB = UserI->getParent();
-        std::set<BasicBlock *> &ColorsForUserBB = BlockColors[UserBB];
+        ColorVector &ColorsForUserBB = BlockColors[UserBB];
         assert(!ColorsForUserBB.empty());
         if (ColorsForUserBB.size() > 1 ||
             *ColorsForUserBB.begin() != FuncletPadBB)
@@ -893,19 +810,56 @@ void WinEHPrepare::cloneCommonBlocks(
   }
 }
 
-void WinEHPrepare::removeImplausibleTerminators(Function &F) {
+void WinEHPrepare::removeImplausibleInstructions(Function &F) {
   // Remove implausible terminators and replace them with UnreachableInst.
   for (auto &Funclet : FuncletBlocks) {
     BasicBlock *FuncletPadBB = Funclet.first;
-    std::set<BasicBlock *> &BlocksInFunclet = Funclet.second;
+    std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second;
     Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI();
-    auto *CatchPad = dyn_cast<CatchPadInst>(FirstNonPHI);
-    auto *CleanupPad = dyn_cast<CleanupPadInst>(FirstNonPHI);
+    auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI);
+    auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad);
+    auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad);
 
     for (BasicBlock *BB : BlocksInFunclet) {
+      for (Instruction &I : *BB) {
+        CallSite CS(&I);
+        if (!CS)
+          continue;
+
+        Value *FuncletBundleOperand = nullptr;
+        if (auto BU = CS.getOperandBundle(LLVMContext::OB_funclet))
+          FuncletBundleOperand = BU->Inputs.front();
+
+        if (FuncletBundleOperand == FuncletPad)
+          continue;
+
+        // Skip call sites which are nounwind intrinsics.
+        auto *CalledFn =
+            dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts());
+        if (CalledFn && CalledFn->isIntrinsic() && CS.doesNotThrow())
+          continue;
+
+        // This call site was not part of this funclet, remove it.
+        if (CS.isInvoke()) {
+          // Remove the unwind edge if it was an invoke.
+          removeUnwindEdge(BB);
+          // Get a pointer to the new call.
+          BasicBlock::iterator CallI =
+              std::prev(BB->getTerminator()->getIterator());
+          auto *CI = cast<CallInst>(&*CallI);
+          changeToUnreachable(CI, /*UseLLVMTrap=*/false);
+        } else {
+          changeToUnreachable(&I, /*UseLLVMTrap=*/false);
+        }
+
+        // There are no more instructions in the block (except for unreachable),
+        // we are done.
+        break;
+      }
+
       TerminatorInst *TI = BB->getTerminator();
       // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
-      bool IsUnreachableRet = isa<ReturnInst>(TI) && (CatchPad || CleanupPad);
+      bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad;
       // The token consumed by a CatchReturnInst must match the funclet token.
       bool IsUnreachableCatchret = false;
       if (auto *CRI = dyn_cast<CatchReturnInst>(TI))
@@ -914,34 +868,17 @@ void WinEHPrepare::removeImplausibleTerminators(Function &F) {
       bool IsUnreachableCleanupret = false;
       if (auto *CRI = dyn_cast<CleanupReturnInst>(TI))
         IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
-      // The token consumed by a CleanupEndPadInst must match the funclet token.
-      bool IsUnreachableCleanupendpad = false;
-      if (auto *CEPI = dyn_cast<CleanupEndPadInst>(TI))
-        IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
       if (IsUnreachableRet || IsUnreachableCatchret ||
-          IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
-        // Loop through all of our successors and make sure they know that one
-        // of their predecessors is going away.
-        for (BasicBlock *SuccBB : TI->successors())
-          SuccBB->removePredecessor(BB);
-
-        if (IsUnreachableCleanupendpad) {
-          // We can't simply replace a cleanupendpad with unreachable, because
-          // its predecessor edges are EH edges and unreachable is not an EH
-          // pad.  Change all predecessors to the "unwind to caller" form.
-          for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
-               PI != PE;) {
-            BasicBlock *Pred = *PI++;
-            removeUnwindEdge(Pred);
-          }
+          IsUnreachableCleanupret) {
+        changeToUnreachable(TI, /*UseLLVMTrap=*/false);
+      } else if (isa<InvokeInst>(TI)) {
+        if (Personality == EHPersonality::MSVC_CXX && CleanupPad) {
+          // Invokes within a cleanuppad for the MSVC++ personality never
+          // transfer control to their unwind edge: the personality will
+          // terminate the program.
+          removeUnwindEdge(BB);
         }
-
-        new UnreachableInst(BB->getContext(), TI);
-        TI->eraseFromParent();
       }
-      // FIXME: Check for invokes/cleanuprets/cleanupendpads which unwind to
-      // implausible catchendpads (i.e. catchendpad not in immediate parent
-      // funclet).
     }
   }
 }
@@ -962,7 +899,6 @@ void WinEHPrepare::cleanupPreparedFunclets(Function &F) {
 }
 
 void WinEHPrepare::verifyPreparedFunclets(Function &F) {
-  // Recolor the CFG to verify that all is well.
   for (BasicBlock &BB : F) {
     size_t NumColors = BlockColors[&BB].size();
     assert(NumColors == 1 && "Expected monochromatic BB!");
@@ -973,40 +909,39 @@ void WinEHPrepare::verifyPreparedFunclets(Function &F) {
     if (!DisableDemotion) {
       bool EHPadHasPHI = BB.isEHPad() && isa<PHINode>(BB.begin());
       assert(!EHPadHasPHI && "EH Pad still has a PHI!");
-      if (EHPadHasPHI)
-        report_fatal_error("EH Pad still has a PHI!");
     }
   }
 }
 
-bool WinEHPrepare::prepareExplicitEH(
-    Function &F, SmallVectorImpl<BasicBlock *> &EntryBlocks) {
-  replaceTerminatePadWithCleanup(F);
+bool WinEHPrepare::prepareExplicitEH(Function &F) {
+  // Remove unreachable blocks.  It is not valuable to assign them a color and
+  // their existence can trick us into thinking values are alive when they are
+  // not.
+  removeUnreachableBlocks(F);
 
   // Determine which blocks are reachable from which funclet entries.
-  colorFunclets(F, EntryBlocks);
-
-  if (!DisableDemotion) {
-    demotePHIsOnFunclets(F);
-
-    demoteUsesBetweenFunclets(F);
+  colorFunclets(F);
 
-    demoteArgumentUses(F);
-  }
+  cloneCommonBlocks(F);
 
-  cloneCommonBlocks(F, EntryBlocks);
+  if (!DisableDemotion)
+    demotePHIsOnFunclets(F);
 
   if (!DisableCleanups) {
-    removeImplausibleTerminators(F);
+    DEBUG(verifyFunction(F));
+    removeImplausibleInstructions(F);
 
+    DEBUG(verifyFunction(F));
     cleanupPreparedFunclets(F);
   }
 
-  verifyPreparedFunclets(F);
+  DEBUG(verifyPreparedFunclets(F));
+  // Recolor the CFG to verify that all is well.
+  DEBUG(colorFunclets(F));
+  DEBUG(verifyPreparedFunclets(F));
 
   BlockColors.clear();
   FuncletBlocks.clear();
-  FuncletChildren.clear();
 
   return true;
 }
@@ -1016,9 +951,11 @@ bool WinEHPrepare::prepareExplicitEH(
 AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
   BasicBlock *PHIBlock = PN->getParent();
   AllocaInst *SpillSlot = nullptr;
+  Instruction *EHPad = PHIBlock->getFirstNonPHI();
 
-  if (isa<CleanupPadInst>(PHIBlock->getFirstNonPHI())) {
-    // Insert a load in place of the PHI and replace all uses.
+  if (!isa<TerminatorInst>(EHPad)) {
+    // If the EHPad isn't a terminator, then we can insert a load in this block
+    // that will dominate all uses.
     SpillSlot = new AllocaInst(PN->getType(), nullptr,
                                Twine(PN->getName(), ".wineh.spillslot"),
                                &F.getEntryBlock().front());
@@ -1028,16 +965,16 @@ AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) {
     return SpillSlot;
   }
 
+  // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
+  // loads of the slot before every use.
   DenseMap<BasicBlock *, Value *> Loads;
   for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
        UI != UE;) {
     Use &U = *UI++;
     auto *UsingInst = cast<Instruction>(U.getUser());
-    BasicBlock *UsingBB = UsingInst->getParent();
-    if (UsingBB->isEHPad()) {
+    if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) {
       // Use is on an EH pad phi.  Leave it alone; we'll insert loads and
       // stores for it separately.
-      assert(isa<PHINode>(UsingInst));
       continue;
     }
     replaceUseWithLoad(PN, U, SpillSlot, Loads, F);
@@ -1091,7 +1028,7 @@ void WinEHPrepare::insertPHIStore(
     SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) {
 
   if (PredBlock->isEHPad() &&
-      !isa<CleanupPadInst>(PredBlock->getFirstNonPHI())) {
+      isa<TerminatorInst>(PredBlock->getFirstNonPHI())) {
     // Pred is unsplittable, so we need to queue it on the worklist.
     Worklist.push_back({PredBlock, PredVal});
     return;
@@ -1101,69 +1038,6 @@ void WinEHPrepare::insertPHIStore(
   new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator());
 }
 
-// TODO: Share loads for same-funclet uses (requires dominators if funclets
-// aren't properly nested).
-void WinEHPrepare::demoteNonlocalUses(Value *V,
-                                      std::set<BasicBlock *> &ColorsForBB,
-                                      Function &F) {
-  // Tokens can only be used non-locally due to control flow involving
-  // unreachable edges.  Don't try to demote the token usage, we'll simply
-  // delete the cloned user later.
-  if (isa<CatchPadInst>(V) || isa<CleanupPadInst>(V))
-    return;
-
-  DenseMap<BasicBlock *, Value *> Loads;
-  AllocaInst *SpillSlot = nullptr;
-  for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;) {
-    Use &U = *UI++;
-    auto *UsingInst = cast<Instruction>(U.getUser());
-    BasicBlock *UsingBB = UsingInst->getParent();
-
-    // Is the Use inside a block which is colored the same as the Def?
-    // If so, we don't need to escape the Def because we will clone
-    // ourselves our own private copy.
-    std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[UsingBB];
-    if (ColorsForUsingBB == ColorsForBB)
-      continue;
-
-    replaceUseWithLoad(V, U, SpillSlot, Loads, F);
-  }
-  if (SpillSlot) {
-    // Insert stores of the computed value into the stack slot.
-    // We have to be careful if I is an invoke instruction,
-    // because we can't insert the store AFTER the terminator instruction.
-    BasicBlock::iterator InsertPt;
-    if (isa<Argument>(V)) {
-      InsertPt = F.getEntryBlock().getTerminator()->getIterator();
-    } else if (isa<TerminatorInst>(V)) {
-      auto *II = cast<InvokeInst>(V);
-      // We cannot demote invoke instructions to the stack if their normal
-      // edge is critical. Therefore, split the critical edge and create a
-      // basic block into which the store can be inserted.
-      if (!II->getNormalDest()->getSinglePredecessor()) {
-        unsigned SuccNum =
-            GetSuccessorNumber(II->getParent(), II->getNormalDest());
-        assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
-        BasicBlock *NewBlock = SplitCriticalEdge(II, SuccNum);
-        assert(NewBlock && "Unable to split critical edge.");
-        // Update the color mapping for the newly split edge.
-        std::set<BasicBlock *> &ColorsForUsingBB = BlockColors[II->getParent()];
-        BlockColors[NewBlock] = ColorsForUsingBB;
-        for (BasicBlock *FuncletPad : ColorsForUsingBB)
-          FuncletBlocks[FuncletPad].insert(NewBlock);
-      }
-      InsertPt = II->getNormalDest()->getFirstInsertionPt();
-    } else {
-      InsertPt = cast<Instruction>(V)->getIterator();
-      ++InsertPt;
-      // Don't insert before PHI nodes or EH pad instrs.
-      for (; isa<PHINode>(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
-        ;
-    }
-    new StoreInst(V, SpillSlot, &*InsertPt);
-  }
-}
-
 void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
                                       DenseMap<BasicBlock *, Value *> &Loads,
                                       Function &F) {
@@ -1214,10 +1088,10 @@ void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
       Goto->setSuccessor(0, PHIBlock);
       CatchRet->setSuccessor(NewBlock);
       // Update the color mapping for the newly split edge.
-      std::set<BasicBlock *> &ColorsForPHIBlock = BlockColors[PHIBlock];
+      ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock];
       BlockColors[NewBlock] = ColorsForPHIBlock;
       for (BasicBlock *FuncletPad : ColorsForPHIBlock)
-        FuncletBlocks[FuncletPad].insert(NewBlock);
+        FuncletBlocks[FuncletPad].push_back(NewBlock);
       // Treat the new block as incoming for load insertion.
       IncomingBlock = NewBlock;
     }
@@ -1236,11 +1110,12 @@ void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
   }
 }
 
-void WinEHFuncInfo::addIPToStateRange(const BasicBlock *PadBB,
+void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II,
                                       MCSymbol *InvokeBegin,
                                       MCSymbol *InvokeEnd) {
-  assert(PadBB->isEHPad() && EHPadStateMap.count(PadBB->getFirstNonPHI()) &&
-         "should get EH pad BB with precomputed state");
-  InvokeToStateMap[InvokeBegin] =
-      std::make_pair(EHPadStateMap[PadBB->getFirstNonPHI()], InvokeEnd);
+  assert(InvokeStateMap.count(II) &&
+         "should get invoke with precomputed state");
+  LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd);
 }
+
+WinEHFuncInfo::WinEHFuncInfo() {}