@@ -87,9 +87,8 @@ class WebAssemblyCFGStackify final : public MachineFunctionPass {
8787 const MachineBasicBlock *MBB);
8888 unsigned getDelegateDepth (const SmallVectorImpl<EndMarkerInfo> &Stack,
8989 const MachineBasicBlock *MBB);
90- unsigned
91- getRethrowDepth (const SmallVectorImpl<EndMarkerInfo> &Stack,
92- const SmallVectorImpl<const MachineBasicBlock *> &EHPadStack);
90+ unsigned getRethrowDepth (const SmallVectorImpl<EndMarkerInfo> &Stack,
91+ const MachineBasicBlock *EHPadToRethrow);
9392 void rewriteDepthImmediates (MachineFunction &MF);
9493 void fixEndsAtEndOfFunction (MachineFunction &MF);
9594 void cleanupFunctionData (MachineFunction &MF);
@@ -1612,34 +1611,13 @@ unsigned WebAssemblyCFGStackify::getDelegateDepth(
16121611
16131612unsigned WebAssemblyCFGStackify::getRethrowDepth (
16141613 const SmallVectorImpl<EndMarkerInfo> &Stack,
1615- const SmallVectorImpl< const MachineBasicBlock *> &EHPadStack ) {
1614+ const MachineBasicBlock *EHPadToRethrow ) {
16161615 unsigned Depth = 0 ;
1617- // In our current implementation, rethrows always rethrow the exception caught
1618- // by the innermost enclosing catch. This means while traversing Stack in the
1619- // reverse direction, when we encounter END_TRY, we should check if the
1620- // END_TRY corresponds to the current innermost EH pad. For example:
1621- // try
1622- // ...
1623- // catch ;; (a)
1624- // try
1625- // rethrow 1 ;; (b)
1626- // catch ;; (c)
1627- // rethrow 0 ;; (d)
1628- // end ;; (e)
1629- // end ;; (f)
1630- //
1631- // When we are at 'rethrow' (d), while reversely traversing Stack the first
1632- // 'end' we encounter is the 'end' (e), which corresponds to the 'catch' (c).
1633- // And 'rethrow' (d) rethrows the exception caught by 'catch' (c), so we stop
1634- // there and the depth should be 0. But when we are at 'rethrow' (b), it
1635- // rethrows the exception caught by 'catch' (a), so when traversing Stack
1636- // reversely, we should skip the 'end' (e) and choose 'end' (f), which
1637- // corresponds to 'catch' (a).
16381616 for (auto X : reverse (Stack)) {
16391617 const MachineInstr *End = X.second ;
16401618 if (End->getOpcode () == WebAssembly::END_TRY) {
16411619 auto *EHPad = TryToEHPad[EndToBegin[End]];
1642- if (EHPadStack. back () == EHPad)
1620+ if (EHPadToRethrow == EHPad)
16431621 break ;
16441622 }
16451623 ++Depth;
@@ -1651,7 +1629,6 @@ unsigned WebAssemblyCFGStackify::getRethrowDepth(
16511629void WebAssemblyCFGStackify::rewriteDepthImmediates (MachineFunction &MF) {
16521630 // Now rewrite references to basic blocks to be depth immediates.
16531631 SmallVector<EndMarkerInfo, 8 > Stack;
1654- SmallVector<const MachineBasicBlock *, 8 > EHPadStack;
16551632 for (auto &MBB : reverse (MF)) {
16561633 for (MachineInstr &MI : llvm::reverse (MBB)) {
16571634 switch (MI.getOpcode ()) {
@@ -1669,31 +1646,14 @@ void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction &MF) {
16691646 break ;
16701647
16711648 case WebAssembly::END_BLOCK:
1649+ case WebAssembly::END_TRY:
16721650 Stack.push_back (std::make_pair (&MBB, &MI));
16731651 break ;
16741652
1675- case WebAssembly::END_TRY: {
1676- // We handle DELEGATE in the default level, because DELEGATE has
1677- // immediate operands to rewrite.
1678- Stack.push_back (std::make_pair (&MBB, &MI));
1679- auto *EHPad = TryToEHPad[EndToBegin[&MI]];
1680- EHPadStack.push_back (EHPad);
1681- break ;
1682- }
1683-
16841653 case WebAssembly::END_LOOP:
16851654 Stack.push_back (std::make_pair (EndToBegin[&MI]->getParent (), &MI));
16861655 break ;
16871656
1688- case WebAssembly::CATCH:
1689- case WebAssembly::CATCH_ALL:
1690- EHPadStack.pop_back ();
1691- break ;
1692-
1693- case WebAssembly::RETHROW:
1694- MI.getOperand (0 ).setImm (getRethrowDepth (Stack, EHPadStack));
1695- break ;
1696-
16971657 default :
16981658 if (MI.isTerminator ()) {
16991659 // Rewrite MBB operands to be depth immediates.
@@ -1705,6 +1665,9 @@ void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction &MF) {
17051665 if (MI.getOpcode () == WebAssembly::DELEGATE)
17061666 MO = MachineOperand::CreateImm (
17071667 getDelegateDepth (Stack, MO.getMBB ()));
1668+ else if (MI.getOpcode () == WebAssembly::RETHROW)
1669+ MO = MachineOperand::CreateImm (
1670+ getRethrowDepth (Stack, MO.getMBB ()));
17081671 else
17091672 MO = MachineOperand::CreateImm (
17101673 getBranchDepth (Stack, MO.getMBB ()));
0 commit comments