@@ -517,11 +517,28 @@ int WinEHStatePass::getBaseStateForBB(
517517 return BaseState;
518518}
519519
520+ static bool isIntrinsic (const CallBase &Call, Intrinsic::ID ID) {
521+ const Function *CF = Call.getCalledFunction ();
522+ return CF && CF->isIntrinsic () && CF->getIntrinsicID () == ID;
523+ }
524+
525+ static bool isSehScopeEnd (const CallBase &Call) {
526+ return isIntrinsic (Call, Intrinsic::seh_scope_end);
527+ }
528+
529+ static bool isSehScopeBegin (const CallBase &Call) {
530+ return isIntrinsic (Call, Intrinsic::seh_scope_begin);
531+ }
532+
520533// Calculate the state a call-site is in.
521534int WinEHStatePass::getStateForCall (
522535 DenseMap<BasicBlock *, ColorVector> &BlockColors, WinEHFuncInfo &FuncInfo,
523536 CallBase &Call) {
524537 if (auto *II = dyn_cast<InvokeInst>(&Call)) {
538+ if (isSehScopeEnd (*II)) {
539+ return getBaseStateForBB (BlockColors, FuncInfo, II->getNormalDest ());
540+ }
541+
525542 // Look up the state number of the EH pad this unwinds to.
526543 assert (FuncInfo.InvokeStateMap .count (II) && " invoke has no state!" );
527544 return FuncInfo.InvokeStateMap [II];
@@ -608,22 +625,9 @@ static int getSuccState(DenseMap<BasicBlock *, int> &InitialStates, Function &F,
608625 return CommonState;
609626}
610627
611- static bool isIntrinsic (const CallBase &Call, Intrinsic::ID ID) {
612- const Function *CF = Call.getCalledFunction ();
613- return CF && CF->isIntrinsic () && CF->getIntrinsicID () == ID;
614- }
615-
616- static bool isSehScopeEnd (const CallBase &Call) {
617- return isIntrinsic (Call, Intrinsic::seh_scope_end);
618- }
619-
620- static bool isSehScopeBegin (const CallBase &Call) {
621- return isIntrinsic (Call, Intrinsic::seh_scope_begin);
622- }
623-
624628bool WinEHStatePass::isStateStoreNeeded (EHPersonality Personality,
625629 CallBase &Call) {
626- if (isSehScopeBegin (Call)) {
630+ if (isSehScopeBegin (Call) || isSehScopeEnd (Call) ) {
627631 return true ;
628632 }
629633
@@ -663,8 +667,6 @@ void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
663667 DenseMap<BasicBlock *, int > InitialStates;
664668 // FinalStates yields the state of the last call-site for a BasicBlock.
665669 DenseMap<BasicBlock *, int > FinalStates;
666- // SEH scope end target blocks
667- SmallPtrSet<BasicBlock *, 4 > ScopeEndBlocks;
668670 // Worklist used to revisit BasicBlocks with indeterminate
669671 // Initial/Final-States.
670672 std::deque<BasicBlock *> Worklist;
@@ -676,15 +678,7 @@ void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
676678 InitialState = FinalState = ParentBaseState;
677679 for (Instruction &I : *BB) {
678680 auto *Call = dyn_cast<CallBase>(&I);
679- if (!Call)
680- continue ;
681-
682- if (isSehScopeEnd (*Call)) {
683- auto *Invoke = cast<InvokeInst>(Call);
684- ScopeEndBlocks.insert (Invoke->getNormalDest ());
685- }
686-
687- if (!isStateStoreNeeded (Personality, *Call))
681+ if (!Call || !isStateStoreNeeded (Personality, *Call))
688682 continue ;
689683
690684 int State = getStateForCall (BlockColors, FuncInfo, *Call);
@@ -737,12 +731,6 @@ void WinEHStatePass::addStateStores(Function &F, WinEHFuncInfo &FuncInfo) {
737731 FinalStates.insert ({BB, SuccState});
738732 }
739733
740- // Insert state restores after SEH scope ends
741- for (BasicBlock *BB : ScopeEndBlocks) {
742- int state = getBaseStateForBB (BlockColors, FuncInfo, BB);
743- insertStateNumberStore (BB->getFirstNonPHI (), state);
744- }
745-
746734 // Finally, insert state stores before call-sites which transition us to a new
747735 // state.
748736 for (BasicBlock *BB : RPOT) {
0 commit comments