Re-land r235154-r235156 under the existing -sehprepare flag

Keep the old SEH fan-in lowering on by default for now, since projects
rely on it.  This will make it easy to test this change with a simple
flag flip.

llvm-svn: 235399
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index 1e116dd..235cda6 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -911,7 +911,7 @@
 
 /// PrepareEHLandingPad - Emit an EH_LABEL, set up live-in registers, and
 /// do other setup for EH landing-pad blocks.
-void SelectionDAGISel::PrepareEHLandingPad() {
+bool SelectionDAGISel::PrepareEHLandingPad() {
   MachineBasicBlock *MBB = FuncInfo->MBB;
 
   const TargetRegisterClass *PtrRC = TLI->getRegClassFor(TLI->getPointerTy());
@@ -937,12 +937,12 @@
 
   if (isMSVCEHPersonality(Personality)) {
     SmallVector<MachineBasicBlock *, 4> ClauseBBs;
-    const IntrinsicInst *Actions =
+    const IntrinsicInst *ActionsCall =
         dyn_cast<IntrinsicInst>(LLVMBB->getFirstInsertionPt());
     // Get all invoke BBs that unwind to this landingpad.
     SmallVector<MachineBasicBlock *, 4> InvokeBBs(MBB->pred_begin(),
                                                   MBB->pred_end());
-    if (Actions && Actions->getIntrinsicID() == Intrinsic::eh_actions) {
+    if (ActionsCall && ActionsCall->getIntrinsicID() == Intrinsic::eh_actions) {
       // If this is a call to llvm.eh.actions followed by indirectbr, then we've
       // run WinEHPrepare, and we should remove this block from the machine CFG.
       // Mark the targets of the indirectbr as landingpads instead.
@@ -951,11 +951,15 @@
         // Add the edge from the invoke to the clause.
         for (MachineBasicBlock *InvokeBB : InvokeBBs)
           InvokeBB->addSuccessor(ClauseBB);
+
+        // Mark the clause as a landing pad or MI passes will delete it.
+        ClauseBB->setIsLandingPad();
       }
     } else {
       // Otherwise, we haven't done the preparation, and we need to invent some
       // clause basic blocks that branch into the landingpad.
       // FIXME: Remove this code once SEH preparation works.
+      ActionsCall = nullptr;
 
       // Make virtual registers and a series of labels that fill in values for
       // the clauses.
@@ -1017,7 +1021,10 @@
       WinEHFuncInfo &FI = MF->getMMI().getWinEHFuncInfo(MF->getFunction());
       MF->getMMI().addWinEHState(MBB, FI.LandingPadStateMap[LPadInst]);
     }
-    return;
+
+    // Select instructions for the landingpad if there was no llvm.eh.actions
+    // call.
+    return ActionsCall == nullptr;
   }
 
   // Mark exception register as live in.
@@ -1027,6 +1034,8 @@
   // Mark exception selector register as live in.
   if (unsigned Reg = TLI->getExceptionSelectorRegister())
     FuncInfo->ExceptionSelectorVirtReg = MBB->addLiveIn(Reg, PtrRC);
+
+  return true;
 }
 
 /// isFoldedOrDeadInstruction - Return true if the specified instruction is
@@ -1197,7 +1206,8 @@
     FuncInfo->ExceptionPointerVirtReg = 0;
     FuncInfo->ExceptionSelectorVirtReg = 0;
     if (LLVMBB->isLandingPad())
-      PrepareEHLandingPad();
+      if (!PrepareEHLandingPad())
+        continue;
 
     // Before doing SelectionDAG ISel, see if FastISel has been requested.
     if (FastIS) {