Index: lib/CodeGen/WinEHPrepare.cpp =================================================================== --- lib/CodeGen/WinEHPrepare.cpp +++ lib/CodeGen/WinEHPrepare.cpp @@ -1280,7 +1280,6 @@ // Remap any bad sibling-to-sibling transitions for funclets that // we just cloned. for (BasicBlock *ChildFunclet : FuncletChildren[CurFunclet]) { - bool NeedOrigInvokeRemapping = false; for (auto *BB : FuncletBlocks[ChildFunclet]) { TerminatorInst *Terminator = BB->getTerminator(); if (!Terminator->isExceptional()) @@ -1290,34 +1289,7 @@ // Note that catchendpads are handled when the associated catchpad // is cloned. They don't fit the pattern we're looking for here. BasicBlock *UnwindDest = nullptr; - if (auto *II = dyn_cast(Terminator)) { - UnwindDest = II->getUnwindDest(); - assert(UnwindDest && "Invoke unwinds to a null destination."); - assert(UnwindDest->isEHPad() && "Invoke does not unwind to an EH pad."); - auto *EHPadInst = UnwindDest->getFirstNonPHI(); - if (isa(EHPadInst)) { - // If the invoke unwind destination is the unwind destination for - // the current child catch pad funclet, there is nothing to be done. - auto *CurCatch = cast(ChildFunclet->getFirstNonPHI()); - if (CurCatch->getUnwindDest() == UnwindDest) - continue; - - // Otherwise, the invoke unwinds to a catch end pad that is the unwind - // destination another catch pad in the unwind chain from either the - // current catch pad or one of its clones. If it is already the - // catch end pad at the end unwind chain from the current catch pad, - // we'll need to check the invoke instructions in the original funclet - // later. Otherwise, we need to remap this invoke now. - BasicBlock *CurCatchEnd = getEndPadForCatch(CurCatch); - if (CurCatchEnd == UnwindDest) - NeedOrigInvokeRemapping = true; - else - II->setUnwindDest(CurCatchEnd); - continue; - } - // All other unwind scenarios for the invoke are handled elsewhere. - continue; - } else if (auto *I = dyn_cast(Terminator)) { + if (auto *I = dyn_cast(Terminator)) { UnwindDest = I->getUnwindDest(); // The catchendpad is not a sibling destination. This case should // have been handled in cloneFuncletForParent(). @@ -1380,6 +1352,43 @@ else if (auto *I = dyn_cast(Terminator)) I->setUnwindDest(TargetSibling); } + } + + // Invoke remapping can't be done correctly until after all of their + // other sibling-to-sibling unwinds have been remapped. + for (BasicBlock *ChildFunclet : FuncletChildren[CurFunclet]) { + bool NeedOrigInvokeRemapping = false; + for (auto *BB : FuncletBlocks[ChildFunclet]) { + TerminatorInst *Terminator = BB->getTerminator(); + auto *II = dyn_cast(Terminator); + if (!II) + continue; + + BasicBlock *UnwindDest = II->getUnwindDest(); + assert(UnwindDest && "Invoke unwinds to a null destination."); + assert(UnwindDest->isEHPad() && "Invoke does not unwind to an EH pad."); + auto *EHPadInst = UnwindDest->getFirstNonPHI(); + if (isa(EHPadInst)) { + // If the invoke unwind destination is the unwind destination for + // the current child catch pad funclet, there is nothing to be done. + auto *CurCatch = cast(ChildFunclet->getFirstNonPHI()); + if (CurCatch->getUnwindDest() == UnwindDest) + continue; + + // Otherwise, the invoke unwinds to a catch end pad that is the unwind + // destination another catch pad in the unwind chain from either the + // current catch pad or one of its clones. If it is already the + // catch end pad at the end unwind chain from the current catch pad, + // we'll need to check the invoke instructions in the original funclet + // later. Otherwise, we need to remap this invoke now. + BasicBlock *CurCatchEnd = getEndPadForCatch(CurCatch); + if (CurCatchEnd == UnwindDest) + NeedOrigInvokeRemapping = true; + else + II->setUnwindDest(CurCatchEnd); + } + // All other unwind scenarios for the invoke are handled elsewhere. + } if (NeedOrigInvokeRemapping) { // To properly remap invoke instructions that unwind to catch end pads // that are not the unwind destination of the catch pad funclet in which