1 //===-- WebAssemblyCFGStackify.cpp - CFG Stackification -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file implements a CFG stacking pass.
12 /// This pass inserts BLOCK, LOOP, and TRY markers to mark the start of scopes,
13 /// since scope boundaries serve as the labels for WebAssembly's control
16 /// This is sufficient to convert arbitrary CFGs into a form that works on
17 /// WebAssembly, provided that all loops are single-entry.
19 /// In case we use exceptions, this pass also fixes mismatches in unwind
20 /// destinations created during transforming CFG into wasm structured format.
22 //===----------------------------------------------------------------------===//
24 #include "WebAssembly.h"
25 #include "WebAssemblyExceptionInfo.h"
26 #include "WebAssemblyMachineFunctionInfo.h"
27 #include "WebAssemblySubtarget.h"
28 #include "WebAssemblyUtilities.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/CodeGen/MachineDominators.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineLoopInfo.h"
33 #include "llvm/MC/MCAsmInfo.h"
36 #define DEBUG_TYPE "wasm-cfg-stackify"
38 STATISTIC(NumUnwindMismatches
, "Number of EH pad unwind mismatches found");
41 class WebAssemblyCFGStackify final
: public MachineFunctionPass
{
42 StringRef
getPassName() const override
{ return "WebAssembly CFG Stackify"; }
44 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
45 AU
.addRequired
<MachineDominatorTree
>();
46 AU
.addRequired
<MachineLoopInfo
>();
47 AU
.addRequired
<WebAssemblyExceptionInfo
>();
48 MachineFunctionPass::getAnalysisUsage(AU
);
51 bool runOnMachineFunction(MachineFunction
&MF
) override
;
53 // For each block whose label represents the end of a scope, record the block
54 // which holds the beginning of the scope. This will allow us to quickly skip
55 // over scoped regions when walking blocks.
56 SmallVector
<MachineBasicBlock
*, 8> ScopeTops
;
59 void placeMarkers(MachineFunction
&MF
);
60 void placeBlockMarker(MachineBasicBlock
&MBB
);
61 void placeLoopMarker(MachineBasicBlock
&MBB
);
62 void placeTryMarker(MachineBasicBlock
&MBB
);
63 void removeUnnecessaryInstrs(MachineFunction
&MF
);
64 bool fixUnwindMismatches(MachineFunction
&MF
);
65 void rewriteDepthImmediates(MachineFunction
&MF
);
66 void fixEndsAtEndOfFunction(MachineFunction
&MF
);
68 // For each BLOCK|LOOP|TRY, the corresponding END_(BLOCK|LOOP|TRY).
69 DenseMap
<const MachineInstr
*, MachineInstr
*> BeginToEnd
;
70 // For each END_(BLOCK|LOOP|TRY), the corresponding BLOCK|LOOP|TRY.
71 DenseMap
<const MachineInstr
*, MachineInstr
*> EndToBegin
;
72 // <TRY marker, EH pad> map
73 DenseMap
<const MachineInstr
*, MachineBasicBlock
*> TryToEHPad
;
74 // <EH pad, TRY marker> map
75 DenseMap
<const MachineBasicBlock
*, MachineInstr
*> EHPadToTry
;
77 // There can be an appendix block at the end of each function, shared for:
78 // - creating a correct signature for fallthrough returns
79 // - target for rethrows that need to unwind to the caller, but are trapped
80 // inside another try/catch
81 MachineBasicBlock
*AppendixBB
= nullptr;
82 MachineBasicBlock
*getAppendixBlock(MachineFunction
&MF
) {
84 AppendixBB
= MF
.CreateMachineBasicBlock();
85 // Give it a fake predecessor so that AsmPrinter prints its label.
86 AppendixBB
->addSuccessor(AppendixBB
);
87 MF
.push_back(AppendixBB
);
92 // Helper functions to register / unregister scope information created by
93 // marker instructions.
94 void registerScope(MachineInstr
*Begin
, MachineInstr
*End
);
95 void registerTryScope(MachineInstr
*Begin
, MachineInstr
*End
,
96 MachineBasicBlock
*EHPad
);
97 void unregisterScope(MachineInstr
*Begin
);
100 static char ID
; // Pass identification, replacement for typeid
101 WebAssemblyCFGStackify() : MachineFunctionPass(ID
) {}
102 ~WebAssemblyCFGStackify() override
{ releaseMemory(); }
103 void releaseMemory() override
;
105 } // end anonymous namespace
107 char WebAssemblyCFGStackify::ID
= 0;
108 INITIALIZE_PASS(WebAssemblyCFGStackify
, DEBUG_TYPE
,
109 "Insert BLOCK/LOOP/TRY markers for WebAssembly scopes", false,
112 FunctionPass
*llvm::createWebAssemblyCFGStackify() {
113 return new WebAssemblyCFGStackify();
116 /// Test whether Pred has any terminators explicitly branching to MBB, as
117 /// opposed to falling through. Note that it's possible (eg. in unoptimized
118 /// code) for a branch instruction to both branch to a block and fallthrough
119 /// to it, so we check the actual branch operands to see if there are any
120 /// explicit mentions.
121 static bool explicitlyBranchesTo(MachineBasicBlock
*Pred
,
122 MachineBasicBlock
*MBB
) {
123 for (MachineInstr
&MI
: Pred
->terminators())
124 for (MachineOperand
&MO
: MI
.explicit_operands())
125 if (MO
.isMBB() && MO
.getMBB() == MBB
)
130 // Returns an iterator to the earliest position possible within the MBB,
131 // satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet
132 // contains instructions that should go before the marker, and AfterSet contains
133 // ones that should go after the marker. In this function, AfterSet is only
134 // used for sanity checking.
135 static MachineBasicBlock::iterator
136 getEarliestInsertPos(MachineBasicBlock
*MBB
,
137 const SmallPtrSet
<const MachineInstr
*, 4> &BeforeSet
,
138 const SmallPtrSet
<const MachineInstr
*, 4> &AfterSet
) {
139 auto InsertPos
= MBB
->end();
140 while (InsertPos
!= MBB
->begin()) {
141 if (BeforeSet
.count(&*std::prev(InsertPos
))) {
144 for (auto Pos
= InsertPos
, E
= MBB
->begin(); Pos
!= E
; --Pos
)
145 assert(!AfterSet
.count(&*std::prev(Pos
)));
154 // Returns an iterator to the latest position possible within the MBB,
155 // satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet
156 // contains instructions that should go before the marker, and AfterSet contains
157 // ones that should go after the marker. In this function, BeforeSet is only
158 // used for sanity checking.
159 static MachineBasicBlock::iterator
160 getLatestInsertPos(MachineBasicBlock
*MBB
,
161 const SmallPtrSet
<const MachineInstr
*, 4> &BeforeSet
,
162 const SmallPtrSet
<const MachineInstr
*, 4> &AfterSet
) {
163 auto InsertPos
= MBB
->begin();
164 while (InsertPos
!= MBB
->end()) {
165 if (AfterSet
.count(&*InsertPos
)) {
168 for (auto Pos
= InsertPos
, E
= MBB
->end(); Pos
!= E
; ++Pos
)
169 assert(!BeforeSet
.count(&*Pos
));
178 void WebAssemblyCFGStackify::registerScope(MachineInstr
*Begin
,
180 BeginToEnd
[Begin
] = End
;
181 EndToBegin
[End
] = Begin
;
184 void WebAssemblyCFGStackify::registerTryScope(MachineInstr
*Begin
,
186 MachineBasicBlock
*EHPad
) {
187 registerScope(Begin
, End
);
188 TryToEHPad
[Begin
] = EHPad
;
189 EHPadToTry
[EHPad
] = Begin
;
192 void WebAssemblyCFGStackify::unregisterScope(MachineInstr
*Begin
) {
193 assert(BeginToEnd
.count(Begin
));
194 MachineInstr
*End
= BeginToEnd
[Begin
];
195 assert(EndToBegin
.count(End
));
196 BeginToEnd
.erase(Begin
);
197 EndToBegin
.erase(End
);
198 MachineBasicBlock
*EHPad
= TryToEHPad
.lookup(Begin
);
200 assert(EHPadToTry
.count(EHPad
));
201 TryToEHPad
.erase(Begin
);
202 EHPadToTry
.erase(EHPad
);
206 /// Insert a BLOCK marker for branches to MBB (if needed).
207 // TODO Consider a more generalized way of handling block (and also loop and
208 // try) signatures when we implement the multi-value proposal later.
209 void WebAssemblyCFGStackify::placeBlockMarker(MachineBasicBlock
&MBB
) {
210 assert(!MBB
.isEHPad());
211 MachineFunction
&MF
= *MBB
.getParent();
212 auto &MDT
= getAnalysis
<MachineDominatorTree
>();
213 const auto &TII
= *MF
.getSubtarget
<WebAssemblySubtarget
>().getInstrInfo();
214 const auto &MFI
= *MF
.getInfo
<WebAssemblyFunctionInfo
>();
216 // First compute the nearest common dominator of all forward non-fallthrough
217 // predecessors so that we minimize the time that the BLOCK is on the stack,
218 // which reduces overall stack height.
219 MachineBasicBlock
*Header
= nullptr;
220 bool IsBranchedTo
= false;
221 bool IsBrOnExn
= false;
222 MachineInstr
*BrOnExn
= nullptr;
223 int MBBNumber
= MBB
.getNumber();
224 for (MachineBasicBlock
*Pred
: MBB
.predecessors()) {
225 if (Pred
->getNumber() < MBBNumber
) {
226 Header
= Header
? MDT
.findNearestCommonDominator(Header
, Pred
) : Pred
;
227 if (explicitlyBranchesTo(Pred
, &MBB
)) {
229 if (Pred
->getFirstTerminator()->getOpcode() == WebAssembly::BR_ON_EXN
) {
231 assert(!BrOnExn
&& "There should be only one br_on_exn per block");
232 BrOnExn
= &*Pred
->getFirstTerminator();
242 assert(&MBB
!= &MF
.front() && "Header blocks shouldn't have predecessors");
243 MachineBasicBlock
*LayoutPred
= MBB
.getPrevNode();
245 // If the nearest common dominator is inside a more deeply nested context,
246 // walk out to the nearest scope which isn't more deeply nested.
247 for (MachineFunction::iterator
I(LayoutPred
), E(Header
); I
!= E
; --I
) {
248 if (MachineBasicBlock
*ScopeTop
= ScopeTops
[I
->getNumber()]) {
249 if (ScopeTop
->getNumber() > Header
->getNumber()) {
250 // Skip over an intervening scope.
251 I
= std::next(ScopeTop
->getIterator());
253 // We found a scope level at an appropriate depth.
260 // Decide where in Header to put the BLOCK.
262 // Instructions that should go before the BLOCK.
263 SmallPtrSet
<const MachineInstr
*, 4> BeforeSet
;
264 // Instructions that should go after the BLOCK.
265 SmallPtrSet
<const MachineInstr
*, 4> AfterSet
;
266 for (const auto &MI
: *Header
) {
267 // If there is a previously placed LOOP marker and the bottom block of the
268 // loop is above MBB, it should be after the BLOCK, because the loop is
269 // nested in this BLOCK. Otherwise it should be before the BLOCK.
270 if (MI
.getOpcode() == WebAssembly::LOOP
) {
271 auto *LoopBottom
= BeginToEnd
[&MI
]->getParent()->getPrevNode();
272 if (MBB
.getNumber() > LoopBottom
->getNumber())
273 AfterSet
.insert(&MI
);
276 BeforeSet
.insert(&MI
);
280 // All previously inserted BLOCK/TRY markers should be after the BLOCK
281 // because they are all nested blocks.
282 if (MI
.getOpcode() == WebAssembly::BLOCK
||
283 MI
.getOpcode() == WebAssembly::TRY
)
284 AfterSet
.insert(&MI
);
287 // All END_(BLOCK|LOOP|TRY) markers should be before the BLOCK.
288 if (MI
.getOpcode() == WebAssembly::END_BLOCK
||
289 MI
.getOpcode() == WebAssembly::END_LOOP
||
290 MI
.getOpcode() == WebAssembly::END_TRY
)
291 BeforeSet
.insert(&MI
);
294 // Terminators should go after the BLOCK.
295 if (MI
.isTerminator())
296 AfterSet
.insert(&MI
);
299 // Local expression tree should go after the BLOCK.
300 for (auto I
= Header
->getFirstTerminator(), E
= Header
->begin(); I
!= E
;
302 if (std::prev(I
)->isDebugInstr() || std::prev(I
)->isPosition())
304 if (WebAssembly::isChild(*std::prev(I
), MFI
))
305 AfterSet
.insert(&*std::prev(I
));
312 // 'br_on_exn' extracts exnref object and pushes variable number of values
313 // depending on its tag. For C++ exception, its a single i32 value, and the
314 // generated code will be in the form of:
316 // br_on_exn 0, $__cpp_exception
319 WebAssembly::BlockType ReturnType
= WebAssembly::BlockType::Void
;
321 const char *TagName
= BrOnExn
->getOperand(1).getSymbolName();
322 if (std::strcmp(TagName
, "__cpp_exception") != 0)
323 llvm_unreachable("Only C++ exception is supported");
324 ReturnType
= WebAssembly::BlockType::I32
;
327 auto InsertPos
= getLatestInsertPos(Header
, BeforeSet
, AfterSet
);
328 MachineInstr
*Begin
=
329 BuildMI(*Header
, InsertPos
, Header
->findDebugLoc(InsertPos
),
330 TII
.get(WebAssembly::BLOCK
))
331 .addImm(int64_t(ReturnType
));
333 // Decide where in Header to put the END_BLOCK.
336 for (auto &MI
: MBB
) {
338 // END_BLOCK should precede existing LOOP and TRY markers.
339 if (MI
.getOpcode() == WebAssembly::LOOP
||
340 MI
.getOpcode() == WebAssembly::TRY
)
341 AfterSet
.insert(&MI
);
344 // If there is a previously placed END_LOOP marker and the header of the
345 // loop is above this block's header, the END_LOOP should be placed after
346 // the BLOCK, because the loop contains this block. Otherwise the END_LOOP
347 // should be placed before the BLOCK. The same for END_TRY.
348 if (MI
.getOpcode() == WebAssembly::END_LOOP
||
349 MI
.getOpcode() == WebAssembly::END_TRY
) {
350 if (EndToBegin
[&MI
]->getParent()->getNumber() >= Header
->getNumber())
351 BeforeSet
.insert(&MI
);
354 AfterSet
.insert(&MI
);
359 // Mark the end of the block.
360 InsertPos
= getEarliestInsertPos(&MBB
, BeforeSet
, AfterSet
);
361 MachineInstr
*End
= BuildMI(MBB
, InsertPos
, MBB
.findPrevDebugLoc(InsertPos
),
362 TII
.get(WebAssembly::END_BLOCK
));
363 registerScope(Begin
, End
);
365 // Track the farthest-spanning scope that ends at this point.
366 int Number
= MBB
.getNumber();
367 if (!ScopeTops
[Number
] ||
368 ScopeTops
[Number
]->getNumber() > Header
->getNumber())
369 ScopeTops
[Number
] = Header
;
372 /// Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
373 void WebAssemblyCFGStackify::placeLoopMarker(MachineBasicBlock
&MBB
) {
374 MachineFunction
&MF
= *MBB
.getParent();
375 const auto &MLI
= getAnalysis
<MachineLoopInfo
>();
376 const auto &TII
= *MF
.getSubtarget
<WebAssemblySubtarget
>().getInstrInfo();
378 MachineLoop
*Loop
= MLI
.getLoopFor(&MBB
);
379 if (!Loop
|| Loop
->getHeader() != &MBB
)
382 // The operand of a LOOP is the first block after the loop. If the loop is the
383 // bottom of the function, insert a dummy block at the end.
384 MachineBasicBlock
*Bottom
= WebAssembly::getBottom(Loop
);
385 auto Iter
= std::next(Bottom
->getIterator());
386 if (Iter
== MF
.end()) {
387 getAppendixBlock(MF
);
388 Iter
= std::next(Bottom
->getIterator());
390 MachineBasicBlock
*AfterLoop
= &*Iter
;
392 // Decide where in Header to put the LOOP.
393 SmallPtrSet
<const MachineInstr
*, 4> BeforeSet
;
394 SmallPtrSet
<const MachineInstr
*, 4> AfterSet
;
395 for (const auto &MI
: MBB
) {
396 // LOOP marker should be after any existing loop that ends here. Otherwise
397 // we assume the instruction belongs to the loop.
398 if (MI
.getOpcode() == WebAssembly::END_LOOP
)
399 BeforeSet
.insert(&MI
);
402 AfterSet
.insert(&MI
);
406 // Mark the beginning of the loop.
407 auto InsertPos
= getEarliestInsertPos(&MBB
, BeforeSet
, AfterSet
);
408 MachineInstr
*Begin
= BuildMI(MBB
, InsertPos
, MBB
.findDebugLoc(InsertPos
),
409 TII
.get(WebAssembly::LOOP
))
410 .addImm(int64_t(WebAssembly::BlockType::Void
));
412 // Decide where in Header to put the END_LOOP.
416 for (const auto &MI
: MBB
)
417 // Existing END_LOOP markers belong to parent loops of this loop
418 if (MI
.getOpcode() == WebAssembly::END_LOOP
)
419 AfterSet
.insert(&MI
);
422 // Mark the end of the loop (using arbitrary debug location that branched to
423 // the loop end as its location).
424 InsertPos
= getEarliestInsertPos(AfterLoop
, BeforeSet
, AfterSet
);
425 DebugLoc EndDL
= AfterLoop
->pred_empty()
427 : (*AfterLoop
->pred_rbegin())->findBranchDebugLoc();
429 BuildMI(*AfterLoop
, InsertPos
, EndDL
, TII
.get(WebAssembly::END_LOOP
));
430 registerScope(Begin
, End
);
432 assert((!ScopeTops
[AfterLoop
->getNumber()] ||
433 ScopeTops
[AfterLoop
->getNumber()]->getNumber() < MBB
.getNumber()) &&
434 "With block sorting the outermost loop for a block should be first.");
435 if (!ScopeTops
[AfterLoop
->getNumber()])
436 ScopeTops
[AfterLoop
->getNumber()] = &MBB
;
439 void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock
&MBB
) {
440 assert(MBB
.isEHPad());
441 MachineFunction
&MF
= *MBB
.getParent();
442 auto &MDT
= getAnalysis
<MachineDominatorTree
>();
443 const auto &TII
= *MF
.getSubtarget
<WebAssemblySubtarget
>().getInstrInfo();
444 const auto &WEI
= getAnalysis
<WebAssemblyExceptionInfo
>();
445 const auto &MFI
= *MF
.getInfo
<WebAssemblyFunctionInfo
>();
447 // Compute the nearest common dominator of all unwind predecessors
448 MachineBasicBlock
*Header
= nullptr;
449 int MBBNumber
= MBB
.getNumber();
450 for (auto *Pred
: MBB
.predecessors()) {
451 if (Pred
->getNumber() < MBBNumber
) {
452 Header
= Header
? MDT
.findNearestCommonDominator(Header
, Pred
) : Pred
;
453 assert(!explicitlyBranchesTo(Pred
, &MBB
) &&
454 "Explicit branch to an EH pad!");
460 // If this try is at the bottom of the function, insert a dummy block at the
462 WebAssemblyException
*WE
= WEI
.getExceptionFor(&MBB
);
464 MachineBasicBlock
*Bottom
= WebAssembly::getBottom(WE
);
466 auto Iter
= std::next(Bottom
->getIterator());
467 if (Iter
== MF
.end()) {
468 getAppendixBlock(MF
);
469 Iter
= std::next(Bottom
->getIterator());
471 MachineBasicBlock
*Cont
= &*Iter
;
473 assert(Cont
!= &MF
.front());
474 MachineBasicBlock
*LayoutPred
= Cont
->getPrevNode();
476 // If the nearest common dominator is inside a more deeply nested context,
477 // walk out to the nearest scope which isn't more deeply nested.
478 for (MachineFunction::iterator
I(LayoutPred
), E(Header
); I
!= E
; --I
) {
479 if (MachineBasicBlock
*ScopeTop
= ScopeTops
[I
->getNumber()]) {
480 if (ScopeTop
->getNumber() > Header
->getNumber()) {
481 // Skip over an intervening scope.
482 I
= std::next(ScopeTop
->getIterator());
484 // We found a scope level at an appropriate depth.
491 // Decide where in Header to put the TRY.
493 // Instructions that should go before the TRY.
494 SmallPtrSet
<const MachineInstr
*, 4> BeforeSet
;
495 // Instructions that should go after the TRY.
496 SmallPtrSet
<const MachineInstr
*, 4> AfterSet
;
497 for (const auto &MI
: *Header
) {
498 // If there is a previously placed LOOP marker and the bottom block of the
499 // loop is above MBB, it should be after the TRY, because the loop is nested
500 // in this TRY. Otherwise it should be before the TRY.
501 if (MI
.getOpcode() == WebAssembly::LOOP
) {
502 auto *LoopBottom
= BeginToEnd
[&MI
]->getParent()->getPrevNode();
503 if (MBB
.getNumber() > LoopBottom
->getNumber())
504 AfterSet
.insert(&MI
);
507 BeforeSet
.insert(&MI
);
511 // All previously inserted BLOCK/TRY markers should be after the TRY because
512 // they are all nested trys.
513 if (MI
.getOpcode() == WebAssembly::BLOCK
||
514 MI
.getOpcode() == WebAssembly::TRY
)
515 AfterSet
.insert(&MI
);
518 // All END_(BLOCK/LOOP/TRY) markers should be before the TRY.
519 if (MI
.getOpcode() == WebAssembly::END_BLOCK
||
520 MI
.getOpcode() == WebAssembly::END_LOOP
||
521 MI
.getOpcode() == WebAssembly::END_TRY
)
522 BeforeSet
.insert(&MI
);
525 // Terminators should go after the TRY.
526 if (MI
.isTerminator())
527 AfterSet
.insert(&MI
);
530 // If Header unwinds to MBB (= Header contains 'invoke'), the try block should
531 // contain the call within it. So the call should go after the TRY. The
532 // exception is when the header's terminator is a rethrow instruction, in
533 // which case that instruction, not a call instruction before it, is gonna
535 MachineInstr
*ThrowingCall
= nullptr;
536 if (MBB
.isPredecessor(Header
)) {
537 auto TermPos
= Header
->getFirstTerminator();
538 if (TermPos
== Header
->end() ||
539 TermPos
->getOpcode() != WebAssembly::RETHROW
) {
540 for (auto &MI
: reverse(*Header
)) {
542 AfterSet
.insert(&MI
);
544 // Possibly throwing calls are usually wrapped by EH_LABEL
545 // instructions. We don't want to split them and the call.
546 if (MI
.getIterator() != Header
->begin() &&
547 std::prev(MI
.getIterator())->isEHLabel()) {
548 AfterSet
.insert(&*std::prev(MI
.getIterator()));
549 ThrowingCall
= &*std::prev(MI
.getIterator());
557 // Local expression tree should go after the TRY.
558 // For BLOCK placement, we start the search from the previous instruction of a
559 // BB's terminator, but in TRY's case, we should start from the previous
560 // instruction of a call that can throw, or a EH_LABEL that precedes the call,
561 // because the return values of the call's previous instructions can be
562 // stackified and consumed by the throwing call.
563 auto SearchStartPt
= ThrowingCall
? MachineBasicBlock::iterator(ThrowingCall
)
564 : Header
->getFirstTerminator();
565 for (auto I
= SearchStartPt
, E
= Header
->begin(); I
!= E
; --I
) {
566 if (std::prev(I
)->isDebugInstr() || std::prev(I
)->isPosition())
568 if (WebAssembly::isChild(*std::prev(I
), MFI
))
569 AfterSet
.insert(&*std::prev(I
));
575 auto InsertPos
= getLatestInsertPos(Header
, BeforeSet
, AfterSet
);
576 MachineInstr
*Begin
=
577 BuildMI(*Header
, InsertPos
, Header
->findDebugLoc(InsertPos
),
578 TII
.get(WebAssembly::TRY
))
579 .addImm(int64_t(WebAssembly::BlockType::Void
));
581 // Decide where in Header to put the END_TRY.
584 for (const auto &MI
: *Cont
) {
586 // END_TRY should precede existing LOOP and BLOCK markers.
587 if (MI
.getOpcode() == WebAssembly::LOOP
||
588 MI
.getOpcode() == WebAssembly::BLOCK
)
589 AfterSet
.insert(&MI
);
591 // All END_TRY markers placed earlier belong to exceptions that contains
593 if (MI
.getOpcode() == WebAssembly::END_TRY
)
594 AfterSet
.insert(&MI
);
597 // If there is a previously placed END_LOOP marker and its header is after
598 // where TRY marker is, this loop is contained within the 'catch' part, so
599 // the END_TRY marker should go after that. Otherwise, the whole try-catch
600 // is contained within this loop, so the END_TRY should go before that.
601 if (MI
.getOpcode() == WebAssembly::END_LOOP
) {
602 // For a LOOP to be after TRY, LOOP's BB should be after TRY's BB; if they
603 // are in the same BB, LOOP is always before TRY.
604 if (EndToBegin
[&MI
]->getParent()->getNumber() > Header
->getNumber())
605 BeforeSet
.insert(&MI
);
608 AfterSet
.insert(&MI
);
612 // It is not possible for an END_BLOCK to be already in this block.
615 // Mark the end of the TRY.
616 InsertPos
= getEarliestInsertPos(Cont
, BeforeSet
, AfterSet
);
618 BuildMI(*Cont
, InsertPos
, Bottom
->findBranchDebugLoc(),
619 TII
.get(WebAssembly::END_TRY
));
620 registerTryScope(Begin
, End
, &MBB
);
622 // Track the farthest-spanning scope that ends at this point. We create two
623 // mappings: (BB with 'end_try' -> BB with 'try') and (BB with 'catch' -> BB
624 // with 'try'). We need to create 'catch' -> 'try' mapping here too because
625 // markers should not span across 'catch'. For example, this should not
633 for (int Number
: {Cont
->getNumber(), MBB
.getNumber()}) {
634 if (!ScopeTops
[Number
] ||
635 ScopeTops
[Number
]->getNumber() > Header
->getNumber())
636 ScopeTops
[Number
] = Header
;
640 void WebAssemblyCFGStackify::removeUnnecessaryInstrs(MachineFunction
&MF
) {
641 const auto &TII
= *MF
.getSubtarget
<WebAssemblySubtarget
>().getInstrInfo();
643 // When there is an unconditional branch right before a catch instruction and
644 // it branches to the end of end_try marker, we don't need the branch, because
645 // it there is no exception, the control flow transfers to that point anyway.
649 // br bb2 <- Not necessary
655 for (auto &MBB
: MF
) {
659 MachineBasicBlock
*TBB
= nullptr, *FBB
= nullptr;
660 SmallVector
<MachineOperand
, 4> Cond
;
661 MachineBasicBlock
*EHPadLayoutPred
= MBB
.getPrevNode();
662 MachineBasicBlock
*Cont
= BeginToEnd
[EHPadToTry
[&MBB
]]->getParent();
663 bool Analyzable
= !TII
.analyzeBranch(*EHPadLayoutPred
, TBB
, FBB
, Cond
);
664 if (Analyzable
&& ((Cond
.empty() && TBB
&& TBB
== Cont
) ||
665 (!Cond
.empty() && FBB
&& FBB
== Cont
)))
666 TII
.removeBranch(*EHPadLayoutPred
);
669 // When there are block / end_block markers that overlap with try / end_try
670 // markers, and the block and try markers' return types are the same, the
671 // block /end_block markers are not necessary, because try / end_try markers
672 // also can serve as boundaries for branches.
673 // block <- Not necessary
679 // end <- Not necessary
680 SmallVector
<MachineInstr
*, 32> ToDelete
;
681 for (auto &MBB
: MF
) {
682 for (auto &MI
: MBB
) {
683 if (MI
.getOpcode() != WebAssembly::TRY
)
686 MachineInstr
*Try
= &MI
, *EndTry
= BeginToEnd
[Try
];
687 MachineBasicBlock
*TryBB
= Try
->getParent();
688 MachineBasicBlock
*Cont
= EndTry
->getParent();
689 int64_t RetType
= Try
->getOperand(0).getImm();
690 for (auto B
= Try
->getIterator(), E
= std::next(EndTry
->getIterator());
691 B
!= TryBB
->begin() && E
!= Cont
->end() &&
692 std::prev(B
)->getOpcode() == WebAssembly::BLOCK
&&
693 E
->getOpcode() == WebAssembly::END_BLOCK
&&
694 std::prev(B
)->getOperand(0).getImm() == RetType
;
696 ToDelete
.push_back(&*std::prev(B
));
697 ToDelete
.push_back(&*E
);
701 for (auto *MI
: ToDelete
) {
702 if (MI
->getOpcode() == WebAssembly::BLOCK
)
704 MI
->eraseFromParent();
708 // When MBB is split into MBB and Split, we should unstackify defs in MBB that
709 // have their uses in Split.
710 static void unstackifyVRegsUsedInSplitBB(MachineBasicBlock
&MBB
,
711 MachineBasicBlock
&Split
,
712 WebAssemblyFunctionInfo
&MFI
,
713 MachineRegisterInfo
&MRI
) {
714 for (auto &MI
: Split
) {
715 for (auto &MO
: MI
.explicit_uses()) {
716 if (!MO
.isReg() || Register::isPhysicalRegister(MO
.getReg()))
718 if (MachineInstr
*Def
= MRI
.getUniqueVRegDef(MO
.getReg()))
719 if (Def
->getParent() == &MBB
)
720 MFI
.unstackifyVReg(MO
.getReg());
725 bool WebAssemblyCFGStackify::fixUnwindMismatches(MachineFunction
&MF
) {
726 const auto &TII
= *MF
.getSubtarget
<WebAssemblySubtarget
>().getInstrInfo();
727 auto &MFI
= *MF
.getInfo
<WebAssemblyFunctionInfo
>();
728 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
730 // Linearizing the control flow by placing TRY / END_TRY markers can create
731 // mismatches in unwind destinations. There are two kinds of mismatches we
732 // try to solve here.
734 // 1. When an instruction may throw, but the EH pad it will unwind to can be
735 // different from the original CFG.
737 // Example: we have the following CFG:
739 // call @foo (if it throws, unwind to bb2)
741 // call @bar (if it throws, unwind to bb3)
749 // And the CFG is sorted in this order. Then after placing TRY markers, it
750 // will look like: (BB markers are omitted)
754 // call @bar (if it throws, unwind to bb3)
755 // catch <- ehpad (bb2)
758 // catch <- ehpad (bb3)
762 // Now if bar() throws, it is going to end up ip in bb2, not bb3, where it
763 // is supposed to end up. We solve this problem by
764 // a. Split the target unwind EH pad (here bb3) so that the handler body is
765 // right after 'end_try', which means we extract the handler body out of
766 // the catch block. We do this because this handler body should be
767 // somewhere branch-eable from the inner scope.
768 // b. Wrap the call that has an incorrect unwind destination ('call @bar'
769 // here) with a nested try/catch/end_try scope, and within the new catch
770 // block, branches to the handler body.
771 // c. Place a branch after the newly inserted nested end_try so it can bypass
772 // the handler body, which is now outside of a catch block.
774 // The result will like as follows. (new: a) means this instruction is newly
775 // created in the process of doing 'a' above.
777 // block $label0 (new: placeBlockMarker)
784 // local.set n / drop (new: b)
785 // br $label1 (new: b)
787 // catch <- ehpad (bb2)
789 // br $label0 (new: c)
790 // catch <- ehpad (bb3)
791 // end_try (hoisted: a)
793 // end_block (new: placeBlockMarker)
795 // Note that the new wrapping block/end_block will be generated later in
798 // TODO Currently local.set and local.gets are generated to move exnref value
799 // created by catches. That's because we don't support yielding values from a
800 // block in LLVM machine IR yet, even though it is supported by wasm. Delete
801 // unnecessary local.get/local.sets once yielding values from a block is
802 // supported. The full EH spec requires multi-value support to do this, but
803 // for C++ we don't yet need it because we only throw a single i32.
806 // 2. The same as 1, but in this case an instruction unwinds to a caller
807 // function and not another EH pad.
809 // Example: we have the following CFG:
811 // call @foo (if it throws, unwind to bb2)
813 // call @bar (if it throws, unwind to caller)
818 // And the CFG is sorted in this order. Then after placing TRY markers, it
822 // call @bar (if it throws, unwind to caller)
823 // catch <- ehpad (bb2)
827 // Now if bar() throws, it is going to end up ip in bb2, when it is supposed
828 // throw up to the caller.
829 // We solve this problem by
830 // a. Create a new 'appendix' BB at the end of the function and put a single
831 // 'rethrow' instruction (+ local.get) in there.
832 // b. Wrap the call that has an incorrect unwind destination ('call @bar'
833 // here) with a nested try/catch/end_try scope, and within the new catch
834 // block, branches to the new appendix block.
836 // block $label0 (new: placeBlockMarker)
842 // local.set n (new: b)
843 // br $label0 (new: b)
845 // catch <- ehpad (bb2)
849 // end_block (new: placeBlockMarker)
850 // local.get n (new: a) <- appendix block
853 // In case there are multiple calls in a BB that may throw to the caller, they
854 // can be wrapped together in one nested try scope. (In 1, this couldn't
855 // happen, because may-throwing instruction there had an unwind destination,
856 // i.e., it was an invoke before, and there could be only one invoke within a
859 SmallVector
<const MachineBasicBlock
*, 8> EHPadStack
;
860 // Range of intructions to be wrapped in a new nested try/catch
861 using TryRange
= std::pair
<MachineInstr
*, MachineInstr
*>;
862 // In original CFG, <unwind destination BB, a vector of try ranges>
863 DenseMap
<MachineBasicBlock
*, SmallVector
<TryRange
, 4>> UnwindDestToTryRanges
;
864 // In new CFG, <destination to branch to, a vector of try ranges>
865 DenseMap
<MachineBasicBlock
*, SmallVector
<TryRange
, 4>> BrDestToTryRanges
;
866 // In new CFG, <destination to branch to, register containing exnref>
867 DenseMap
<MachineBasicBlock
*, unsigned> BrDestToExnReg
;
869 // Gather possibly throwing calls (i.e., previously invokes) whose current
870 // unwind destination is not the same as the original CFG.
871 for (auto &MBB
: reverse(MF
)) {
872 bool SeenThrowableInstInBB
= false;
873 for (auto &MI
: reverse(MBB
)) {
874 if (MI
.getOpcode() == WebAssembly::TRY
)
875 EHPadStack
.pop_back();
876 else if (MI
.getOpcode() == WebAssembly::CATCH
)
877 EHPadStack
.push_back(MI
.getParent());
879 // In this loop we only gather calls that have an EH pad to unwind. So
880 // there will be at most 1 such call (= invoke) in a BB, so after we've
881 // seen one, we can skip the rest of BB. Also if MBB has no EH pad
882 // successor or MI does not throw, this is not an invoke.
883 if (SeenThrowableInstInBB
|| !MBB
.hasEHPadSuccessor() ||
884 !WebAssembly::mayThrow(MI
))
886 SeenThrowableInstInBB
= true;
888 // If the EH pad on the stack top is where this instruction should unwind
890 MachineBasicBlock
*UnwindDest
= nullptr;
891 for (auto *Succ
: MBB
.successors()) {
892 if (Succ
->isEHPad()) {
897 if (EHPadStack
.back() == UnwindDest
)
900 // If not, record the range.
901 UnwindDestToTryRanges
[UnwindDest
].push_back(TryRange(&MI
, &MI
));
905 assert(EHPadStack
.empty());
907 // Gather possibly throwing calls that are supposed to unwind up to the caller
908 // if they throw, but currently unwind to an incorrect destination. Unlike the
909 // loop above, there can be multiple calls within a BB that unwind to the
910 // caller, which we should group together in a range.
911 bool NeedAppendixBlock
= false;
912 for (auto &MBB
: reverse(MF
)) {
913 MachineInstr
*RangeBegin
= nullptr, *RangeEnd
= nullptr; // inclusive
914 for (auto &MI
: reverse(MBB
)) {
915 if (MI
.getOpcode() == WebAssembly::TRY
)
916 EHPadStack
.pop_back();
917 else if (MI
.getOpcode() == WebAssembly::CATCH
)
918 EHPadStack
.push_back(MI
.getParent());
920 // If MBB has an EH pad successor, this inst does not unwind to caller.
921 if (MBB
.hasEHPadSuccessor())
924 // We wrap up the current range when we see a marker even if we haven't
926 if (RangeEnd
&& WebAssembly::isMarker(MI
.getOpcode())) {
927 NeedAppendixBlock
= true;
928 // Record the range. nullptr here means the unwind destination is the
930 UnwindDestToTryRanges
[nullptr].push_back(
931 TryRange(RangeBegin
, RangeEnd
));
932 RangeBegin
= RangeEnd
= nullptr; // Reset range pointers
935 // If EHPadStack is empty, that means it is correctly unwind to caller if
936 // it throws, so we're good. If MI does not throw, we're good too.
937 if (EHPadStack
.empty() || !WebAssembly::mayThrow(MI
))
940 // We found an instruction that unwinds to the caller but currently has an
941 // incorrect unwind destination. Create a new range or increment the
942 // currently existing range.
944 RangeBegin
= RangeEnd
= &MI
;
950 NeedAppendixBlock
= true;
951 // Record the range. nullptr here means the unwind destination is the
953 UnwindDestToTryRanges
[nullptr].push_back(TryRange(RangeBegin
, RangeEnd
));
954 RangeBegin
= RangeEnd
= nullptr; // Reset range pointers
958 assert(EHPadStack
.empty());
959 // We don't have any unwind destination mismatches to resolve.
960 if (UnwindDestToTryRanges
.empty())
963 // If we found instructions that should unwind to the caller but currently
964 // have incorrect unwind destination, we create an appendix block at the end
965 // of the function with a local.get and a rethrow instruction.
966 if (NeedAppendixBlock
) {
967 auto *AppendixBB
= getAppendixBlock(MF
);
968 Register ExnReg
= MRI
.createVirtualRegister(&WebAssembly::EXNREFRegClass
);
969 BuildMI(AppendixBB
, DebugLoc(), TII
.get(WebAssembly::RETHROW
))
971 // These instruction ranges should branch to this appendix BB.
972 for (auto Range
: UnwindDestToTryRanges
[nullptr])
973 BrDestToTryRanges
[AppendixBB
].push_back(Range
);
974 BrDestToExnReg
[AppendixBB
] = ExnReg
;
977 // We loop through unwind destination EH pads that are targeted from some
978 // inner scopes. Because these EH pads are destination of more than one scope
979 // now, we split them so that the handler body is after 'end_try'.
983 // local.set n / drop
992 // local.set n / drop
994 // end_try (hoisted from 'cont' BB)
995 // handler body (taken from 'ehpad')
998 for (auto &P
: UnwindDestToTryRanges
) {
999 NumUnwindMismatches
+= P
.second
.size();
1001 // This means the destination is the appendix BB, which was separately
1006 MachineBasicBlock
*EHPad
= P
.first
;
1008 // Find 'catch' and 'local.set' or 'drop' instruction that follows the
1009 // 'catch'. If -wasm-disable-explicit-locals is not set, 'catch' should be
1010 // always followed by either 'local.set' or a 'drop', because 'br_on_exn' is
1011 // generated after 'catch' in LateEHPrepare and we don't support blocks
1012 // taking values yet.
1013 MachineInstr
*Catch
= nullptr;
1014 unsigned ExnReg
= 0;
1015 for (auto &MI
: *EHPad
) {
1016 switch (MI
.getOpcode()) {
1017 case WebAssembly::CATCH
:
1019 ExnReg
= Catch
->getOperand(0).getReg();
1023 assert(Catch
&& "EH pad does not have a catch");
1024 assert(ExnReg
!= 0 && "Invalid register");
1026 auto SplitPos
= std::next(Catch
->getIterator());
1028 // Create a new BB that's gonna be the destination for branches from the
1029 // inner mismatched scope.
1030 MachineInstr
*BeginTry
= EHPadToTry
[EHPad
];
1031 MachineInstr
*EndTry
= BeginToEnd
[BeginTry
];
1032 MachineBasicBlock
*Cont
= EndTry
->getParent();
1033 auto *BrDest
= MF
.CreateMachineBasicBlock();
1034 MF
.insert(std::next(EHPad
->getIterator()), BrDest
);
1035 // Hoist up the existing 'end_try'.
1036 BrDest
->insert(BrDest
->end(), EndTry
->removeFromParent());
1037 // Take out the handler body from EH pad to the new branch destination BB.
1038 BrDest
->splice(BrDest
->end(), EHPad
, SplitPos
, EHPad
->end());
1039 unstackifyVRegsUsedInSplitBB(*EHPad
, *BrDest
, MFI
, MRI
);
1040 // Fix predecessor-successor relationship.
1041 BrDest
->transferSuccessors(EHPad
);
1042 EHPad
->addSuccessor(BrDest
);
1044 // All try ranges that were supposed to unwind to this EH pad now have to
1045 // branch to this new branch dest BB.
1046 for (auto Range
: UnwindDestToTryRanges
[EHPad
])
1047 BrDestToTryRanges
[BrDest
].push_back(Range
);
1048 BrDestToExnReg
[BrDest
] = ExnReg
;
1050 // In case we fall through to the continuation BB after the catch block, we
1051 // now have to add a branch to it.
1055 // (falls through to 'cont')
1069 MachineBasicBlock
*EHPadLayoutPred
= &*std::prev(EHPad
->getIterator());
1070 MachineBasicBlock
*TBB
= nullptr, *FBB
= nullptr;
1071 SmallVector
<MachineOperand
, 4> Cond
;
1072 bool Analyzable
= !TII
.analyzeBranch(*EHPadLayoutPred
, TBB
, FBB
, Cond
);
1073 if (Analyzable
&& !TBB
&& !FBB
) {
1074 DebugLoc DL
= EHPadLayoutPred
->empty()
1076 : EHPadLayoutPred
->rbegin()->getDebugLoc();
1077 BuildMI(EHPadLayoutPred
, DL
, TII
.get(WebAssembly::BR
)).addMBB(Cont
);
1081 // For possibly throwing calls whose unwind destinations are currently
1082 // incorrect because of CFG linearization, we wrap them with a nested
1083 // try/catch/end_try, and within the new catch block, we branch to the correct
1087 // call @foo <- Unwind destination mismatch!
1095 // nested-ehpad: (new)
1097 // local.set n / drop (new)
1099 // nested-end: (new)
1103 for (auto &P
: BrDestToTryRanges
) {
1104 MachineBasicBlock
*BrDest
= P
.first
;
1105 auto &TryRanges
= P
.second
;
1106 unsigned ExnReg
= BrDestToExnReg
[BrDest
];
1108 for (auto Range
: TryRanges
) {
1109 MachineInstr
*RangeBegin
= nullptr, *RangeEnd
= nullptr;
1110 std::tie(RangeBegin
, RangeEnd
) = Range
;
1111 auto *MBB
= RangeBegin
->getParent();
1113 // Include possible EH_LABELs in the range
1114 if (RangeBegin
->getIterator() != MBB
->begin() &&
1115 std::prev(RangeBegin
->getIterator())->isEHLabel())
1116 RangeBegin
= &*std::prev(RangeBegin
->getIterator());
1117 if (std::next(RangeEnd
->getIterator()) != MBB
->end() &&
1118 std::next(RangeEnd
->getIterator())->isEHLabel())
1119 RangeEnd
= &*std::next(RangeEnd
->getIterator());
1121 MachineBasicBlock
*EHPad
= nullptr;
1122 for (auto *Succ
: MBB
->successors()) {
1123 if (Succ
->isEHPad()) {
1129 // Create the nested try instruction.
1130 MachineInstr
*NestedTry
=
1131 BuildMI(*MBB
, *RangeBegin
, RangeBegin
->getDebugLoc(),
1132 TII
.get(WebAssembly::TRY
))
1133 .addImm(int64_t(WebAssembly::BlockType::Void
));
1135 // Create the nested EH pad and fill instructions in.
1136 MachineBasicBlock
*NestedEHPad
= MF
.CreateMachineBasicBlock();
1137 MF
.insert(std::next(MBB
->getIterator()), NestedEHPad
);
1138 NestedEHPad
->setIsEHPad();
1139 NestedEHPad
->setIsEHScopeEntry();
1140 BuildMI(NestedEHPad
, RangeEnd
->getDebugLoc(), TII
.get(WebAssembly::CATCH
),
1142 BuildMI(NestedEHPad
, RangeEnd
->getDebugLoc(), TII
.get(WebAssembly::BR
))
1145 // Create the nested continuation BB and end_try instruction.
1146 MachineBasicBlock
*NestedCont
= MF
.CreateMachineBasicBlock();
1147 MF
.insert(std::next(NestedEHPad
->getIterator()), NestedCont
);
1148 MachineInstr
*NestedEndTry
=
1149 BuildMI(*NestedCont
, NestedCont
->begin(), RangeEnd
->getDebugLoc(),
1150 TII
.get(WebAssembly::END_TRY
));
1151 // In case MBB has more instructions after the try range, move them to the
1152 // new nested continuation BB.
1153 NestedCont
->splice(NestedCont
->end(), MBB
,
1154 std::next(RangeEnd
->getIterator()), MBB
->end());
1155 unstackifyVRegsUsedInSplitBB(*MBB
, *NestedCont
, MFI
, MRI
);
1156 registerTryScope(NestedTry
, NestedEndTry
, NestedEHPad
);
1158 // Fix predecessor-successor relationship.
1159 NestedCont
->transferSuccessors(MBB
);
1161 NestedCont
->removeSuccessor(EHPad
);
1162 MBB
->addSuccessor(NestedEHPad
);
1163 MBB
->addSuccessor(NestedCont
);
1164 NestedEHPad
->addSuccessor(BrDest
);
1168 // Renumber BBs and recalculate ScopeTop info because new BBs might have been
1169 // created and inserted above.
1170 MF
.RenumberBlocks();
1172 ScopeTops
.resize(MF
.getNumBlockIDs());
1173 for (auto &MBB
: reverse(MF
)) {
1174 for (auto &MI
: reverse(MBB
)) {
1175 if (ScopeTops
[MBB
.getNumber()])
1177 switch (MI
.getOpcode()) {
1178 case WebAssembly::END_BLOCK
:
1179 case WebAssembly::END_LOOP
:
1180 case WebAssembly::END_TRY
:
1181 ScopeTops
[MBB
.getNumber()] = EndToBegin
[&MI
]->getParent();
1183 case WebAssembly::CATCH
:
1184 ScopeTops
[MBB
.getNumber()] = EHPadToTry
[&MBB
]->getParent();
1190 // Recompute the dominator tree.
1191 getAnalysis
<MachineDominatorTree
>().runOnMachineFunction(MF
);
1193 // Place block markers for newly added branches.
1194 SmallVector
<MachineBasicBlock
*, 8> BrDests
;
1195 for (auto &P
: BrDestToTryRanges
)
1196 BrDests
.push_back(P
.first
);
1198 [&](const MachineBasicBlock
*A
, const MachineBasicBlock
*B
) {
1199 auto ANum
= A
->getNumber();
1200 auto BNum
= B
->getNumber();
1203 for (auto *Dest
: BrDests
)
1204 placeBlockMarker(*Dest
);
1210 getDepth(const SmallVectorImpl
<const MachineBasicBlock
*> &Stack
,
1211 const MachineBasicBlock
*MBB
) {
1213 for (auto X
: reverse(Stack
)) {
1218 assert(Depth
< Stack
.size() && "Branch destination should be in scope");
1222 /// In normal assembly languages, when the end of a function is unreachable,
1223 /// because the function ends in an infinite loop or a noreturn call or similar,
1224 /// it isn't necessary to worry about the function return type at the end of
1225 /// the function, because it's never reached. However, in WebAssembly, blocks
1226 /// that end at the function end need to have a return type signature that
1227 /// matches the function signature, even though it's unreachable. This function
1228 /// checks for such cases and fixes up the signatures.
1229 void WebAssemblyCFGStackify::fixEndsAtEndOfFunction(MachineFunction
&MF
) {
1230 const auto &MFI
= *MF
.getInfo
<WebAssemblyFunctionInfo
>();
1232 if (MFI
.getResults().empty())
1235 // MCInstLower will add the proper types to multivalue signatures based on the
1236 // function return type
1237 WebAssembly::BlockType RetType
=
1238 MFI
.getResults().size() > 1
1239 ? WebAssembly::BlockType::Multivalue
1240 : WebAssembly::BlockType(
1241 WebAssembly::toValType(MFI
.getResults().front()));
1243 for (MachineBasicBlock
&MBB
: reverse(MF
)) {
1244 for (MachineInstr
&MI
: reverse(MBB
)) {
1245 if (MI
.isPosition() || MI
.isDebugInstr())
1247 switch (MI
.getOpcode()) {
1248 case WebAssembly::END_BLOCK
:
1249 case WebAssembly::END_LOOP
:
1250 case WebAssembly::END_TRY
:
1251 EndToBegin
[&MI
]->getOperand(0).setImm(int32_t(RetType
));
1254 // Something other than an `end`. We're done.
1261 // WebAssembly functions end with an end instruction, as if the function body
1263 static void appendEndToFunction(MachineFunction
&MF
,
1264 const WebAssemblyInstrInfo
&TII
) {
1265 BuildMI(MF
.back(), MF
.back().end(),
1266 MF
.back().findPrevDebugLoc(MF
.back().end()),
1267 TII
.get(WebAssembly::END_FUNCTION
));
1270 /// Insert LOOP/TRY/BLOCK markers at appropriate places.
1271 void WebAssemblyCFGStackify::placeMarkers(MachineFunction
&MF
) {
1272 // We allocate one more than the number of blocks in the function to
1273 // accommodate for the possible fake block we may insert at the end.
1274 ScopeTops
.resize(MF
.getNumBlockIDs() + 1);
1275 // Place the LOOP for MBB if MBB is the header of a loop.
1276 for (auto &MBB
: MF
)
1277 placeLoopMarker(MBB
);
1279 const MCAsmInfo
*MCAI
= MF
.getTarget().getMCAsmInfo();
1280 for (auto &MBB
: MF
) {
1281 if (MBB
.isEHPad()) {
1282 // Place the TRY for MBB if MBB is the EH pad of an exception.
1283 if (MCAI
->getExceptionHandlingType() == ExceptionHandling::Wasm
&&
1284 MF
.getFunction().hasPersonalityFn())
1285 placeTryMarker(MBB
);
1287 // Place the BLOCK for MBB if MBB is branched to from above.
1288 placeBlockMarker(MBB
);
1291 // Fix mismatches in unwind destinations induced by linearizing the code.
1292 if (MCAI
->getExceptionHandlingType() == ExceptionHandling::Wasm
&&
1293 MF
.getFunction().hasPersonalityFn())
1294 fixUnwindMismatches(MF
);
1297 void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction
&MF
) {
1298 // Now rewrite references to basic blocks to be depth immediates.
1299 SmallVector
<const MachineBasicBlock
*, 8> Stack
;
1300 for (auto &MBB
: reverse(MF
)) {
1301 for (auto I
= MBB
.rbegin(), E
= MBB
.rend(); I
!= E
; ++I
) {
1302 MachineInstr
&MI
= *I
;
1303 switch (MI
.getOpcode()) {
1304 case WebAssembly::BLOCK
:
1305 case WebAssembly::TRY
:
1306 assert(ScopeTops
[Stack
.back()->getNumber()]->getNumber() <=
1308 "Block/try marker should be balanced");
1312 case WebAssembly::LOOP
:
1313 assert(Stack
.back() == &MBB
&& "Loop top should be balanced");
1317 case WebAssembly::END_BLOCK
:
1318 case WebAssembly::END_TRY
:
1319 Stack
.push_back(&MBB
);
1322 case WebAssembly::END_LOOP
:
1323 Stack
.push_back(EndToBegin
[&MI
]->getParent());
1327 if (MI
.isTerminator()) {
1328 // Rewrite MBB operands to be depth immediates.
1329 SmallVector
<MachineOperand
, 4> Ops(MI
.operands());
1330 while (MI
.getNumOperands() > 0)
1331 MI
.RemoveOperand(MI
.getNumOperands() - 1);
1332 for (auto MO
: Ops
) {
1334 MO
= MachineOperand::CreateImm(getDepth(Stack
, MO
.getMBB()));
1335 MI
.addOperand(MF
, MO
);
1342 assert(Stack
.empty() && "Control flow should be balanced");
1345 void WebAssemblyCFGStackify::releaseMemory() {
1351 AppendixBB
= nullptr;
1354 bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction
&MF
) {
1355 LLVM_DEBUG(dbgs() << "********** CFG Stackifying **********\n"
1356 "********** Function: "
1357 << MF
.getName() << '\n');
1358 const MCAsmInfo
*MCAI
= MF
.getTarget().getMCAsmInfo();
1362 // Liveness is not tracked for VALUE_STACK physreg.
1363 MF
.getRegInfo().invalidateLiveness();
1365 // Place the BLOCK/LOOP/TRY markers to indicate the beginnings of scopes.
1368 // Remove unnecessary instructions possibly introduced by try/end_trys.
1369 if (MCAI
->getExceptionHandlingType() == ExceptionHandling::Wasm
&&
1370 MF
.getFunction().hasPersonalityFn())
1371 removeUnnecessaryInstrs(MF
);
1373 // Convert MBB operands in terminators to relative depth immediates.
1374 rewriteDepthImmediates(MF
);
1376 // Fix up block/loop/try signatures at the end of the function to conform to
1377 // WebAssembly's rules.
1378 fixEndsAtEndOfFunction(MF
);
1380 // Add an end instruction at the end of the function body.
1381 const auto &TII
= *MF
.getSubtarget
<WebAssemblySubtarget
>().getInstrInfo();
1382 if (!MF
.getSubtarget
<WebAssemblySubtarget
>()
1384 .isOSBinFormatELF())
1385 appendEndToFunction(MF
, TII
);
1387 MF
.getInfo
<WebAssemblyFunctionInfo
>()->setCFGStackified();