@@ -193,10 +193,7 @@ void WebAssemblyCFGStackify::unregisterScope(MachineInstr *Begin) {
193
193
194
194
// / Insert a BLOCK marker for branches to MBB (if needed).
195
195
void WebAssemblyCFGStackify::placeBlockMarker (MachineBasicBlock &MBB) {
196
- // This should have been handled in placeTryMarker.
197
- if (MBB.isEHPad ())
198
- return ;
199
-
196
+ assert (!MBB.isEHPad ());
200
197
MachineFunction &MF = *MBB.getParent ();
201
198
auto &MDT = getAnalysis<MachineDominatorTree>();
202
199
const auto &TII = *MF.getSubtarget <WebAssemblySubtarget>().getInstrInfo ();
@@ -253,24 +250,23 @@ void WebAssemblyCFGStackify::placeBlockMarker(MachineBasicBlock &MBB) {
253
250
// Instructions that should go after the BLOCK.
254
251
SmallPtrSet<const MachineInstr *, 4 > AfterSet;
255
252
for (const auto &MI : *Header) {
256
- // If there is a previously placed LOOP/TRY marker and the bottom block of
257
- // the loop/exception is above MBB, it should be after the BLOCK, because
258
- // the loop/exception is nested in this block. Otherwise it should be before
259
- // the BLOCK.
260
- if (MI.getOpcode () == WebAssembly::LOOP ||
261
- MI.getOpcode () == WebAssembly::TRY) {
262
- auto *BottomBB = BeginToEnd[&MI]->getParent ()->getPrevNode ();
263
- if (MBB.getNumber () > BottomBB->getNumber ())
253
+ // If there is a previously placed LOOP marker and the bottom block of the
254
+ // loop is above MBB, it should be after the BLOCK, because the loop is
255
+ // nested in this BLOCK. Otherwise it should be before the BLOCK.
256
+ if (MI.getOpcode () == WebAssembly::LOOP) {
257
+ auto *LoopBottom = BeginToEnd[&MI]->getParent ()->getPrevNode ();
258
+ if (MBB.getNumber () > LoopBottom->getNumber ())
264
259
AfterSet.insert (&MI);
265
260
#ifndef NDEBUG
266
261
else
267
262
BeforeSet.insert (&MI);
268
263
#endif
269
264
}
270
265
271
- // All previously inserted BLOCK markers should be after the BLOCK because
272
- // they are all nested blocks.
273
- if (MI.getOpcode () == WebAssembly::BLOCK)
266
+ // All previously inserted BLOCK/TRY markers should be after the BLOCK
267
+ // because they are all nested blocks.
268
+ if (MI.getOpcode () == WebAssembly::BLOCK ||
269
+ MI.getOpcode () == WebAssembly::TRY)
274
270
AfterSet.insert (&MI);
275
271
276
272
#ifndef NDEBUG
@@ -428,9 +424,7 @@ void WebAssemblyCFGStackify::placeLoopMarker(MachineBasicBlock &MBB) {
428
424
}
429
425
430
426
void WebAssemblyCFGStackify::placeTryMarker (MachineBasicBlock &MBB) {
431
- if (!MBB.isEHPad ())
432
- return ;
433
-
427
+ assert (MBB.isEHPad ());
434
428
MachineFunction &MF = *MBB.getParent ();
435
429
auto &MDT = getAnalysis<MachineDominatorTree>();
436
430
const auto &TII = *MF.getSubtarget <WebAssemblySubtarget>().getInstrInfo ();
@@ -486,31 +480,34 @@ void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) {
486
480
487
481
// Decide where in Header to put the TRY.
488
482
489
- // Instructions that should go before the BLOCK .
483
+ // Instructions that should go before the TRY .
490
484
SmallPtrSet<const MachineInstr *, 4 > BeforeSet;
491
- // Instructions that should go after the BLOCK .
485
+ // Instructions that should go after the TRY .
492
486
SmallPtrSet<const MachineInstr *, 4 > AfterSet;
493
487
for (const auto &MI : *Header) {
494
- // If there is a previously placed LOOP marker and the bottom block of
495
- // the loop is above MBB, the LOOP should be after the TRY, because the
496
- // loop is nested in this try . Otherwise it should be before the TRY.
488
+ // If there is a previously placed LOOP marker and the bottom block of the
489
+ // loop is above MBB, it should be after the TRY, because the loop is nested
490
+ // in this TRY . Otherwise it should be before the TRY.
497
491
if (MI.getOpcode () == WebAssembly::LOOP) {
498
- if (MBB.getNumber () > Bottom->getNumber ())
492
+ auto *LoopBottom = BeginToEnd[&MI]->getParent ()->getPrevNode ();
493
+ if (MBB.getNumber () > LoopBottom->getNumber ())
499
494
AfterSet.insert (&MI);
500
495
#ifndef NDEBUG
501
496
else
502
497
BeforeSet.insert (&MI);
503
498
#endif
504
499
}
505
500
506
- // All previously inserted TRY markers should be after the TRY because they
507
- // are all nested trys.
508
- if (MI.getOpcode () == WebAssembly::TRY)
501
+ // All previously inserted BLOCK/TRY markers should be after the TRY because
502
+ // they are all nested trys.
503
+ if (MI.getOpcode () == WebAssembly::BLOCK ||
504
+ MI.getOpcode () == WebAssembly::TRY)
509
505
AfterSet.insert (&MI);
510
506
511
507
#ifndef NDEBUG
512
- // All END_(LOOP/TRY) markers should be before the TRY.
513
- if (MI.getOpcode () == WebAssembly::END_LOOP ||
508
+ // All END_(BLOCK/LOOP/TRY) markers should be before the TRY.
509
+ if (MI.getOpcode () == WebAssembly::END_BLOCK ||
510
+ MI.getOpcode () == WebAssembly::END_LOOP ||
514
511
MI.getOpcode () == WebAssembly::END_TRY)
515
512
BeforeSet.insert (&MI);
516
513
#endif
@@ -566,8 +563,9 @@ void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) {
566
563
AfterSet.clear ();
567
564
for (const auto &MI : *Cont) {
568
565
#ifndef NDEBUG
569
- // END_TRY should precede existing LOOP markers.
570
- if (MI.getOpcode () == WebAssembly::LOOP)
566
+ // END_TRY should precede existing LOOP and BLOCK markers.
567
+ if (MI.getOpcode () == WebAssembly::LOOP ||
568
+ MI.getOpcode () == WebAssembly::BLOCK)
571
569
AfterSet.insert (&MI);
572
570
573
571
// All END_TRY markers placed earlier belong to exceptions that contains
@@ -588,6 +586,8 @@ void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) {
588
586
AfterSet.insert (&MI);
589
587
#endif
590
588
}
589
+
590
+ // It is not possible for an END_BLOCK to be already in this block.
591
591
}
592
592
593
593
// Mark the end of the TRY.
@@ -774,15 +774,19 @@ void WebAssemblyCFGStackify::placeMarkers(MachineFunction &MF) {
774
774
// Place the LOOP for MBB if MBB is the header of a loop.
775
775
for (auto &MBB : MF)
776
776
placeLoopMarker (MBB);
777
- // Place the TRY for MBB if MBB is the EH pad of an exception.
777
+
778
778
const MCAsmInfo *MCAI = MF.getTarget ().getMCAsmInfo ();
779
- if (MCAI->getExceptionHandlingType () == ExceptionHandling::Wasm &&
780
- MF.getFunction ().hasPersonalityFn ())
781
- for (auto &MBB : MF)
782
- placeTryMarker (MBB);
783
- // Place the BLOCK for MBB if MBB is branched to from above.
784
- for (auto &MBB : MF)
785
- placeBlockMarker (MBB);
779
+ for (auto &MBB : MF) {
780
+ if (MBB.isEHPad ()) {
781
+ // Place the TRY for MBB if MBB is the EH pad of an exception.
782
+ if (MCAI->getExceptionHandlingType () == ExceptionHandling::Wasm &&
783
+ MF.getFunction ().hasPersonalityFn ())
784
+ placeTryMarker (MBB);
785
+ } else {
786
+ // Place the BLOCK for MBB if MBB is branched to from above.
787
+ placeBlockMarker (MBB);
788
+ }
789
+ }
786
790
}
787
791
788
792
void WebAssemblyCFGStackify::rewriteDepthImmediates (MachineFunction &MF) {
0 commit comments