10b57cec5SDimitry Andric //===-- WebAssemblyCFGStackify.cpp - CFG Stackification -------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file implements a CFG stacking pass.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric /// This pass inserts BLOCK, LOOP, and TRY markers to mark the start of scopes,
130b57cec5SDimitry Andric /// since scope boundaries serve as the labels for WebAssembly's control
140b57cec5SDimitry Andric /// transfers.
150b57cec5SDimitry Andric ///
160b57cec5SDimitry Andric /// This is sufficient to convert arbitrary CFGs into a form that works on
170b57cec5SDimitry Andric /// WebAssembly, provided that all loops are single-entry.
180b57cec5SDimitry Andric ///
190b57cec5SDimitry Andric /// In case we use exceptions, this pass also fixes mismatches in unwind
200b57cec5SDimitry Andric /// destinations created during transforming CFG into wasm structured format.
210b57cec5SDimitry Andric ///
220b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
230b57cec5SDimitry Andric 
24fe6060f1SDimitry Andric #include "Utils/WebAssemblyTypeUtilities.h"
250b57cec5SDimitry Andric #include "WebAssembly.h"
260b57cec5SDimitry Andric #include "WebAssemblyExceptionInfo.h"
270b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
28e8d8bef9SDimitry Andric #include "WebAssemblySortRegion.h"
290b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
305f757f3fSDimitry Andric #include "WebAssemblyUtilities.h"
310b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
348bcb0991SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
35fe6060f1SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h"
360b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
375ffd83dbSDimitry Andric #include "llvm/Target/TargetMachine.h"
380b57cec5SDimitry Andric using namespace llvm;
39e8d8bef9SDimitry Andric using WebAssembly::SortRegionInfo;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-cfg-stackify"
420b57cec5SDimitry Andric 
43fe6060f1SDimitry Andric STATISTIC(NumCallUnwindMismatches, "Number of call unwind mismatches found");
44fe6060f1SDimitry Andric STATISTIC(NumCatchUnwindMismatches, "Number of catch unwind mismatches found");
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric namespace {
470b57cec5SDimitry Andric class WebAssemblyCFGStackify final : public MachineFunctionPass {
getPassName() const480b57cec5SDimitry Andric   StringRef getPassName() const override { return "WebAssembly CFG Stackify"; }
490b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const500b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
510b57cec5SDimitry Andric     AU.addRequired<MachineDominatorTree>();
520b57cec5SDimitry Andric     AU.addRequired<MachineLoopInfo>();
530b57cec5SDimitry Andric     AU.addRequired<WebAssemblyExceptionInfo>();
540b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
550b57cec5SDimitry Andric   }
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   // For each block whose label represents the end of a scope, record the block
600b57cec5SDimitry Andric   // which holds the beginning of the scope. This will allow us to quickly skip
610b57cec5SDimitry Andric   // over scoped regions when walking blocks.
620b57cec5SDimitry Andric   SmallVector<MachineBasicBlock *, 8> ScopeTops;
updateScopeTops(MachineBasicBlock * Begin,MachineBasicBlock * End)63e8d8bef9SDimitry Andric   void updateScopeTops(MachineBasicBlock *Begin, MachineBasicBlock *End) {
64e8d8bef9SDimitry Andric     int EndNo = End->getNumber();
65e8d8bef9SDimitry Andric     if (!ScopeTops[EndNo] || ScopeTops[EndNo]->getNumber() > Begin->getNumber())
66e8d8bef9SDimitry Andric       ScopeTops[EndNo] = Begin;
67e8d8bef9SDimitry Andric   }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   // Placing markers.
700b57cec5SDimitry Andric   void placeMarkers(MachineFunction &MF);
710b57cec5SDimitry Andric   void placeBlockMarker(MachineBasicBlock &MBB);
720b57cec5SDimitry Andric   void placeLoopMarker(MachineBasicBlock &MBB);
730b57cec5SDimitry Andric   void placeTryMarker(MachineBasicBlock &MBB);
74fe6060f1SDimitry Andric 
75fe6060f1SDimitry Andric   // Exception handling related functions
76fe6060f1SDimitry Andric   bool fixCallUnwindMismatches(MachineFunction &MF);
77fe6060f1SDimitry Andric   bool fixCatchUnwindMismatches(MachineFunction &MF);
78fe6060f1SDimitry Andric   void addTryDelegate(MachineInstr *RangeBegin, MachineInstr *RangeEnd,
79fe6060f1SDimitry Andric                       MachineBasicBlock *DelegateDest);
80fe6060f1SDimitry Andric   void recalculateScopeTops(MachineFunction &MF);
810b57cec5SDimitry Andric   void removeUnnecessaryInstrs(MachineFunction &MF);
82fe6060f1SDimitry Andric 
83fe6060f1SDimitry Andric   // Wrap-up
84fe6060f1SDimitry Andric   using EndMarkerInfo =
85fe6060f1SDimitry Andric       std::pair<const MachineBasicBlock *, const MachineInstr *>;
86fe6060f1SDimitry Andric   unsigned getBranchDepth(const SmallVectorImpl<EndMarkerInfo> &Stack,
87fe6060f1SDimitry Andric                           const MachineBasicBlock *MBB);
88fe6060f1SDimitry Andric   unsigned getDelegateDepth(const SmallVectorImpl<EndMarkerInfo> &Stack,
89fe6060f1SDimitry Andric                             const MachineBasicBlock *MBB);
90fe6060f1SDimitry Andric   unsigned
91fe6060f1SDimitry Andric   getRethrowDepth(const SmallVectorImpl<EndMarkerInfo> &Stack,
92fe6060f1SDimitry Andric                   const SmallVectorImpl<const MachineBasicBlock *> &EHPadStack);
930b57cec5SDimitry Andric   void rewriteDepthImmediates(MachineFunction &MF);
940b57cec5SDimitry Andric   void fixEndsAtEndOfFunction(MachineFunction &MF);
95fe6060f1SDimitry Andric   void cleanupFunctionData(MachineFunction &MF);
960b57cec5SDimitry Andric 
97fe6060f1SDimitry Andric   // For each BLOCK|LOOP|TRY, the corresponding END_(BLOCK|LOOP|TRY) or DELEGATE
98fe6060f1SDimitry Andric   // (in case of TRY).
990b57cec5SDimitry Andric   DenseMap<const MachineInstr *, MachineInstr *> BeginToEnd;
100fe6060f1SDimitry Andric   // For each END_(BLOCK|LOOP|TRY) or DELEGATE, the corresponding
101fe6060f1SDimitry Andric   // BLOCK|LOOP|TRY.
1020b57cec5SDimitry Andric   DenseMap<const MachineInstr *, MachineInstr *> EndToBegin;
1030b57cec5SDimitry Andric   // <TRY marker, EH pad> map
1040b57cec5SDimitry Andric   DenseMap<const MachineInstr *, MachineBasicBlock *> TryToEHPad;
1050b57cec5SDimitry Andric   // <EH pad, TRY marker> map
1060b57cec5SDimitry Andric   DenseMap<const MachineBasicBlock *, MachineInstr *> EHPadToTry;
1070b57cec5SDimitry Andric 
108fe6060f1SDimitry Andric   // We need an appendix block to place 'end_loop' or 'end_try' marker when the
109fe6060f1SDimitry Andric   // loop / exception bottom block is the last block in a function
1100b57cec5SDimitry Andric   MachineBasicBlock *AppendixBB = nullptr;
getAppendixBlock(MachineFunction & MF)1110b57cec5SDimitry Andric   MachineBasicBlock *getAppendixBlock(MachineFunction &MF) {
1120b57cec5SDimitry Andric     if (!AppendixBB) {
1130b57cec5SDimitry Andric       AppendixBB = MF.CreateMachineBasicBlock();
1140b57cec5SDimitry Andric       // Give it a fake predecessor so that AsmPrinter prints its label.
1150b57cec5SDimitry Andric       AppendixBB->addSuccessor(AppendixBB);
1160b57cec5SDimitry Andric       MF.push_back(AppendixBB);
1170b57cec5SDimitry Andric     }
1180b57cec5SDimitry Andric     return AppendixBB;
1190b57cec5SDimitry Andric   }
1200b57cec5SDimitry Andric 
121fe6060f1SDimitry Andric   // Before running rewriteDepthImmediates function, 'delegate' has a BB as its
122fe6060f1SDimitry Andric   // destination operand. getFakeCallerBlock() returns a fake BB that will be
123fe6060f1SDimitry Andric   // used for the operand when 'delegate' needs to rethrow to the caller. This
124fe6060f1SDimitry Andric   // will be rewritten as an immediate value that is the number of block depths
125fe6060f1SDimitry Andric   // + 1 in rewriteDepthImmediates, and this fake BB will be removed at the end
126fe6060f1SDimitry Andric   // of the pass.
127fe6060f1SDimitry Andric   MachineBasicBlock *FakeCallerBB = nullptr;
getFakeCallerBlock(MachineFunction & MF)128fe6060f1SDimitry Andric   MachineBasicBlock *getFakeCallerBlock(MachineFunction &MF) {
129fe6060f1SDimitry Andric     if (!FakeCallerBB)
130fe6060f1SDimitry Andric       FakeCallerBB = MF.CreateMachineBasicBlock();
131fe6060f1SDimitry Andric     return FakeCallerBB;
132fe6060f1SDimitry Andric   }
133fe6060f1SDimitry Andric 
1340b57cec5SDimitry Andric   // Helper functions to register / unregister scope information created by
1350b57cec5SDimitry Andric   // marker instructions.
1360b57cec5SDimitry Andric   void registerScope(MachineInstr *Begin, MachineInstr *End);
1370b57cec5SDimitry Andric   void registerTryScope(MachineInstr *Begin, MachineInstr *End,
1380b57cec5SDimitry Andric                         MachineBasicBlock *EHPad);
1390b57cec5SDimitry Andric   void unregisterScope(MachineInstr *Begin);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric public:
1420b57cec5SDimitry Andric   static char ID; // Pass identification, replacement for typeid
WebAssemblyCFGStackify()1430b57cec5SDimitry Andric   WebAssemblyCFGStackify() : MachineFunctionPass(ID) {}
~WebAssemblyCFGStackify()1440b57cec5SDimitry Andric   ~WebAssemblyCFGStackify() override { releaseMemory(); }
1450b57cec5SDimitry Andric   void releaseMemory() override;
1460b57cec5SDimitry Andric };
1470b57cec5SDimitry Andric } // end anonymous namespace
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric char WebAssemblyCFGStackify::ID = 0;
1500b57cec5SDimitry Andric INITIALIZE_PASS(WebAssemblyCFGStackify, DEBUG_TYPE,
1510b57cec5SDimitry Andric                 "Insert BLOCK/LOOP/TRY markers for WebAssembly scopes", false,
1520b57cec5SDimitry Andric                 false)
1530b57cec5SDimitry Andric 
createWebAssemblyCFGStackify()1540b57cec5SDimitry Andric FunctionPass *llvm::createWebAssemblyCFGStackify() {
1550b57cec5SDimitry Andric   return new WebAssemblyCFGStackify();
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric /// Test whether Pred has any terminators explicitly branching to MBB, as
1590b57cec5SDimitry Andric /// opposed to falling through. Note that it's possible (eg. in unoptimized
1600b57cec5SDimitry Andric /// code) for a branch instruction to both branch to a block and fallthrough
1610b57cec5SDimitry Andric /// to it, so we check the actual branch operands to see if there are any
1620b57cec5SDimitry Andric /// explicit mentions.
explicitlyBranchesTo(MachineBasicBlock * Pred,MachineBasicBlock * MBB)1630b57cec5SDimitry Andric static bool explicitlyBranchesTo(MachineBasicBlock *Pred,
1640b57cec5SDimitry Andric                                  MachineBasicBlock *MBB) {
1650b57cec5SDimitry Andric   for (MachineInstr &MI : Pred->terminators())
1660b57cec5SDimitry Andric     for (MachineOperand &MO : MI.explicit_operands())
1670b57cec5SDimitry Andric       if (MO.isMBB() && MO.getMBB() == MBB)
1680b57cec5SDimitry Andric         return true;
1690b57cec5SDimitry Andric   return false;
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric // Returns an iterator to the earliest position possible within the MBB,
1730b57cec5SDimitry Andric // satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet
1740b57cec5SDimitry Andric // contains instructions that should go before the marker, and AfterSet contains
1750b57cec5SDimitry Andric // ones that should go after the marker. In this function, AfterSet is only
176349cc55cSDimitry Andric // used for validation checking.
177e8d8bef9SDimitry Andric template <typename Container>
1780b57cec5SDimitry Andric static MachineBasicBlock::iterator
getEarliestInsertPos(MachineBasicBlock * MBB,const Container & BeforeSet,const Container & AfterSet)179e8d8bef9SDimitry Andric getEarliestInsertPos(MachineBasicBlock *MBB, const Container &BeforeSet,
180e8d8bef9SDimitry Andric                      const Container &AfterSet) {
1810b57cec5SDimitry Andric   auto InsertPos = MBB->end();
1820b57cec5SDimitry Andric   while (InsertPos != MBB->begin()) {
1830b57cec5SDimitry Andric     if (BeforeSet.count(&*std::prev(InsertPos))) {
1840b57cec5SDimitry Andric #ifndef NDEBUG
185349cc55cSDimitry Andric       // Validation check
1860b57cec5SDimitry Andric       for (auto Pos = InsertPos, E = MBB->begin(); Pos != E; --Pos)
1870b57cec5SDimitry Andric         assert(!AfterSet.count(&*std::prev(Pos)));
1880b57cec5SDimitry Andric #endif
1890b57cec5SDimitry Andric       break;
1900b57cec5SDimitry Andric     }
1910b57cec5SDimitry Andric     --InsertPos;
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric   return InsertPos;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric // Returns an iterator to the latest position possible within the MBB,
1970b57cec5SDimitry Andric // satisfying the restrictions given by BeforeSet and AfterSet. BeforeSet
1980b57cec5SDimitry Andric // contains instructions that should go before the marker, and AfterSet contains
1990b57cec5SDimitry Andric // ones that should go after the marker. In this function, BeforeSet is only
200349cc55cSDimitry Andric // used for validation checking.
201e8d8bef9SDimitry Andric template <typename Container>
2020b57cec5SDimitry Andric static MachineBasicBlock::iterator
getLatestInsertPos(MachineBasicBlock * MBB,const Container & BeforeSet,const Container & AfterSet)203e8d8bef9SDimitry Andric getLatestInsertPos(MachineBasicBlock *MBB, const Container &BeforeSet,
204e8d8bef9SDimitry Andric                    const Container &AfterSet) {
2050b57cec5SDimitry Andric   auto InsertPos = MBB->begin();
2060b57cec5SDimitry Andric   while (InsertPos != MBB->end()) {
2070b57cec5SDimitry Andric     if (AfterSet.count(&*InsertPos)) {
2080b57cec5SDimitry Andric #ifndef NDEBUG
209349cc55cSDimitry Andric       // Validation check
2100b57cec5SDimitry Andric       for (auto Pos = InsertPos, E = MBB->end(); Pos != E; ++Pos)
2110b57cec5SDimitry Andric         assert(!BeforeSet.count(&*Pos));
2120b57cec5SDimitry Andric #endif
2130b57cec5SDimitry Andric       break;
2140b57cec5SDimitry Andric     }
2150b57cec5SDimitry Andric     ++InsertPos;
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric   return InsertPos;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric 
registerScope(MachineInstr * Begin,MachineInstr * End)2200b57cec5SDimitry Andric void WebAssemblyCFGStackify::registerScope(MachineInstr *Begin,
2210b57cec5SDimitry Andric                                            MachineInstr *End) {
2220b57cec5SDimitry Andric   BeginToEnd[Begin] = End;
2230b57cec5SDimitry Andric   EndToBegin[End] = Begin;
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric 
226fe6060f1SDimitry Andric // When 'End' is not an 'end_try' but 'delegate, EHPad is nullptr.
registerTryScope(MachineInstr * Begin,MachineInstr * End,MachineBasicBlock * EHPad)2270b57cec5SDimitry Andric void WebAssemblyCFGStackify::registerTryScope(MachineInstr *Begin,
2280b57cec5SDimitry Andric                                               MachineInstr *End,
2290b57cec5SDimitry Andric                                               MachineBasicBlock *EHPad) {
2300b57cec5SDimitry Andric   registerScope(Begin, End);
2310b57cec5SDimitry Andric   TryToEHPad[Begin] = EHPad;
2320b57cec5SDimitry Andric   EHPadToTry[EHPad] = Begin;
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric 
unregisterScope(MachineInstr * Begin)2350b57cec5SDimitry Andric void WebAssemblyCFGStackify::unregisterScope(MachineInstr *Begin) {
2360b57cec5SDimitry Andric   assert(BeginToEnd.count(Begin));
2370b57cec5SDimitry Andric   MachineInstr *End = BeginToEnd[Begin];
2380b57cec5SDimitry Andric   assert(EndToBegin.count(End));
2390b57cec5SDimitry Andric   BeginToEnd.erase(Begin);
2400b57cec5SDimitry Andric   EndToBegin.erase(End);
2410b57cec5SDimitry Andric   MachineBasicBlock *EHPad = TryToEHPad.lookup(Begin);
2420b57cec5SDimitry Andric   if (EHPad) {
2430b57cec5SDimitry Andric     assert(EHPadToTry.count(EHPad));
2440b57cec5SDimitry Andric     TryToEHPad.erase(Begin);
2450b57cec5SDimitry Andric     EHPadToTry.erase(EHPad);
2460b57cec5SDimitry Andric   }
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric /// Insert a BLOCK marker for branches to MBB (if needed).
2500b57cec5SDimitry Andric // TODO Consider a more generalized way of handling block (and also loop and
2510b57cec5SDimitry Andric // try) signatures when we implement the multi-value proposal later.
placeBlockMarker(MachineBasicBlock & MBB)2520b57cec5SDimitry Andric void WebAssemblyCFGStackify::placeBlockMarker(MachineBasicBlock &MBB) {
2530b57cec5SDimitry Andric   assert(!MBB.isEHPad());
2540b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
2550b57cec5SDimitry Andric   auto &MDT = getAnalysis<MachineDominatorTree>();
2560b57cec5SDimitry Andric   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
2570b57cec5SDimitry Andric   const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric   // First compute the nearest common dominator of all forward non-fallthrough
2600b57cec5SDimitry Andric   // predecessors so that we minimize the time that the BLOCK is on the stack,
2610b57cec5SDimitry Andric   // which reduces overall stack height.
2620b57cec5SDimitry Andric   MachineBasicBlock *Header = nullptr;
2630b57cec5SDimitry Andric   bool IsBranchedTo = false;
2640b57cec5SDimitry Andric   int MBBNumber = MBB.getNumber();
2650b57cec5SDimitry Andric   for (MachineBasicBlock *Pred : MBB.predecessors()) {
2660b57cec5SDimitry Andric     if (Pred->getNumber() < MBBNumber) {
2670b57cec5SDimitry Andric       Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
268e8d8bef9SDimitry Andric       if (explicitlyBranchesTo(Pred, &MBB))
2690b57cec5SDimitry Andric         IsBranchedTo = true;
2700b57cec5SDimitry Andric     }
2710b57cec5SDimitry Andric   }
2720b57cec5SDimitry Andric   if (!Header)
2730b57cec5SDimitry Andric     return;
2740b57cec5SDimitry Andric   if (!IsBranchedTo)
2750b57cec5SDimitry Andric     return;
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors");
2780b57cec5SDimitry Andric   MachineBasicBlock *LayoutPred = MBB.getPrevNode();
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   // If the nearest common dominator is inside a more deeply nested context,
2810b57cec5SDimitry Andric   // walk out to the nearest scope which isn't more deeply nested.
2820b57cec5SDimitry Andric   for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
2830b57cec5SDimitry Andric     if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
2840b57cec5SDimitry Andric       if (ScopeTop->getNumber() > Header->getNumber()) {
2850b57cec5SDimitry Andric         // Skip over an intervening scope.
2860b57cec5SDimitry Andric         I = std::next(ScopeTop->getIterator());
2870b57cec5SDimitry Andric       } else {
2880b57cec5SDimitry Andric         // We found a scope level at an appropriate depth.
2890b57cec5SDimitry Andric         Header = ScopeTop;
2900b57cec5SDimitry Andric         break;
2910b57cec5SDimitry Andric       }
2920b57cec5SDimitry Andric     }
2930b57cec5SDimitry Andric   }
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric   // Decide where in Header to put the BLOCK.
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   // Instructions that should go before the BLOCK.
2980b57cec5SDimitry Andric   SmallPtrSet<const MachineInstr *, 4> BeforeSet;
2990b57cec5SDimitry Andric   // Instructions that should go after the BLOCK.
3000b57cec5SDimitry Andric   SmallPtrSet<const MachineInstr *, 4> AfterSet;
3010b57cec5SDimitry Andric   for (const auto &MI : *Header) {
3020b57cec5SDimitry Andric     // If there is a previously placed LOOP marker and the bottom block of the
3030b57cec5SDimitry Andric     // loop is above MBB, it should be after the BLOCK, because the loop is
3040b57cec5SDimitry Andric     // nested in this BLOCK. Otherwise it should be before the BLOCK.
3050b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::LOOP) {
3060b57cec5SDimitry Andric       auto *LoopBottom = BeginToEnd[&MI]->getParent()->getPrevNode();
3070b57cec5SDimitry Andric       if (MBB.getNumber() > LoopBottom->getNumber())
3080b57cec5SDimitry Andric         AfterSet.insert(&MI);
3090b57cec5SDimitry Andric #ifndef NDEBUG
3100b57cec5SDimitry Andric       else
3110b57cec5SDimitry Andric         BeforeSet.insert(&MI);
3120b57cec5SDimitry Andric #endif
3130b57cec5SDimitry Andric     }
3140b57cec5SDimitry Andric 
3155ffd83dbSDimitry Andric     // If there is a previously placed BLOCK/TRY marker and its corresponding
3165ffd83dbSDimitry Andric     // END marker is before the current BLOCK's END marker, that should be
3175ffd83dbSDimitry Andric     // placed after this BLOCK. Otherwise it should be placed before this BLOCK
3185ffd83dbSDimitry Andric     // marker.
3190b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::BLOCK ||
3205ffd83dbSDimitry Andric         MI.getOpcode() == WebAssembly::TRY) {
3215ffd83dbSDimitry Andric       if (BeginToEnd[&MI]->getParent()->getNumber() <= MBB.getNumber())
3220b57cec5SDimitry Andric         AfterSet.insert(&MI);
3235ffd83dbSDimitry Andric #ifndef NDEBUG
3245ffd83dbSDimitry Andric       else
3255ffd83dbSDimitry Andric         BeforeSet.insert(&MI);
3265ffd83dbSDimitry Andric #endif
3275ffd83dbSDimitry Andric     }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric #ifndef NDEBUG
3300b57cec5SDimitry Andric     // All END_(BLOCK|LOOP|TRY) markers should be before the BLOCK.
3310b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::END_BLOCK ||
3320b57cec5SDimitry Andric         MI.getOpcode() == WebAssembly::END_LOOP ||
3330b57cec5SDimitry Andric         MI.getOpcode() == WebAssembly::END_TRY)
3340b57cec5SDimitry Andric       BeforeSet.insert(&MI);
3350b57cec5SDimitry Andric #endif
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric     // Terminators should go after the BLOCK.
3380b57cec5SDimitry Andric     if (MI.isTerminator())
3390b57cec5SDimitry Andric       AfterSet.insert(&MI);
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   // Local expression tree should go after the BLOCK.
3430b57cec5SDimitry Andric   for (auto I = Header->getFirstTerminator(), E = Header->begin(); I != E;
3440b57cec5SDimitry Andric        --I) {
3450b57cec5SDimitry Andric     if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition())
3460b57cec5SDimitry Andric       continue;
3470b57cec5SDimitry Andric     if (WebAssembly::isChild(*std::prev(I), MFI))
3480b57cec5SDimitry Andric       AfterSet.insert(&*std::prev(I));
3490b57cec5SDimitry Andric     else
3500b57cec5SDimitry Andric       break;
3510b57cec5SDimitry Andric   }
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   // Add the BLOCK.
3548bcb0991SDimitry Andric   WebAssembly::BlockType ReturnType = WebAssembly::BlockType::Void;
3550b57cec5SDimitry Andric   auto InsertPos = getLatestInsertPos(Header, BeforeSet, AfterSet);
3560b57cec5SDimitry Andric   MachineInstr *Begin =
3570b57cec5SDimitry Andric       BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
3580b57cec5SDimitry Andric               TII.get(WebAssembly::BLOCK))
3590b57cec5SDimitry Andric           .addImm(int64_t(ReturnType));
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   // Decide where in Header to put the END_BLOCK.
3620b57cec5SDimitry Andric   BeforeSet.clear();
3630b57cec5SDimitry Andric   AfterSet.clear();
3640b57cec5SDimitry Andric   for (auto &MI : MBB) {
3650b57cec5SDimitry Andric #ifndef NDEBUG
3660b57cec5SDimitry Andric     // END_BLOCK should precede existing LOOP and TRY markers.
3670b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::LOOP ||
3680b57cec5SDimitry Andric         MI.getOpcode() == WebAssembly::TRY)
3690b57cec5SDimitry Andric       AfterSet.insert(&MI);
3700b57cec5SDimitry Andric #endif
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric     // If there is a previously placed END_LOOP marker and the header of the
3730b57cec5SDimitry Andric     // loop is above this block's header, the END_LOOP should be placed after
3740b57cec5SDimitry Andric     // the BLOCK, because the loop contains this block. Otherwise the END_LOOP
3750b57cec5SDimitry Andric     // should be placed before the BLOCK. The same for END_TRY.
3760b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::END_LOOP ||
3770b57cec5SDimitry Andric         MI.getOpcode() == WebAssembly::END_TRY) {
3780b57cec5SDimitry Andric       if (EndToBegin[&MI]->getParent()->getNumber() >= Header->getNumber())
3790b57cec5SDimitry Andric         BeforeSet.insert(&MI);
3800b57cec5SDimitry Andric #ifndef NDEBUG
3810b57cec5SDimitry Andric       else
3820b57cec5SDimitry Andric         AfterSet.insert(&MI);
3830b57cec5SDimitry Andric #endif
3840b57cec5SDimitry Andric     }
3850b57cec5SDimitry Andric   }
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric   // Mark the end of the block.
3880b57cec5SDimitry Andric   InsertPos = getEarliestInsertPos(&MBB, BeforeSet, AfterSet);
3890b57cec5SDimitry Andric   MachineInstr *End = BuildMI(MBB, InsertPos, MBB.findPrevDebugLoc(InsertPos),
3900b57cec5SDimitry Andric                               TII.get(WebAssembly::END_BLOCK));
3910b57cec5SDimitry Andric   registerScope(Begin, End);
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   // Track the farthest-spanning scope that ends at this point.
394e8d8bef9SDimitry Andric   updateScopeTops(Header, &MBB);
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric /// Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
placeLoopMarker(MachineBasicBlock & MBB)3980b57cec5SDimitry Andric void WebAssemblyCFGStackify::placeLoopMarker(MachineBasicBlock &MBB) {
3990b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
4000b57cec5SDimitry Andric   const auto &MLI = getAnalysis<MachineLoopInfo>();
401e8d8bef9SDimitry Andric   const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
402e8d8bef9SDimitry Andric   SortRegionInfo SRI(MLI, WEI);
4030b57cec5SDimitry Andric   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   MachineLoop *Loop = MLI.getLoopFor(&MBB);
4060b57cec5SDimitry Andric   if (!Loop || Loop->getHeader() != &MBB)
4070b57cec5SDimitry Andric     return;
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   // The operand of a LOOP is the first block after the loop. If the loop is the
4100b57cec5SDimitry Andric   // bottom of the function, insert a dummy block at the end.
411e8d8bef9SDimitry Andric   MachineBasicBlock *Bottom = SRI.getBottom(Loop);
4120b57cec5SDimitry Andric   auto Iter = std::next(Bottom->getIterator());
4130b57cec5SDimitry Andric   if (Iter == MF.end()) {
4140b57cec5SDimitry Andric     getAppendixBlock(MF);
4150b57cec5SDimitry Andric     Iter = std::next(Bottom->getIterator());
4160b57cec5SDimitry Andric   }
4170b57cec5SDimitry Andric   MachineBasicBlock *AfterLoop = &*Iter;
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric   // Decide where in Header to put the LOOP.
4200b57cec5SDimitry Andric   SmallPtrSet<const MachineInstr *, 4> BeforeSet;
4210b57cec5SDimitry Andric   SmallPtrSet<const MachineInstr *, 4> AfterSet;
4220b57cec5SDimitry Andric   for (const auto &MI : MBB) {
4230b57cec5SDimitry Andric     // LOOP marker should be after any existing loop that ends here. Otherwise
4240b57cec5SDimitry Andric     // we assume the instruction belongs to the loop.
4250b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::END_LOOP)
4260b57cec5SDimitry Andric       BeforeSet.insert(&MI);
4270b57cec5SDimitry Andric #ifndef NDEBUG
4280b57cec5SDimitry Andric     else
4290b57cec5SDimitry Andric       AfterSet.insert(&MI);
4300b57cec5SDimitry Andric #endif
4310b57cec5SDimitry Andric   }
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric   // Mark the beginning of the loop.
4340b57cec5SDimitry Andric   auto InsertPos = getEarliestInsertPos(&MBB, BeforeSet, AfterSet);
4350b57cec5SDimitry Andric   MachineInstr *Begin = BuildMI(MBB, InsertPos, MBB.findDebugLoc(InsertPos),
4360b57cec5SDimitry Andric                                 TII.get(WebAssembly::LOOP))
4378bcb0991SDimitry Andric                             .addImm(int64_t(WebAssembly::BlockType::Void));
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric   // Decide where in Header to put the END_LOOP.
4400b57cec5SDimitry Andric   BeforeSet.clear();
4410b57cec5SDimitry Andric   AfterSet.clear();
4420b57cec5SDimitry Andric #ifndef NDEBUG
4430b57cec5SDimitry Andric   for (const auto &MI : MBB)
4440b57cec5SDimitry Andric     // Existing END_LOOP markers belong to parent loops of this loop
4450b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::END_LOOP)
4460b57cec5SDimitry Andric       AfterSet.insert(&MI);
4470b57cec5SDimitry Andric #endif
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   // Mark the end of the loop (using arbitrary debug location that branched to
4500b57cec5SDimitry Andric   // the loop end as its location).
4510b57cec5SDimitry Andric   InsertPos = getEarliestInsertPos(AfterLoop, BeforeSet, AfterSet);
4520b57cec5SDimitry Andric   DebugLoc EndDL = AfterLoop->pred_empty()
4530b57cec5SDimitry Andric                        ? DebugLoc()
4540b57cec5SDimitry Andric                        : (*AfterLoop->pred_rbegin())->findBranchDebugLoc();
4550b57cec5SDimitry Andric   MachineInstr *End =
4560b57cec5SDimitry Andric       BuildMI(*AfterLoop, InsertPos, EndDL, TII.get(WebAssembly::END_LOOP));
4570b57cec5SDimitry Andric   registerScope(Begin, End);
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   assert((!ScopeTops[AfterLoop->getNumber()] ||
4600b57cec5SDimitry Andric           ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) &&
4610b57cec5SDimitry Andric          "With block sorting the outermost loop for a block should be first.");
462e8d8bef9SDimitry Andric   updateScopeTops(&MBB, AfterLoop);
4630b57cec5SDimitry Andric }
4640b57cec5SDimitry Andric 
placeTryMarker(MachineBasicBlock & MBB)4650b57cec5SDimitry Andric void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) {
4660b57cec5SDimitry Andric   assert(MBB.isEHPad());
4670b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
4680b57cec5SDimitry Andric   auto &MDT = getAnalysis<MachineDominatorTree>();
4690b57cec5SDimitry Andric   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
470e8d8bef9SDimitry Andric   const auto &MLI = getAnalysis<MachineLoopInfo>();
4710b57cec5SDimitry Andric   const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
472e8d8bef9SDimitry Andric   SortRegionInfo SRI(MLI, WEI);
4730b57cec5SDimitry Andric   const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   // Compute the nearest common dominator of all unwind predecessors
4760b57cec5SDimitry Andric   MachineBasicBlock *Header = nullptr;
4770b57cec5SDimitry Andric   int MBBNumber = MBB.getNumber();
4780b57cec5SDimitry Andric   for (auto *Pred : MBB.predecessors()) {
4790b57cec5SDimitry Andric     if (Pred->getNumber() < MBBNumber) {
4800b57cec5SDimitry Andric       Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
4810b57cec5SDimitry Andric       assert(!explicitlyBranchesTo(Pred, &MBB) &&
4820b57cec5SDimitry Andric              "Explicit branch to an EH pad!");
4830b57cec5SDimitry Andric     }
4840b57cec5SDimitry Andric   }
4850b57cec5SDimitry Andric   if (!Header)
4860b57cec5SDimitry Andric     return;
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric   // If this try is at the bottom of the function, insert a dummy block at the
4890b57cec5SDimitry Andric   // end.
4900b57cec5SDimitry Andric   WebAssemblyException *WE = WEI.getExceptionFor(&MBB);
4910b57cec5SDimitry Andric   assert(WE);
492e8d8bef9SDimitry Andric   MachineBasicBlock *Bottom = SRI.getBottom(WE);
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric   auto Iter = std::next(Bottom->getIterator());
4950b57cec5SDimitry Andric   if (Iter == MF.end()) {
4960b57cec5SDimitry Andric     getAppendixBlock(MF);
4970b57cec5SDimitry Andric     Iter = std::next(Bottom->getIterator());
4980b57cec5SDimitry Andric   }
4990b57cec5SDimitry Andric   MachineBasicBlock *Cont = &*Iter;
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   assert(Cont != &MF.front());
5020b57cec5SDimitry Andric   MachineBasicBlock *LayoutPred = Cont->getPrevNode();
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   // If the nearest common dominator is inside a more deeply nested context,
5050b57cec5SDimitry Andric   // walk out to the nearest scope which isn't more deeply nested.
5060b57cec5SDimitry Andric   for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
5070b57cec5SDimitry Andric     if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
5080b57cec5SDimitry Andric       if (ScopeTop->getNumber() > Header->getNumber()) {
5090b57cec5SDimitry Andric         // Skip over an intervening scope.
5100b57cec5SDimitry Andric         I = std::next(ScopeTop->getIterator());
5110b57cec5SDimitry Andric       } else {
5120b57cec5SDimitry Andric         // We found a scope level at an appropriate depth.
5130b57cec5SDimitry Andric         Header = ScopeTop;
5140b57cec5SDimitry Andric         break;
5150b57cec5SDimitry Andric       }
5160b57cec5SDimitry Andric     }
5170b57cec5SDimitry Andric   }
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   // Decide where in Header to put the TRY.
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric   // Instructions that should go before the TRY.
5220b57cec5SDimitry Andric   SmallPtrSet<const MachineInstr *, 4> BeforeSet;
5230b57cec5SDimitry Andric   // Instructions that should go after the TRY.
5240b57cec5SDimitry Andric   SmallPtrSet<const MachineInstr *, 4> AfterSet;
5250b57cec5SDimitry Andric   for (const auto &MI : *Header) {
5260b57cec5SDimitry Andric     // If there is a previously placed LOOP marker and the bottom block of the
5270b57cec5SDimitry Andric     // loop is above MBB, it should be after the TRY, because the loop is nested
5280b57cec5SDimitry Andric     // in this TRY. Otherwise it should be before the TRY.
5290b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::LOOP) {
5300b57cec5SDimitry Andric       auto *LoopBottom = BeginToEnd[&MI]->getParent()->getPrevNode();
5310b57cec5SDimitry Andric       if (MBB.getNumber() > LoopBottom->getNumber())
5320b57cec5SDimitry Andric         AfterSet.insert(&MI);
5330b57cec5SDimitry Andric #ifndef NDEBUG
5340b57cec5SDimitry Andric       else
5350b57cec5SDimitry Andric         BeforeSet.insert(&MI);
5360b57cec5SDimitry Andric #endif
5370b57cec5SDimitry Andric     }
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric     // All previously inserted BLOCK/TRY markers should be after the TRY because
5400b57cec5SDimitry Andric     // they are all nested trys.
5410b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::BLOCK ||
5420b57cec5SDimitry Andric         MI.getOpcode() == WebAssembly::TRY)
5430b57cec5SDimitry Andric       AfterSet.insert(&MI);
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric #ifndef NDEBUG
5460b57cec5SDimitry Andric     // All END_(BLOCK/LOOP/TRY) markers should be before the TRY.
5470b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::END_BLOCK ||
5480b57cec5SDimitry Andric         MI.getOpcode() == WebAssembly::END_LOOP ||
5490b57cec5SDimitry Andric         MI.getOpcode() == WebAssembly::END_TRY)
5500b57cec5SDimitry Andric       BeforeSet.insert(&MI);
5510b57cec5SDimitry Andric #endif
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric     // Terminators should go after the TRY.
5540b57cec5SDimitry Andric     if (MI.isTerminator())
5550b57cec5SDimitry Andric       AfterSet.insert(&MI);
5560b57cec5SDimitry Andric   }
5570b57cec5SDimitry Andric 
5588bcb0991SDimitry Andric   // If Header unwinds to MBB (= Header contains 'invoke'), the try block should
5598bcb0991SDimitry Andric   // contain the call within it. So the call should go after the TRY. The
5608bcb0991SDimitry Andric   // exception is when the header's terminator is a rethrow instruction, in
5618bcb0991SDimitry Andric   // which case that instruction, not a call instruction before it, is gonna
5628bcb0991SDimitry Andric   // throw.
5638bcb0991SDimitry Andric   MachineInstr *ThrowingCall = nullptr;
5648bcb0991SDimitry Andric   if (MBB.isPredecessor(Header)) {
5658bcb0991SDimitry Andric     auto TermPos = Header->getFirstTerminator();
5668bcb0991SDimitry Andric     if (TermPos == Header->end() ||
5678bcb0991SDimitry Andric         TermPos->getOpcode() != WebAssembly::RETHROW) {
5688bcb0991SDimitry Andric       for (auto &MI : reverse(*Header)) {
5698bcb0991SDimitry Andric         if (MI.isCall()) {
5708bcb0991SDimitry Andric           AfterSet.insert(&MI);
5718bcb0991SDimitry Andric           ThrowingCall = &MI;
5728bcb0991SDimitry Andric           // Possibly throwing calls are usually wrapped by EH_LABEL
5738bcb0991SDimitry Andric           // instructions. We don't want to split them and the call.
5748bcb0991SDimitry Andric           if (MI.getIterator() != Header->begin() &&
5758bcb0991SDimitry Andric               std::prev(MI.getIterator())->isEHLabel()) {
5768bcb0991SDimitry Andric             AfterSet.insert(&*std::prev(MI.getIterator()));
5778bcb0991SDimitry Andric             ThrowingCall = &*std::prev(MI.getIterator());
5788bcb0991SDimitry Andric           }
5798bcb0991SDimitry Andric           break;
5808bcb0991SDimitry Andric         }
5818bcb0991SDimitry Andric       }
5828bcb0991SDimitry Andric     }
5838bcb0991SDimitry Andric   }
5848bcb0991SDimitry Andric 
5850b57cec5SDimitry Andric   // Local expression tree should go after the TRY.
5868bcb0991SDimitry Andric   // For BLOCK placement, we start the search from the previous instruction of a
5878bcb0991SDimitry Andric   // BB's terminator, but in TRY's case, we should start from the previous
5888bcb0991SDimitry Andric   // instruction of a call that can throw, or a EH_LABEL that precedes the call,
5898bcb0991SDimitry Andric   // because the return values of the call's previous instructions can be
5908bcb0991SDimitry Andric   // stackified and consumed by the throwing call.
5918bcb0991SDimitry Andric   auto SearchStartPt = ThrowingCall ? MachineBasicBlock::iterator(ThrowingCall)
5928bcb0991SDimitry Andric                                     : Header->getFirstTerminator();
5938bcb0991SDimitry Andric   for (auto I = SearchStartPt, E = Header->begin(); I != E; --I) {
5940b57cec5SDimitry Andric     if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition())
5950b57cec5SDimitry Andric       continue;
5960b57cec5SDimitry Andric     if (WebAssembly::isChild(*std::prev(I), MFI))
5970b57cec5SDimitry Andric       AfterSet.insert(&*std::prev(I));
5980b57cec5SDimitry Andric     else
5990b57cec5SDimitry Andric       break;
6000b57cec5SDimitry Andric   }
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric   // Add the TRY.
6030b57cec5SDimitry Andric   auto InsertPos = getLatestInsertPos(Header, BeforeSet, AfterSet);
6040b57cec5SDimitry Andric   MachineInstr *Begin =
6050b57cec5SDimitry Andric       BuildMI(*Header, InsertPos, Header->findDebugLoc(InsertPos),
6060b57cec5SDimitry Andric               TII.get(WebAssembly::TRY))
6078bcb0991SDimitry Andric           .addImm(int64_t(WebAssembly::BlockType::Void));
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric   // Decide where in Header to put the END_TRY.
6100b57cec5SDimitry Andric   BeforeSet.clear();
6110b57cec5SDimitry Andric   AfterSet.clear();
6120b57cec5SDimitry Andric   for (const auto &MI : *Cont) {
6130b57cec5SDimitry Andric #ifndef NDEBUG
6140b57cec5SDimitry Andric     // END_TRY should precede existing LOOP and BLOCK markers.
6150b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::LOOP ||
6160b57cec5SDimitry Andric         MI.getOpcode() == WebAssembly::BLOCK)
6170b57cec5SDimitry Andric       AfterSet.insert(&MI);
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric     // All END_TRY markers placed earlier belong to exceptions that contains
6200b57cec5SDimitry Andric     // this one.
6210b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::END_TRY)
6220b57cec5SDimitry Andric       AfterSet.insert(&MI);
6230b57cec5SDimitry Andric #endif
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric     // If there is a previously placed END_LOOP marker and its header is after
6260b57cec5SDimitry Andric     // where TRY marker is, this loop is contained within the 'catch' part, so
6270b57cec5SDimitry Andric     // the END_TRY marker should go after that. Otherwise, the whole try-catch
6280b57cec5SDimitry Andric     // is contained within this loop, so the END_TRY should go before that.
6290b57cec5SDimitry Andric     if (MI.getOpcode() == WebAssembly::END_LOOP) {
6300b57cec5SDimitry Andric       // For a LOOP to be after TRY, LOOP's BB should be after TRY's BB; if they
6310b57cec5SDimitry Andric       // are in the same BB, LOOP is always before TRY.
6320b57cec5SDimitry Andric       if (EndToBegin[&MI]->getParent()->getNumber() > Header->getNumber())
6330b57cec5SDimitry Andric         BeforeSet.insert(&MI);
6340b57cec5SDimitry Andric #ifndef NDEBUG
6350b57cec5SDimitry Andric       else
6360b57cec5SDimitry Andric         AfterSet.insert(&MI);
6370b57cec5SDimitry Andric #endif
6380b57cec5SDimitry Andric     }
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric     // It is not possible for an END_BLOCK to be already in this block.
6410b57cec5SDimitry Andric   }
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric   // Mark the end of the TRY.
6440b57cec5SDimitry Andric   InsertPos = getEarliestInsertPos(Cont, BeforeSet, AfterSet);
6450b57cec5SDimitry Andric   MachineInstr *End =
6460b57cec5SDimitry Andric       BuildMI(*Cont, InsertPos, Bottom->findBranchDebugLoc(),
6470b57cec5SDimitry Andric               TII.get(WebAssembly::END_TRY));
6480b57cec5SDimitry Andric   registerTryScope(Begin, End, &MBB);
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric   // Track the farthest-spanning scope that ends at this point. We create two
6510b57cec5SDimitry Andric   // mappings: (BB with 'end_try' -> BB with 'try') and (BB with 'catch' -> BB
6520b57cec5SDimitry Andric   // with 'try'). We need to create 'catch' -> 'try' mapping here too because
6530b57cec5SDimitry Andric   // markers should not span across 'catch'. For example, this should not
6540b57cec5SDimitry Andric   // happen:
6550b57cec5SDimitry Andric   //
6560b57cec5SDimitry Andric   // try
6570b57cec5SDimitry Andric   //   block     --|  (X)
6580b57cec5SDimitry Andric   // catch         |
6590b57cec5SDimitry Andric   //   end_block --|
6600b57cec5SDimitry Andric   // end_try
661e8d8bef9SDimitry Andric   for (auto *End : {&MBB, Cont})
662e8d8bef9SDimitry Andric     updateScopeTops(Header, End);
6630b57cec5SDimitry Andric }
6640b57cec5SDimitry Andric 
removeUnnecessaryInstrs(MachineFunction & MF)6650b57cec5SDimitry Andric void WebAssemblyCFGStackify::removeUnnecessaryInstrs(MachineFunction &MF) {
6660b57cec5SDimitry Andric   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric   // When there is an unconditional branch right before a catch instruction and
6690b57cec5SDimitry Andric   // it branches to the end of end_try marker, we don't need the branch, because
6705f757f3fSDimitry Andric   // if there is no exception, the control flow transfers to that point anyway.
6710b57cec5SDimitry Andric   // bb0:
6720b57cec5SDimitry Andric   //   try
6730b57cec5SDimitry Andric   //     ...
6740b57cec5SDimitry Andric   //     br bb2      <- Not necessary
675e8d8bef9SDimitry Andric   // bb1 (ehpad):
6760b57cec5SDimitry Andric   //   catch
6770b57cec5SDimitry Andric   //     ...
678e8d8bef9SDimitry Andric   // bb2:            <- Continuation BB
6790b57cec5SDimitry Andric   //   end
680e8d8bef9SDimitry Andric   //
681e8d8bef9SDimitry Andric   // A more involved case: When the BB where 'end' is located is an another EH
682e8d8bef9SDimitry Andric   // pad, the Cont (= continuation) BB is that EH pad's 'end' BB. For example,
683e8d8bef9SDimitry Andric   // bb0:
684e8d8bef9SDimitry Andric   //   try
685e8d8bef9SDimitry Andric   //     try
686e8d8bef9SDimitry Andric   //       ...
687e8d8bef9SDimitry Andric   //       br bb3      <- Not necessary
688e8d8bef9SDimitry Andric   // bb1 (ehpad):
689e8d8bef9SDimitry Andric   //     catch
690e8d8bef9SDimitry Andric   // bb2 (ehpad):
691e8d8bef9SDimitry Andric   //     end
692e8d8bef9SDimitry Andric   //   catch
693e8d8bef9SDimitry Andric   //     ...
694e8d8bef9SDimitry Andric   // bb3:            <- Continuation BB
695e8d8bef9SDimitry Andric   //   end
696e8d8bef9SDimitry Andric   //
697e8d8bef9SDimitry Andric   // When the EH pad at hand is bb1, its matching end_try is in bb2. But it is
698e8d8bef9SDimitry Andric   // another EH pad, so bb0's continuation BB becomes bb3. So 'br bb3' in the
699e8d8bef9SDimitry Andric   // code can be deleted. This is why we run 'while' until 'Cont' is not an EH
700e8d8bef9SDimitry Andric   // pad.
7010b57cec5SDimitry Andric   for (auto &MBB : MF) {
7020b57cec5SDimitry Andric     if (!MBB.isEHPad())
7030b57cec5SDimitry Andric       continue;
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric     MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
7060b57cec5SDimitry Andric     SmallVector<MachineOperand, 4> Cond;
7070b57cec5SDimitry Andric     MachineBasicBlock *EHPadLayoutPred = MBB.getPrevNode();
708e8d8bef9SDimitry Andric 
709e8d8bef9SDimitry Andric     MachineBasicBlock *Cont = &MBB;
710e8d8bef9SDimitry Andric     while (Cont->isEHPad()) {
711e8d8bef9SDimitry Andric       MachineInstr *Try = EHPadToTry[Cont];
712e8d8bef9SDimitry Andric       MachineInstr *EndTry = BeginToEnd[Try];
713fe6060f1SDimitry Andric       // We started from an EH pad, so the end marker cannot be a delegate
714fe6060f1SDimitry Andric       assert(EndTry->getOpcode() != WebAssembly::DELEGATE);
715e8d8bef9SDimitry Andric       Cont = EndTry->getParent();
716e8d8bef9SDimitry Andric     }
717e8d8bef9SDimitry Andric 
7180b57cec5SDimitry Andric     bool Analyzable = !TII.analyzeBranch(*EHPadLayoutPred, TBB, FBB, Cond);
7195ffd83dbSDimitry Andric     // This condition means either
7205ffd83dbSDimitry Andric     // 1. This BB ends with a single unconditional branch whose destinaion is
7215ffd83dbSDimitry Andric     //    Cont.
7225ffd83dbSDimitry Andric     // 2. This BB ends with a conditional branch followed by an unconditional
7235ffd83dbSDimitry Andric     //    branch, and the unconditional branch's destination is Cont.
7245ffd83dbSDimitry Andric     // In both cases, we want to remove the last (= unconditional) branch.
7250b57cec5SDimitry Andric     if (Analyzable && ((Cond.empty() && TBB && TBB == Cont) ||
7265ffd83dbSDimitry Andric                        (!Cond.empty() && FBB && FBB == Cont))) {
7275ffd83dbSDimitry Andric       bool ErasedUncondBr = false;
7285ffd83dbSDimitry Andric       (void)ErasedUncondBr;
7295ffd83dbSDimitry Andric       for (auto I = EHPadLayoutPred->end(), E = EHPadLayoutPred->begin();
7305ffd83dbSDimitry Andric            I != E; --I) {
7315ffd83dbSDimitry Andric         auto PrevI = std::prev(I);
7325ffd83dbSDimitry Andric         if (PrevI->isTerminator()) {
7335ffd83dbSDimitry Andric           assert(PrevI->getOpcode() == WebAssembly::BR);
7345ffd83dbSDimitry Andric           PrevI->eraseFromParent();
7355ffd83dbSDimitry Andric           ErasedUncondBr = true;
7365ffd83dbSDimitry Andric           break;
7375ffd83dbSDimitry Andric         }
7385ffd83dbSDimitry Andric       }
7395ffd83dbSDimitry Andric       assert(ErasedUncondBr && "Unconditional branch not erased!");
7405ffd83dbSDimitry Andric     }
7410b57cec5SDimitry Andric   }
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric   // When there are block / end_block markers that overlap with try / end_try
7440b57cec5SDimitry Andric   // markers, and the block and try markers' return types are the same, the
7450b57cec5SDimitry Andric   // block /end_block markers are not necessary, because try / end_try markers
7460b57cec5SDimitry Andric   // also can serve as boundaries for branches.
7470b57cec5SDimitry Andric   // block         <- Not necessary
7480b57cec5SDimitry Andric   //   try
7490b57cec5SDimitry Andric   //     ...
7500b57cec5SDimitry Andric   //   catch
7510b57cec5SDimitry Andric   //     ...
7520b57cec5SDimitry Andric   //   end
7530b57cec5SDimitry Andric   // end           <- Not necessary
7540b57cec5SDimitry Andric   SmallVector<MachineInstr *, 32> ToDelete;
7550b57cec5SDimitry Andric   for (auto &MBB : MF) {
7560b57cec5SDimitry Andric     for (auto &MI : MBB) {
7570b57cec5SDimitry Andric       if (MI.getOpcode() != WebAssembly::TRY)
7580b57cec5SDimitry Andric         continue;
7590b57cec5SDimitry Andric       MachineInstr *Try = &MI, *EndTry = BeginToEnd[Try];
760fe6060f1SDimitry Andric       if (EndTry->getOpcode() == WebAssembly::DELEGATE)
761fe6060f1SDimitry Andric         continue;
762fe6060f1SDimitry Andric 
7630b57cec5SDimitry Andric       MachineBasicBlock *TryBB = Try->getParent();
7640b57cec5SDimitry Andric       MachineBasicBlock *Cont = EndTry->getParent();
7650b57cec5SDimitry Andric       int64_t RetType = Try->getOperand(0).getImm();
7660b57cec5SDimitry Andric       for (auto B = Try->getIterator(), E = std::next(EndTry->getIterator());
7670b57cec5SDimitry Andric            B != TryBB->begin() && E != Cont->end() &&
7680b57cec5SDimitry Andric            std::prev(B)->getOpcode() == WebAssembly::BLOCK &&
7690b57cec5SDimitry Andric            E->getOpcode() == WebAssembly::END_BLOCK &&
7700b57cec5SDimitry Andric            std::prev(B)->getOperand(0).getImm() == RetType;
7710b57cec5SDimitry Andric            --B, ++E) {
7720b57cec5SDimitry Andric         ToDelete.push_back(&*std::prev(B));
7730b57cec5SDimitry Andric         ToDelete.push_back(&*E);
7740b57cec5SDimitry Andric       }
7750b57cec5SDimitry Andric     }
7760b57cec5SDimitry Andric   }
7770b57cec5SDimitry Andric   for (auto *MI : ToDelete) {
7780b57cec5SDimitry Andric     if (MI->getOpcode() == WebAssembly::BLOCK)
7790b57cec5SDimitry Andric       unregisterScope(MI);
7800b57cec5SDimitry Andric     MI->eraseFromParent();
7810b57cec5SDimitry Andric   }
7820b57cec5SDimitry Andric }
7830b57cec5SDimitry Andric 
7848bcb0991SDimitry Andric // When MBB is split into MBB and Split, we should unstackify defs in MBB that
7858bcb0991SDimitry Andric // have their uses in Split.
unstackifyVRegsUsedInSplitBB(MachineBasicBlock & MBB,MachineBasicBlock & Split)786fe6060f1SDimitry Andric static void unstackifyVRegsUsedInSplitBB(MachineBasicBlock &MBB,
787fe6060f1SDimitry Andric                                          MachineBasicBlock &Split) {
788e8d8bef9SDimitry Andric   MachineFunction &MF = *MBB.getParent();
789e8d8bef9SDimitry Andric   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
790e8d8bef9SDimitry Andric   auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
791e8d8bef9SDimitry Andric   auto &MRI = MF.getRegInfo();
792e8d8bef9SDimitry Andric 
7938bcb0991SDimitry Andric   for (auto &MI : Split) {
7948bcb0991SDimitry Andric     for (auto &MO : MI.explicit_uses()) {
795bdd1243dSDimitry Andric       if (!MO.isReg() || MO.getReg().isPhysical())
7968bcb0991SDimitry Andric         continue;
7978bcb0991SDimitry Andric       if (MachineInstr *Def = MRI.getUniqueVRegDef(MO.getReg()))
7988bcb0991SDimitry Andric         if (Def->getParent() == &MBB)
7998bcb0991SDimitry Andric           MFI.unstackifyVReg(MO.getReg());
8008bcb0991SDimitry Andric     }
8018bcb0991SDimitry Andric   }
8025ffd83dbSDimitry Andric 
8035ffd83dbSDimitry Andric   // In RegStackify, when a register definition is used multiple times,
8045ffd83dbSDimitry Andric   //    Reg = INST ...
8055ffd83dbSDimitry Andric   //    INST ..., Reg, ...
8065ffd83dbSDimitry Andric   //    INST ..., Reg, ...
8075ffd83dbSDimitry Andric   //    INST ..., Reg, ...
8085ffd83dbSDimitry Andric   //
8095ffd83dbSDimitry Andric   // we introduce a TEE, which has the following form:
8105ffd83dbSDimitry Andric   //    DefReg = INST ...
8115ffd83dbSDimitry Andric   //    TeeReg, Reg = TEE_... DefReg
8125ffd83dbSDimitry Andric   //    INST ..., TeeReg, ...
8135ffd83dbSDimitry Andric   //    INST ..., Reg, ...
8145ffd83dbSDimitry Andric   //    INST ..., Reg, ...
8155ffd83dbSDimitry Andric   // with DefReg and TeeReg stackified but Reg not stackified.
8165ffd83dbSDimitry Andric   //
8175ffd83dbSDimitry Andric   // But the invariant that TeeReg should be stackified can be violated while we
8185ffd83dbSDimitry Andric   // unstackify registers in the split BB above. In this case, we convert TEEs
8195ffd83dbSDimitry Andric   // into two COPYs. This COPY will be eventually eliminated in ExplicitLocals.
8205ffd83dbSDimitry Andric   //    DefReg = INST ...
8215ffd83dbSDimitry Andric   //    TeeReg = COPY DefReg
8225ffd83dbSDimitry Andric   //    Reg = COPY DefReg
8235ffd83dbSDimitry Andric   //    INST ..., TeeReg, ...
8245ffd83dbSDimitry Andric   //    INST ..., Reg, ...
8255ffd83dbSDimitry Andric   //    INST ..., Reg, ...
826349cc55cSDimitry Andric   for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
8275ffd83dbSDimitry Andric     if (!WebAssembly::isTee(MI.getOpcode()))
8285ffd83dbSDimitry Andric       continue;
8295ffd83dbSDimitry Andric     Register TeeReg = MI.getOperand(0).getReg();
8305ffd83dbSDimitry Andric     Register Reg = MI.getOperand(1).getReg();
8315ffd83dbSDimitry Andric     Register DefReg = MI.getOperand(2).getReg();
8325ffd83dbSDimitry Andric     if (!MFI.isVRegStackified(TeeReg)) {
8335ffd83dbSDimitry Andric       // Now we are not using TEE anymore, so unstackify DefReg too
8345ffd83dbSDimitry Andric       MFI.unstackifyVReg(DefReg);
835753f127fSDimitry Andric       unsigned CopyOpc =
836753f127fSDimitry Andric           WebAssembly::getCopyOpcodeForRegClass(MRI.getRegClass(DefReg));
8375ffd83dbSDimitry Andric       BuildMI(MBB, &MI, MI.getDebugLoc(), TII.get(CopyOpc), TeeReg)
8385ffd83dbSDimitry Andric           .addReg(DefReg);
8395ffd83dbSDimitry Andric       BuildMI(MBB, &MI, MI.getDebugLoc(), TII.get(CopyOpc), Reg).addReg(DefReg);
8405ffd83dbSDimitry Andric       MI.eraseFromParent();
8415ffd83dbSDimitry Andric     }
8425ffd83dbSDimitry Andric   }
8438bcb0991SDimitry Andric }
8448bcb0991SDimitry Andric 
845fe6060f1SDimitry Andric // Wrap the given range of instruction with try-delegate. RangeBegin and
846fe6060f1SDimitry Andric // RangeEnd are inclusive.
addTryDelegate(MachineInstr * RangeBegin,MachineInstr * RangeEnd,MachineBasicBlock * DelegateDest)847fe6060f1SDimitry Andric void WebAssemblyCFGStackify::addTryDelegate(MachineInstr *RangeBegin,
848fe6060f1SDimitry Andric                                             MachineInstr *RangeEnd,
849fe6060f1SDimitry Andric                                             MachineBasicBlock *DelegateDest) {
850fe6060f1SDimitry Andric   auto *BeginBB = RangeBegin->getParent();
851fe6060f1SDimitry Andric   auto *EndBB = RangeEnd->getParent();
852fe6060f1SDimitry Andric   MachineFunction &MF = *BeginBB->getParent();
853fe6060f1SDimitry Andric   const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
854fe6060f1SDimitry Andric   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
855fe6060f1SDimitry Andric 
856fe6060f1SDimitry Andric   // Local expression tree before the first call of this range should go
857fe6060f1SDimitry Andric   // after the nested TRY.
858fe6060f1SDimitry Andric   SmallPtrSet<const MachineInstr *, 4> AfterSet;
859fe6060f1SDimitry Andric   AfterSet.insert(RangeBegin);
860fe6060f1SDimitry Andric   for (auto I = MachineBasicBlock::iterator(RangeBegin), E = BeginBB->begin();
861fe6060f1SDimitry Andric        I != E; --I) {
862fe6060f1SDimitry Andric     if (std::prev(I)->isDebugInstr() || std::prev(I)->isPosition())
863fe6060f1SDimitry Andric       continue;
864fe6060f1SDimitry Andric     if (WebAssembly::isChild(*std::prev(I), MFI))
865fe6060f1SDimitry Andric       AfterSet.insert(&*std::prev(I));
866fe6060f1SDimitry Andric     else
867fe6060f1SDimitry Andric       break;
8680b57cec5SDimitry Andric   }
8690b57cec5SDimitry Andric 
870fe6060f1SDimitry Andric   // Create the nested try instruction.
871fe6060f1SDimitry Andric   auto TryPos = getLatestInsertPos(
872fe6060f1SDimitry Andric       BeginBB, SmallPtrSet<const MachineInstr *, 4>(), AfterSet);
873fe6060f1SDimitry Andric   MachineInstr *Try = BuildMI(*BeginBB, TryPos, RangeBegin->getDebugLoc(),
874fe6060f1SDimitry Andric                               TII.get(WebAssembly::TRY))
875fe6060f1SDimitry Andric                           .addImm(int64_t(WebAssembly::BlockType::Void));
876fe6060f1SDimitry Andric 
877fe6060f1SDimitry Andric   // Create a BB to insert the 'delegate' instruction.
878fe6060f1SDimitry Andric   MachineBasicBlock *DelegateBB = MF.CreateMachineBasicBlock();
879fe6060f1SDimitry Andric   // If the destination of 'delegate' is not the caller, adds the destination to
880fe6060f1SDimitry Andric   // the BB's successors.
881fe6060f1SDimitry Andric   if (DelegateDest != FakeCallerBB)
882fe6060f1SDimitry Andric     DelegateBB->addSuccessor(DelegateDest);
883fe6060f1SDimitry Andric 
884fe6060f1SDimitry Andric   auto SplitPos = std::next(RangeEnd->getIterator());
885fe6060f1SDimitry Andric   if (SplitPos == EndBB->end()) {
886fe6060f1SDimitry Andric     // If the range's end instruction is at the end of the BB, insert the new
887fe6060f1SDimitry Andric     // delegate BB after the current BB.
888fe6060f1SDimitry Andric     MF.insert(std::next(EndBB->getIterator()), DelegateBB);
889fe6060f1SDimitry Andric     EndBB->addSuccessor(DelegateBB);
890fe6060f1SDimitry Andric 
891fe6060f1SDimitry Andric   } else {
892fe6060f1SDimitry Andric     // When the split pos is in the middle of a BB, we split the BB into two and
893fe6060f1SDimitry Andric     // put the 'delegate' BB in between. We normally create a split BB and make
894fe6060f1SDimitry Andric     // it a successor of the original BB (PostSplit == true), but in case the BB
895fe6060f1SDimitry Andric     // is an EH pad and the split pos is before 'catch', we should preserve the
896fe6060f1SDimitry Andric     // BB's property, including that it is an EH pad, in the later part of the
897fe6060f1SDimitry Andric     // BB, where 'catch' is. In this case we set PostSplit to false.
898fe6060f1SDimitry Andric     bool PostSplit = true;
899fe6060f1SDimitry Andric     if (EndBB->isEHPad()) {
900fe6060f1SDimitry Andric       for (auto I = MachineBasicBlock::iterator(SplitPos), E = EndBB->end();
901fe6060f1SDimitry Andric            I != E; ++I) {
902fe6060f1SDimitry Andric         if (WebAssembly::isCatch(I->getOpcode())) {
903fe6060f1SDimitry Andric           PostSplit = false;
9040b57cec5SDimitry Andric           break;
9050b57cec5SDimitry Andric         }
906fe6060f1SDimitry Andric       }
907fe6060f1SDimitry Andric     }
908fe6060f1SDimitry Andric 
909fe6060f1SDimitry Andric     MachineBasicBlock *PreBB = nullptr, *PostBB = nullptr;
910fe6060f1SDimitry Andric     if (PostSplit) {
911fe6060f1SDimitry Andric       // If the range's end instruction is in the middle of the BB, we split the
912fe6060f1SDimitry Andric       // BB into two and insert the delegate BB in between.
913fe6060f1SDimitry Andric       // - Before:
914fe6060f1SDimitry Andric       // bb:
915fe6060f1SDimitry Andric       //   range_end
916fe6060f1SDimitry Andric       //   other_insts
917fe6060f1SDimitry Andric       //
918fe6060f1SDimitry Andric       // - After:
919fe6060f1SDimitry Andric       // pre_bb: (previous 'bb')
920fe6060f1SDimitry Andric       //   range_end
921fe6060f1SDimitry Andric       // delegate_bb: (new)
922fe6060f1SDimitry Andric       //   delegate
923fe6060f1SDimitry Andric       // post_bb: (new)
924fe6060f1SDimitry Andric       //   other_insts
925fe6060f1SDimitry Andric       PreBB = EndBB;
926fe6060f1SDimitry Andric       PostBB = MF.CreateMachineBasicBlock();
927fe6060f1SDimitry Andric       MF.insert(std::next(PreBB->getIterator()), PostBB);
928fe6060f1SDimitry Andric       MF.insert(std::next(PreBB->getIterator()), DelegateBB);
929fe6060f1SDimitry Andric       PostBB->splice(PostBB->end(), PreBB, SplitPos, PreBB->end());
930fe6060f1SDimitry Andric       PostBB->transferSuccessors(PreBB);
931fe6060f1SDimitry Andric     } else {
932fe6060f1SDimitry Andric       // - Before:
933fe6060f1SDimitry Andric       // ehpad:
934fe6060f1SDimitry Andric       //   range_end
935fe6060f1SDimitry Andric       //   catch
936fe6060f1SDimitry Andric       //   ...
937fe6060f1SDimitry Andric       //
938fe6060f1SDimitry Andric       // - After:
939fe6060f1SDimitry Andric       // pre_bb: (new)
940fe6060f1SDimitry Andric       //   range_end
941fe6060f1SDimitry Andric       // delegate_bb: (new)
942fe6060f1SDimitry Andric       //   delegate
943fe6060f1SDimitry Andric       // post_bb: (previous 'ehpad')
944fe6060f1SDimitry Andric       //   catch
945fe6060f1SDimitry Andric       //   ...
946fe6060f1SDimitry Andric       assert(EndBB->isEHPad());
947fe6060f1SDimitry Andric       PreBB = MF.CreateMachineBasicBlock();
948fe6060f1SDimitry Andric       PostBB = EndBB;
949fe6060f1SDimitry Andric       MF.insert(PostBB->getIterator(), PreBB);
950fe6060f1SDimitry Andric       MF.insert(PostBB->getIterator(), DelegateBB);
951fe6060f1SDimitry Andric       PreBB->splice(PreBB->end(), PostBB, PostBB->begin(), SplitPos);
952fe6060f1SDimitry Andric       // We don't need to transfer predecessors of the EH pad to 'PreBB',
953fe6060f1SDimitry Andric       // because an EH pad's predecessors are all through unwind edges and they
954fe6060f1SDimitry Andric       // should still unwind to the EH pad, not PreBB.
955fe6060f1SDimitry Andric     }
956fe6060f1SDimitry Andric     unstackifyVRegsUsedInSplitBB(*PreBB, *PostBB);
957fe6060f1SDimitry Andric     PreBB->addSuccessor(DelegateBB);
958fe6060f1SDimitry Andric     PreBB->addSuccessor(PostBB);
959fe6060f1SDimitry Andric   }
960fe6060f1SDimitry Andric 
961fe6060f1SDimitry Andric   // Add 'delegate' instruction in the delegate BB created above.
962fe6060f1SDimitry Andric   MachineInstr *Delegate = BuildMI(DelegateBB, RangeEnd->getDebugLoc(),
963fe6060f1SDimitry Andric                                    TII.get(WebAssembly::DELEGATE))
964fe6060f1SDimitry Andric                                .addMBB(DelegateDest);
965fe6060f1SDimitry Andric   registerTryScope(Try, Delegate, nullptr);
966fe6060f1SDimitry Andric }
967fe6060f1SDimitry Andric 
fixCallUnwindMismatches(MachineFunction & MF)968fe6060f1SDimitry Andric bool WebAssemblyCFGStackify::fixCallUnwindMismatches(MachineFunction &MF) {
969fe6060f1SDimitry Andric   // Linearizing the control flow by placing TRY / END_TRY markers can create
970fe6060f1SDimitry Andric   // mismatches in unwind destinations for throwing instructions, such as calls.
971fe6060f1SDimitry Andric   //
972fe6060f1SDimitry Andric   // We use the 'delegate' instruction to fix the unwind mismatches. 'delegate'
973fe6060f1SDimitry Andric   // instruction delegates an exception to an outer 'catch'. It can target not
974fe6060f1SDimitry Andric   // only 'catch' but all block-like structures including another 'delegate',
975fe6060f1SDimitry Andric   // but with slightly different semantics than branches. When it targets a
976fe6060f1SDimitry Andric   // 'catch', it will delegate the exception to that catch. It is being
977fe6060f1SDimitry Andric   // discussed how to define the semantics when 'delegate''s target is a non-try
978fe6060f1SDimitry Andric   // block: it will either be a validation failure or it will target the next
979fe6060f1SDimitry Andric   // outer try-catch. But anyway our LLVM backend currently does not generate
980fe6060f1SDimitry Andric   // such code. The example below illustrates where the 'delegate' instruction
981fe6060f1SDimitry Andric   // in the middle will delegate the exception to, depending on the value of N.
982fe6060f1SDimitry Andric   // try
983fe6060f1SDimitry Andric   //   try
984fe6060f1SDimitry Andric   //     block
985fe6060f1SDimitry Andric   //       try
986fe6060f1SDimitry Andric   //         try
987fe6060f1SDimitry Andric   //           call @foo
988fe6060f1SDimitry Andric   //         delegate N    ;; Where will this delegate to?
989fe6060f1SDimitry Andric   //       catch           ;; N == 0
990fe6060f1SDimitry Andric   //       end
991fe6060f1SDimitry Andric   //     end               ;; N == 1 (invalid; will not be generated)
992fe6060f1SDimitry Andric   //   delegate            ;; N == 2
993fe6060f1SDimitry Andric   // catch                 ;; N == 3
994fe6060f1SDimitry Andric   // end
995fe6060f1SDimitry Andric   //                       ;; N == 4 (to caller)
996fe6060f1SDimitry Andric 
997fe6060f1SDimitry Andric   // 1. When an instruction may throw, but the EH pad it will unwind to can be
998fe6060f1SDimitry Andric   //    different from the original CFG.
999fe6060f1SDimitry Andric   //
1000fe6060f1SDimitry Andric   // Example: we have the following CFG:
1001fe6060f1SDimitry Andric   // bb0:
1002fe6060f1SDimitry Andric   //   call @foo    ; if it throws, unwind to bb2
1003fe6060f1SDimitry Andric   // bb1:
1004fe6060f1SDimitry Andric   //   call @bar    ; if it throws, unwind to bb3
1005fe6060f1SDimitry Andric   // bb2 (ehpad):
1006fe6060f1SDimitry Andric   //   catch
1007fe6060f1SDimitry Andric   //   ...
1008fe6060f1SDimitry Andric   // bb3 (ehpad)
1009fe6060f1SDimitry Andric   //   catch
1010fe6060f1SDimitry Andric   //   ...
1011fe6060f1SDimitry Andric   //
1012fe6060f1SDimitry Andric   // And the CFG is sorted in this order. Then after placing TRY markers, it
1013fe6060f1SDimitry Andric   // will look like: (BB markers are omitted)
1014fe6060f1SDimitry Andric   // try
1015fe6060f1SDimitry Andric   //   try
1016fe6060f1SDimitry Andric   //     call @foo
1017fe6060f1SDimitry Andric   //     call @bar   ;; if it throws, unwind to bb3
1018fe6060f1SDimitry Andric   //   catch         ;; ehpad (bb2)
1019fe6060f1SDimitry Andric   //     ...
1020fe6060f1SDimitry Andric   //   end_try
1021fe6060f1SDimitry Andric   // catch           ;; ehpad (bb3)
1022fe6060f1SDimitry Andric   //   ...
1023fe6060f1SDimitry Andric   // end_try
1024fe6060f1SDimitry Andric   //
1025fe6060f1SDimitry Andric   // Now if bar() throws, it is going to end up ip in bb2, not bb3, where it
1026fe6060f1SDimitry Andric   // is supposed to end up. We solve this problem by wrapping the mismatching
1027fe6060f1SDimitry Andric   // call with an inner try-delegate that rethrows the exception to the right
1028fe6060f1SDimitry Andric   // 'catch'.
1029fe6060f1SDimitry Andric   //
1030fe6060f1SDimitry Andric   // try
1031fe6060f1SDimitry Andric   //   try
1032fe6060f1SDimitry Andric   //     call @foo
1033fe6060f1SDimitry Andric   //     try               ;; (new)
1034fe6060f1SDimitry Andric   //       call @bar
1035fe6060f1SDimitry Andric   //     delegate 1 (bb3)  ;; (new)
1036fe6060f1SDimitry Andric   //   catch               ;; ehpad (bb2)
1037fe6060f1SDimitry Andric   //     ...
1038fe6060f1SDimitry Andric   //   end_try
1039fe6060f1SDimitry Andric   // catch                 ;; ehpad (bb3)
1040fe6060f1SDimitry Andric   //   ...
1041fe6060f1SDimitry Andric   // end_try
1042fe6060f1SDimitry Andric   //
1043fe6060f1SDimitry Andric   // ---
1044fe6060f1SDimitry Andric   // 2. The same as 1, but in this case an instruction unwinds to a caller
1045fe6060f1SDimitry Andric   //    function and not another EH pad.
1046fe6060f1SDimitry Andric   //
1047fe6060f1SDimitry Andric   // Example: we have the following CFG:
1048fe6060f1SDimitry Andric   // bb0:
1049fe6060f1SDimitry Andric   //   call @foo       ; if it throws, unwind to bb2
1050fe6060f1SDimitry Andric   // bb1:
1051fe6060f1SDimitry Andric   //   call @bar       ; if it throws, unwind to caller
1052fe6060f1SDimitry Andric   // bb2 (ehpad):
1053fe6060f1SDimitry Andric   //   catch
1054fe6060f1SDimitry Andric   //   ...
1055fe6060f1SDimitry Andric   //
1056fe6060f1SDimitry Andric   // And the CFG is sorted in this order. Then after placing TRY markers, it
1057fe6060f1SDimitry Andric   // will look like:
1058fe6060f1SDimitry Andric   // try
1059fe6060f1SDimitry Andric   //   call @foo
1060fe6060f1SDimitry Andric   //   call @bar     ;; if it throws, unwind to caller
1061fe6060f1SDimitry Andric   // catch           ;; ehpad (bb2)
1062fe6060f1SDimitry Andric   //   ...
1063fe6060f1SDimitry Andric   // end_try
1064fe6060f1SDimitry Andric   //
1065fe6060f1SDimitry Andric   // Now if bar() throws, it is going to end up ip in bb2, when it is supposed
1066fe6060f1SDimitry Andric   // throw up to the caller. We solve this problem in the same way, but in this
1067fe6060f1SDimitry Andric   // case 'delegate's immediate argument is the number of block depths + 1,
1068fe6060f1SDimitry Andric   // which means it rethrows to the caller.
1069fe6060f1SDimitry Andric   // try
1070fe6060f1SDimitry Andric   //   call @foo
1071fe6060f1SDimitry Andric   //   try                  ;; (new)
1072fe6060f1SDimitry Andric   //     call @bar
1073fe6060f1SDimitry Andric   //   delegate 1 (caller)  ;; (new)
1074fe6060f1SDimitry Andric   // catch                  ;; ehpad (bb2)
1075fe6060f1SDimitry Andric   //   ...
1076fe6060f1SDimitry Andric   // end_try
1077fe6060f1SDimitry Andric   //
1078fe6060f1SDimitry Andric   // Before rewriteDepthImmediates, delegate's argument is a BB. In case of the
1079fe6060f1SDimitry Andric   // caller, it will take a fake BB generated by getFakeCallerBlock(), which
1080fe6060f1SDimitry Andric   // will be converted to a correct immediate argument later.
1081fe6060f1SDimitry Andric   //
1082fe6060f1SDimitry Andric   // In case there are multiple calls in a BB that may throw to the caller, they
1083fe6060f1SDimitry Andric   // can be wrapped together in one nested try-delegate scope. (In 1, this
1084fe6060f1SDimitry Andric   // couldn't happen, because may-throwing instruction there had an unwind
1085fe6060f1SDimitry Andric   // destination, i.e., it was an invoke before, and there could be only one
1086fe6060f1SDimitry Andric   // invoke within a BB.)
1087fe6060f1SDimitry Andric 
1088fe6060f1SDimitry Andric   SmallVector<const MachineBasicBlock *, 8> EHPadStack;
1089fe6060f1SDimitry Andric   // Range of intructions to be wrapped in a new nested try/catch. A range
1090fe6060f1SDimitry Andric   // exists in a single BB and does not span multiple BBs.
1091fe6060f1SDimitry Andric   using TryRange = std::pair<MachineInstr *, MachineInstr *>;
1092fe6060f1SDimitry Andric   // In original CFG, <unwind destination BB, a vector of try ranges>
1093fe6060f1SDimitry Andric   DenseMap<MachineBasicBlock *, SmallVector<TryRange, 4>> UnwindDestToTryRanges;
1094fe6060f1SDimitry Andric 
1095fe6060f1SDimitry Andric   // Gather possibly throwing calls (i.e., previously invokes) whose current
1096fe6060f1SDimitry Andric   // unwind destination is not the same as the original CFG. (Case 1)
1097fe6060f1SDimitry Andric 
1098fe6060f1SDimitry Andric   for (auto &MBB : reverse(MF)) {
1099fe6060f1SDimitry Andric     bool SeenThrowableInstInBB = false;
1100fe6060f1SDimitry Andric     for (auto &MI : reverse(MBB)) {
1101fe6060f1SDimitry Andric       if (MI.getOpcode() == WebAssembly::TRY)
1102fe6060f1SDimitry Andric         EHPadStack.pop_back();
1103fe6060f1SDimitry Andric       else if (WebAssembly::isCatch(MI.getOpcode()))
1104fe6060f1SDimitry Andric         EHPadStack.push_back(MI.getParent());
1105fe6060f1SDimitry Andric 
1106fe6060f1SDimitry Andric       // In this loop we only gather calls that have an EH pad to unwind. So
1107fe6060f1SDimitry Andric       // there will be at most 1 such call (= invoke) in a BB, so after we've
1108fe6060f1SDimitry Andric       // seen one, we can skip the rest of BB. Also if MBB has no EH pad
1109fe6060f1SDimitry Andric       // successor or MI does not throw, this is not an invoke.
1110fe6060f1SDimitry Andric       if (SeenThrowableInstInBB || !MBB.hasEHPadSuccessor() ||
1111fe6060f1SDimitry Andric           !WebAssembly::mayThrow(MI))
1112fe6060f1SDimitry Andric         continue;
1113fe6060f1SDimitry Andric       SeenThrowableInstInBB = true;
1114fe6060f1SDimitry Andric 
1115fe6060f1SDimitry Andric       // If the EH pad on the stack top is where this instruction should unwind
1116fe6060f1SDimitry Andric       // next, we're good.
1117fe6060f1SDimitry Andric       MachineBasicBlock *UnwindDest = getFakeCallerBlock(MF);
1118fe6060f1SDimitry Andric       for (auto *Succ : MBB.successors()) {
1119fe6060f1SDimitry Andric         // Even though semantically a BB can have multiple successors in case an
1120fe6060f1SDimitry Andric         // exception is not caught by a catchpad, in our backend implementation
1121fe6060f1SDimitry Andric         // it is guaranteed that a BB can have at most one EH pad successor. For
1122fe6060f1SDimitry Andric         // details, refer to comments in findWasmUnwindDestinations function in
1123fe6060f1SDimitry Andric         // SelectionDAGBuilder.cpp.
1124fe6060f1SDimitry Andric         if (Succ->isEHPad()) {
1125fe6060f1SDimitry Andric           UnwindDest = Succ;
1126fe6060f1SDimitry Andric           break;
1127fe6060f1SDimitry Andric         }
1128fe6060f1SDimitry Andric       }
1129fe6060f1SDimitry Andric       if (EHPadStack.back() == UnwindDest)
1130fe6060f1SDimitry Andric         continue;
1131fe6060f1SDimitry Andric 
1132fe6060f1SDimitry Andric       // Include EH_LABELs in the range before and afer the invoke
1133fe6060f1SDimitry Andric       MachineInstr *RangeBegin = &MI, *RangeEnd = &MI;
1134fe6060f1SDimitry Andric       if (RangeBegin->getIterator() != MBB.begin() &&
1135fe6060f1SDimitry Andric           std::prev(RangeBegin->getIterator())->isEHLabel())
1136fe6060f1SDimitry Andric         RangeBegin = &*std::prev(RangeBegin->getIterator());
1137fe6060f1SDimitry Andric       if (std::next(RangeEnd->getIterator()) != MBB.end() &&
1138fe6060f1SDimitry Andric           std::next(RangeEnd->getIterator())->isEHLabel())
1139fe6060f1SDimitry Andric         RangeEnd = &*std::next(RangeEnd->getIterator());
1140fe6060f1SDimitry Andric 
1141fe6060f1SDimitry Andric       // If not, record the range.
1142fe6060f1SDimitry Andric       UnwindDestToTryRanges[UnwindDest].push_back(
1143fe6060f1SDimitry Andric           TryRange(RangeBegin, RangeEnd));
1144fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "- Call unwind mismatch: MBB = " << MBB.getName()
1145fe6060f1SDimitry Andric                         << "\nCall = " << MI
1146fe6060f1SDimitry Andric                         << "\nOriginal dest = " << UnwindDest->getName()
1147fe6060f1SDimitry Andric                         << "  Current dest = " << EHPadStack.back()->getName()
1148fe6060f1SDimitry Andric                         << "\n\n");
1149fe6060f1SDimitry Andric     }
1150fe6060f1SDimitry Andric   }
1151fe6060f1SDimitry Andric 
1152fe6060f1SDimitry Andric   assert(EHPadStack.empty());
1153fe6060f1SDimitry Andric 
1154fe6060f1SDimitry Andric   // Gather possibly throwing calls that are supposed to unwind up to the caller
1155fe6060f1SDimitry Andric   // if they throw, but currently unwind to an incorrect destination. Unlike the
1156fe6060f1SDimitry Andric   // loop above, there can be multiple calls within a BB that unwind to the
1157fe6060f1SDimitry Andric   // caller, which we should group together in a range. (Case 2)
1158fe6060f1SDimitry Andric 
1159fe6060f1SDimitry Andric   MachineInstr *RangeBegin = nullptr, *RangeEnd = nullptr; // inclusive
1160fe6060f1SDimitry Andric 
1161fe6060f1SDimitry Andric   // Record the range.
1162fe6060f1SDimitry Andric   auto RecordCallerMismatchRange = [&](const MachineBasicBlock *CurrentDest) {
1163fe6060f1SDimitry Andric     UnwindDestToTryRanges[getFakeCallerBlock(MF)].push_back(
1164fe6060f1SDimitry Andric         TryRange(RangeBegin, RangeEnd));
1165fe6060f1SDimitry Andric     LLVM_DEBUG(dbgs() << "- Call unwind mismatch: MBB = "
1166fe6060f1SDimitry Andric                       << RangeBegin->getParent()->getName()
1167fe6060f1SDimitry Andric                       << "\nRange begin = " << *RangeBegin
1168fe6060f1SDimitry Andric                       << "Range end = " << *RangeEnd
1169fe6060f1SDimitry Andric                       << "\nOriginal dest = caller  Current dest = "
1170fe6060f1SDimitry Andric                       << CurrentDest->getName() << "\n\n");
1171fe6060f1SDimitry Andric     RangeBegin = RangeEnd = nullptr; // Reset range pointers
1172fe6060f1SDimitry Andric   };
1173fe6060f1SDimitry Andric 
1174fe6060f1SDimitry Andric   for (auto &MBB : reverse(MF)) {
1175fe6060f1SDimitry Andric     bool SeenThrowableInstInBB = false;
1176fe6060f1SDimitry Andric     for (auto &MI : reverse(MBB)) {
1177fe6060f1SDimitry Andric       bool MayThrow = WebAssembly::mayThrow(MI);
1178fe6060f1SDimitry Andric 
1179fe6060f1SDimitry Andric       // If MBB has an EH pad successor and this is the last instruction that
1180fe6060f1SDimitry Andric       // may throw, this instruction unwinds to the EH pad and not to the
1181fe6060f1SDimitry Andric       // caller.
1182fe6060f1SDimitry Andric       if (MBB.hasEHPadSuccessor() && MayThrow && !SeenThrowableInstInBB)
1183fe6060f1SDimitry Andric         SeenThrowableInstInBB = true;
1184fe6060f1SDimitry Andric 
1185fe6060f1SDimitry Andric       // We wrap up the current range when we see a marker even if we haven't
1186fe6060f1SDimitry Andric       // finished a BB.
1187fe6060f1SDimitry Andric       else if (RangeEnd && WebAssembly::isMarker(MI.getOpcode()))
1188fe6060f1SDimitry Andric         RecordCallerMismatchRange(EHPadStack.back());
1189fe6060f1SDimitry Andric 
1190fe6060f1SDimitry Andric       // If EHPadStack is empty, that means it correctly unwinds to the caller
1191fe6060f1SDimitry Andric       // if it throws, so we're good. If MI does not throw, we're good too.
1192fe6060f1SDimitry Andric       else if (EHPadStack.empty() || !MayThrow) {
1193fe6060f1SDimitry Andric       }
1194fe6060f1SDimitry Andric 
1195fe6060f1SDimitry Andric       // We found an instruction that unwinds to the caller but currently has an
1196fe6060f1SDimitry Andric       // incorrect unwind destination. Create a new range or increment the
1197fe6060f1SDimitry Andric       // currently existing range.
1198fe6060f1SDimitry Andric       else {
1199fe6060f1SDimitry Andric         if (!RangeEnd)
1200fe6060f1SDimitry Andric           RangeBegin = RangeEnd = &MI;
1201fe6060f1SDimitry Andric         else
1202fe6060f1SDimitry Andric           RangeBegin = &MI;
1203fe6060f1SDimitry Andric       }
1204fe6060f1SDimitry Andric 
1205fe6060f1SDimitry Andric       // Update EHPadStack.
1206fe6060f1SDimitry Andric       if (MI.getOpcode() == WebAssembly::TRY)
1207fe6060f1SDimitry Andric         EHPadStack.pop_back();
1208fe6060f1SDimitry Andric       else if (WebAssembly::isCatch(MI.getOpcode()))
1209fe6060f1SDimitry Andric         EHPadStack.push_back(MI.getParent());
1210fe6060f1SDimitry Andric     }
1211fe6060f1SDimitry Andric 
1212fe6060f1SDimitry Andric     if (RangeEnd)
1213fe6060f1SDimitry Andric       RecordCallerMismatchRange(EHPadStack.back());
1214fe6060f1SDimitry Andric   }
1215fe6060f1SDimitry Andric 
1216fe6060f1SDimitry Andric   assert(EHPadStack.empty());
1217fe6060f1SDimitry Andric 
1218fe6060f1SDimitry Andric   // We don't have any unwind destination mismatches to resolve.
1219fe6060f1SDimitry Andric   if (UnwindDestToTryRanges.empty())
1220fe6060f1SDimitry Andric     return false;
1221fe6060f1SDimitry Andric 
1222fe6060f1SDimitry Andric   // Now we fix the mismatches by wrapping calls with inner try-delegates.
1223fe6060f1SDimitry Andric   for (auto &P : UnwindDestToTryRanges) {
1224fe6060f1SDimitry Andric     NumCallUnwindMismatches += P.second.size();
1225fe6060f1SDimitry Andric     MachineBasicBlock *UnwindDest = P.first;
1226fe6060f1SDimitry Andric     auto &TryRanges = P.second;
1227fe6060f1SDimitry Andric 
1228fe6060f1SDimitry Andric     for (auto Range : TryRanges) {
1229fe6060f1SDimitry Andric       MachineInstr *RangeBegin = nullptr, *RangeEnd = nullptr;
1230fe6060f1SDimitry Andric       std::tie(RangeBegin, RangeEnd) = Range;
1231fe6060f1SDimitry Andric       auto *MBB = RangeBegin->getParent();
1232fe6060f1SDimitry Andric 
1233fe6060f1SDimitry Andric       // If this BB has an EH pad successor, i.e., ends with an 'invoke', now we
1234fe6060f1SDimitry Andric       // are going to wrap the invoke with try-delegate, making the 'delegate'
1235fe6060f1SDimitry Andric       // BB the new successor instead, so remove the EH pad succesor here. The
1236fe6060f1SDimitry Andric       // BB may not have an EH pad successor if calls in this BB throw to the
1237fe6060f1SDimitry Andric       // caller.
1238fe6060f1SDimitry Andric       MachineBasicBlock *EHPad = nullptr;
1239fe6060f1SDimitry Andric       for (auto *Succ : MBB->successors()) {
1240fe6060f1SDimitry Andric         if (Succ->isEHPad()) {
1241fe6060f1SDimitry Andric           EHPad = Succ;
1242fe6060f1SDimitry Andric           break;
1243fe6060f1SDimitry Andric         }
1244fe6060f1SDimitry Andric       }
1245fe6060f1SDimitry Andric       if (EHPad)
1246fe6060f1SDimitry Andric         MBB->removeSuccessor(EHPad);
1247fe6060f1SDimitry Andric 
1248fe6060f1SDimitry Andric       addTryDelegate(RangeBegin, RangeEnd, UnwindDest);
1249fe6060f1SDimitry Andric     }
1250fe6060f1SDimitry Andric   }
1251fe6060f1SDimitry Andric 
1252fe6060f1SDimitry Andric   return true;
1253fe6060f1SDimitry Andric }
1254fe6060f1SDimitry Andric 
fixCatchUnwindMismatches(MachineFunction & MF)1255fe6060f1SDimitry Andric bool WebAssemblyCFGStackify::fixCatchUnwindMismatches(MachineFunction &MF) {
1256fe6060f1SDimitry Andric   // There is another kind of unwind destination mismatches besides call unwind
1257fe6060f1SDimitry Andric   // mismatches, which we will call "catch unwind mismatches". See this example
1258fe6060f1SDimitry Andric   // after the marker placement:
1259fe6060f1SDimitry Andric   // try
1260fe6060f1SDimitry Andric   //   try
1261fe6060f1SDimitry Andric   //     call @foo
1262fe6060f1SDimitry Andric   //   catch __cpp_exception  ;; ehpad A (next unwind dest: caller)
1263fe6060f1SDimitry Andric   //     ...
1264fe6060f1SDimitry Andric   //   end_try
1265fe6060f1SDimitry Andric   // catch_all                ;; ehpad B
1266fe6060f1SDimitry Andric   //   ...
1267fe6060f1SDimitry Andric   // end_try
1268fe6060f1SDimitry Andric   //
1269fe6060f1SDimitry Andric   // 'call @foo's unwind destination is the ehpad A. But suppose 'call @foo'
1270fe6060f1SDimitry Andric   // throws a foreign exception that is not caught by ehpad A, and its next
1271fe6060f1SDimitry Andric   // destination should be the caller. But after control flow linearization,
1272fe6060f1SDimitry Andric   // another EH pad can be placed in between (e.g. ehpad B here), making the
1273fe6060f1SDimitry Andric   // next unwind destination incorrect. In this case, the  foreign exception
1274fe6060f1SDimitry Andric   // will instead go to ehpad B and will be caught there instead. In this
1275fe6060f1SDimitry Andric   // example the correct next unwind destination is the caller, but it can be
1276fe6060f1SDimitry Andric   // another outer catch in other cases.
1277fe6060f1SDimitry Andric   //
1278fe6060f1SDimitry Andric   // There is no specific 'call' or 'throw' instruction to wrap with a
1279fe6060f1SDimitry Andric   // try-delegate, so we wrap the whole try-catch-end with a try-delegate and
1280fe6060f1SDimitry Andric   // make it rethrow to the right destination, as in the example below:
1281fe6060f1SDimitry Andric   // try
1282fe6060f1SDimitry Andric   //   try                     ;; (new)
1283fe6060f1SDimitry Andric   //     try
1284fe6060f1SDimitry Andric   //       call @foo
1285fe6060f1SDimitry Andric   //     catch __cpp_exception ;; ehpad A (next unwind dest: caller)
1286fe6060f1SDimitry Andric   //       ...
1287fe6060f1SDimitry Andric   //     end_try
1288fe6060f1SDimitry Andric   //   delegate 1 (caller)     ;; (new)
1289fe6060f1SDimitry Andric   // catch_all                 ;; ehpad B
1290fe6060f1SDimitry Andric   //   ...
1291fe6060f1SDimitry Andric   // end_try
1292fe6060f1SDimitry Andric 
1293fe6060f1SDimitry Andric   const auto *EHInfo = MF.getWasmEHFuncInfo();
129406c3fb27SDimitry Andric   assert(EHInfo);
1295fe6060f1SDimitry Andric   SmallVector<const MachineBasicBlock *, 8> EHPadStack;
1296fe6060f1SDimitry Andric   // For EH pads that have catch unwind mismatches, a map of <EH pad, its
1297fe6060f1SDimitry Andric   // correct unwind destination>.
1298fe6060f1SDimitry Andric   DenseMap<MachineBasicBlock *, MachineBasicBlock *> EHPadToUnwindDest;
1299fe6060f1SDimitry Andric 
1300fe6060f1SDimitry Andric   for (auto &MBB : reverse(MF)) {
1301fe6060f1SDimitry Andric     for (auto &MI : reverse(MBB)) {
1302fe6060f1SDimitry Andric       if (MI.getOpcode() == WebAssembly::TRY)
1303fe6060f1SDimitry Andric         EHPadStack.pop_back();
1304fe6060f1SDimitry Andric       else if (MI.getOpcode() == WebAssembly::DELEGATE)
1305fe6060f1SDimitry Andric         EHPadStack.push_back(&MBB);
1306fe6060f1SDimitry Andric       else if (WebAssembly::isCatch(MI.getOpcode())) {
1307fe6060f1SDimitry Andric         auto *EHPad = &MBB;
1308fe6060f1SDimitry Andric 
1309fe6060f1SDimitry Andric         // catch_all always catches an exception, so we don't need to do
1310fe6060f1SDimitry Andric         // anything
1311fe6060f1SDimitry Andric         if (MI.getOpcode() == WebAssembly::CATCH_ALL) {
1312fe6060f1SDimitry Andric         }
1313fe6060f1SDimitry Andric 
1314fe6060f1SDimitry Andric         // This can happen when the unwind dest was removed during the
1315fe6060f1SDimitry Andric         // optimization, e.g. because it was unreachable.
1316fe6060f1SDimitry Andric         else if (EHPadStack.empty() && EHInfo->hasUnwindDest(EHPad)) {
1317fe6060f1SDimitry Andric           LLVM_DEBUG(dbgs() << "EHPad (" << EHPad->getName()
1318fe6060f1SDimitry Andric                             << "'s unwind destination does not exist anymore"
1319fe6060f1SDimitry Andric                             << "\n\n");
1320fe6060f1SDimitry Andric         }
1321fe6060f1SDimitry Andric 
1322fe6060f1SDimitry Andric         // The EHPad's next unwind destination is the caller, but we incorrectly
1323fe6060f1SDimitry Andric         // unwind to another EH pad.
1324fe6060f1SDimitry Andric         else if (!EHPadStack.empty() && !EHInfo->hasUnwindDest(EHPad)) {
1325fe6060f1SDimitry Andric           EHPadToUnwindDest[EHPad] = getFakeCallerBlock(MF);
1326fe6060f1SDimitry Andric           LLVM_DEBUG(dbgs()
1327fe6060f1SDimitry Andric                      << "- Catch unwind mismatch:\nEHPad = " << EHPad->getName()
1328fe6060f1SDimitry Andric                      << "  Original dest = caller  Current dest = "
1329fe6060f1SDimitry Andric                      << EHPadStack.back()->getName() << "\n\n");
1330fe6060f1SDimitry Andric         }
1331fe6060f1SDimitry Andric 
1332fe6060f1SDimitry Andric         // The EHPad's next unwind destination is an EH pad, whereas we
1333fe6060f1SDimitry Andric         // incorrectly unwind to another EH pad.
1334fe6060f1SDimitry Andric         else if (!EHPadStack.empty() && EHInfo->hasUnwindDest(EHPad)) {
1335fe6060f1SDimitry Andric           auto *UnwindDest = EHInfo->getUnwindDest(EHPad);
1336fe6060f1SDimitry Andric           if (EHPadStack.back() != UnwindDest) {
1337fe6060f1SDimitry Andric             EHPadToUnwindDest[EHPad] = UnwindDest;
1338fe6060f1SDimitry Andric             LLVM_DEBUG(dbgs() << "- Catch unwind mismatch:\nEHPad = "
1339fe6060f1SDimitry Andric                               << EHPad->getName() << "  Original dest = "
1340fe6060f1SDimitry Andric                               << UnwindDest->getName() << "  Current dest = "
1341fe6060f1SDimitry Andric                               << EHPadStack.back()->getName() << "\n\n");
1342fe6060f1SDimitry Andric           }
1343fe6060f1SDimitry Andric         }
1344fe6060f1SDimitry Andric 
1345fe6060f1SDimitry Andric         EHPadStack.push_back(EHPad);
1346fe6060f1SDimitry Andric       }
1347fe6060f1SDimitry Andric     }
1348fe6060f1SDimitry Andric   }
1349fe6060f1SDimitry Andric 
1350fe6060f1SDimitry Andric   assert(EHPadStack.empty());
1351fe6060f1SDimitry Andric   if (EHPadToUnwindDest.empty())
1352fe6060f1SDimitry Andric     return false;
1353fe6060f1SDimitry Andric   NumCatchUnwindMismatches += EHPadToUnwindDest.size();
1354fe6060f1SDimitry Andric   SmallPtrSet<MachineBasicBlock *, 4> NewEndTryBBs;
1355fe6060f1SDimitry Andric 
1356fe6060f1SDimitry Andric   for (auto &P : EHPadToUnwindDest) {
1357fe6060f1SDimitry Andric     MachineBasicBlock *EHPad = P.first;
1358fe6060f1SDimitry Andric     MachineBasicBlock *UnwindDest = P.second;
1359fe6060f1SDimitry Andric     MachineInstr *Try = EHPadToTry[EHPad];
1360fe6060f1SDimitry Andric     MachineInstr *EndTry = BeginToEnd[Try];
1361fe6060f1SDimitry Andric     addTryDelegate(Try, EndTry, UnwindDest);
1362fe6060f1SDimitry Andric     NewEndTryBBs.insert(EndTry->getParent());
1363fe6060f1SDimitry Andric   }
1364fe6060f1SDimitry Andric 
1365fe6060f1SDimitry Andric   // Adding a try-delegate wrapping an existing try-catch-end can make existing
1366fe6060f1SDimitry Andric   // branch destination BBs invalid. For example,
1367fe6060f1SDimitry Andric   //
1368fe6060f1SDimitry Andric   // - Before:
1369fe6060f1SDimitry Andric   // bb0:
1370fe6060f1SDimitry Andric   //   block
1371fe6060f1SDimitry Andric   //     br bb3
1372fe6060f1SDimitry Andric   // bb1:
1373fe6060f1SDimitry Andric   //     try
1374fe6060f1SDimitry Andric   //       ...
1375fe6060f1SDimitry Andric   // bb2: (ehpad)
1376fe6060f1SDimitry Andric   //     catch
1377fe6060f1SDimitry Andric   // bb3:
1378fe6060f1SDimitry Andric   //     end_try
1379fe6060f1SDimitry Andric   //   end_block   ;; 'br bb3' targets here
1380fe6060f1SDimitry Andric   //
1381fe6060f1SDimitry Andric   // Suppose this try-catch-end has a catch unwind mismatch, so we need to wrap
1382fe6060f1SDimitry Andric   // this with a try-delegate. Then this becomes:
1383fe6060f1SDimitry Andric   //
1384fe6060f1SDimitry Andric   // - After:
1385fe6060f1SDimitry Andric   // bb0:
1386fe6060f1SDimitry Andric   //   block
1387fe6060f1SDimitry Andric   //     br bb3    ;; invalid destination!
1388fe6060f1SDimitry Andric   // bb1:
1389fe6060f1SDimitry Andric   //     try       ;; (new instruction)
1390fe6060f1SDimitry Andric   //       try
1391fe6060f1SDimitry Andric   //         ...
1392fe6060f1SDimitry Andric   // bb2: (ehpad)
1393fe6060f1SDimitry Andric   //       catch
1394fe6060f1SDimitry Andric   // bb3:
1395fe6060f1SDimitry Andric   //       end_try ;; 'br bb3' still incorrectly targets here!
1396fe6060f1SDimitry Andric   // delegate_bb:  ;; (new BB)
1397fe6060f1SDimitry Andric   //     delegate  ;; (new instruction)
1398fe6060f1SDimitry Andric   // split_bb:     ;; (new BB)
1399fe6060f1SDimitry Andric   //   end_block
1400fe6060f1SDimitry Andric   //
1401fe6060f1SDimitry Andric   // Now 'br bb3' incorrectly branches to an inner scope.
1402fe6060f1SDimitry Andric   //
1403fe6060f1SDimitry Andric   // As we can see in this case, when branches target a BB that has both
1404fe6060f1SDimitry Andric   // 'end_try' and 'end_block' and the BB is split to insert a 'delegate', we
1405fe6060f1SDimitry Andric   // have to remap existing branch destinations so that they target not the
1406fe6060f1SDimitry Andric   // 'end_try' BB but the new 'end_block' BB. There can be multiple 'delegate's
1407fe6060f1SDimitry Andric   // in between, so we try to find the next BB with 'end_block' instruction. In
1408fe6060f1SDimitry Andric   // this example, the 'br bb3' instruction should be remapped to 'br split_bb'.
1409fe6060f1SDimitry Andric   for (auto &MBB : MF) {
1410fe6060f1SDimitry Andric     for (auto &MI : MBB) {
1411fe6060f1SDimitry Andric       if (MI.isTerminator()) {
1412fe6060f1SDimitry Andric         for (auto &MO : MI.operands()) {
1413fe6060f1SDimitry Andric           if (MO.isMBB() && NewEndTryBBs.count(MO.getMBB())) {
1414fe6060f1SDimitry Andric             auto *BrDest = MO.getMBB();
1415fe6060f1SDimitry Andric             bool FoundEndBlock = false;
1416fe6060f1SDimitry Andric             for (; std::next(BrDest->getIterator()) != MF.end();
1417fe6060f1SDimitry Andric                  BrDest = BrDest->getNextNode()) {
1418fe6060f1SDimitry Andric               for (const auto &MI : *BrDest) {
1419fe6060f1SDimitry Andric                 if (MI.getOpcode() == WebAssembly::END_BLOCK) {
1420fe6060f1SDimitry Andric                   FoundEndBlock = true;
1421fe6060f1SDimitry Andric                   break;
1422fe6060f1SDimitry Andric                 }
1423fe6060f1SDimitry Andric               }
1424fe6060f1SDimitry Andric               if (FoundEndBlock)
1425fe6060f1SDimitry Andric                 break;
1426fe6060f1SDimitry Andric             }
1427fe6060f1SDimitry Andric             assert(FoundEndBlock);
1428fe6060f1SDimitry Andric             MO.setMBB(BrDest);
1429fe6060f1SDimitry Andric           }
1430fe6060f1SDimitry Andric         }
1431fe6060f1SDimitry Andric       }
1432fe6060f1SDimitry Andric     }
1433fe6060f1SDimitry Andric   }
1434fe6060f1SDimitry Andric 
1435fe6060f1SDimitry Andric   return true;
1436fe6060f1SDimitry Andric }
1437fe6060f1SDimitry Andric 
recalculateScopeTops(MachineFunction & MF)1438fe6060f1SDimitry Andric void WebAssemblyCFGStackify::recalculateScopeTops(MachineFunction &MF) {
1439fe6060f1SDimitry Andric   // Renumber BBs and recalculate ScopeTop info because new BBs might have been
1440fe6060f1SDimitry Andric   // created and inserted during fixing unwind mismatches.
1441fe6060f1SDimitry Andric   MF.RenumberBlocks();
1442fe6060f1SDimitry Andric   ScopeTops.clear();
1443fe6060f1SDimitry Andric   ScopeTops.resize(MF.getNumBlockIDs());
1444fe6060f1SDimitry Andric   for (auto &MBB : reverse(MF)) {
1445fe6060f1SDimitry Andric     for (auto &MI : reverse(MBB)) {
1446fe6060f1SDimitry Andric       if (ScopeTops[MBB.getNumber()])
1447fe6060f1SDimitry Andric         break;
1448fe6060f1SDimitry Andric       switch (MI.getOpcode()) {
1449fe6060f1SDimitry Andric       case WebAssembly::END_BLOCK:
1450fe6060f1SDimitry Andric       case WebAssembly::END_LOOP:
1451fe6060f1SDimitry Andric       case WebAssembly::END_TRY:
1452fe6060f1SDimitry Andric       case WebAssembly::DELEGATE:
1453fe6060f1SDimitry Andric         updateScopeTops(EndToBegin[&MI]->getParent(), &MBB);
1454fe6060f1SDimitry Andric         break;
1455fe6060f1SDimitry Andric       case WebAssembly::CATCH:
1456fe6060f1SDimitry Andric       case WebAssembly::CATCH_ALL:
1457fe6060f1SDimitry Andric         updateScopeTops(EHPadToTry[&MBB]->getParent(), &MBB);
1458fe6060f1SDimitry Andric         break;
1459fe6060f1SDimitry Andric       }
1460fe6060f1SDimitry Andric     }
1461fe6060f1SDimitry Andric   }
14620b57cec5SDimitry Andric }
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric /// In normal assembly languages, when the end of a function is unreachable,
14650b57cec5SDimitry Andric /// because the function ends in an infinite loop or a noreturn call or similar,
14660b57cec5SDimitry Andric /// it isn't necessary to worry about the function return type at the end of
14670b57cec5SDimitry Andric /// the function, because it's never reached. However, in WebAssembly, blocks
14680b57cec5SDimitry Andric /// that end at the function end need to have a return type signature that
14690b57cec5SDimitry Andric /// matches the function signature, even though it's unreachable. This function
14700b57cec5SDimitry Andric /// checks for such cases and fixes up the signatures.
fixEndsAtEndOfFunction(MachineFunction & MF)14710b57cec5SDimitry Andric void WebAssemblyCFGStackify::fixEndsAtEndOfFunction(MachineFunction &MF) {
14720b57cec5SDimitry Andric   const auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
14730b57cec5SDimitry Andric 
14740b57cec5SDimitry Andric   if (MFI.getResults().empty())
14750b57cec5SDimitry Andric     return;
14760b57cec5SDimitry Andric 
14778bcb0991SDimitry Andric   // MCInstLower will add the proper types to multivalue signatures based on the
14788bcb0991SDimitry Andric   // function return type
14798bcb0991SDimitry Andric   WebAssembly::BlockType RetType =
14808bcb0991SDimitry Andric       MFI.getResults().size() > 1
14818bcb0991SDimitry Andric           ? WebAssembly::BlockType::Multivalue
14828bcb0991SDimitry Andric           : WebAssembly::BlockType(
14838bcb0991SDimitry Andric                 WebAssembly::toValType(MFI.getResults().front()));
14840b57cec5SDimitry Andric 
1485e8d8bef9SDimitry Andric   SmallVector<MachineBasicBlock::reverse_iterator, 4> Worklist;
1486e8d8bef9SDimitry Andric   Worklist.push_back(MF.rbegin()->rbegin());
1487e8d8bef9SDimitry Andric 
1488e8d8bef9SDimitry Andric   auto Process = [&](MachineBasicBlock::reverse_iterator It) {
1489e8d8bef9SDimitry Andric     auto *MBB = It->getParent();
1490e8d8bef9SDimitry Andric     while (It != MBB->rend()) {
1491e8d8bef9SDimitry Andric       MachineInstr &MI = *It++;
14920b57cec5SDimitry Andric       if (MI.isPosition() || MI.isDebugInstr())
14930b57cec5SDimitry Andric         continue;
14948bcb0991SDimitry Andric       switch (MI.getOpcode()) {
1495e8d8bef9SDimitry Andric       case WebAssembly::END_TRY: {
1496e8d8bef9SDimitry Andric         // If a 'try''s return type is fixed, both its try body and catch body
1497e8d8bef9SDimitry Andric         // should satisfy the return type, so we need to search 'end'
1498e8d8bef9SDimitry Andric         // instructions before its corresponding 'catch' too.
1499e8d8bef9SDimitry Andric         auto *EHPad = TryToEHPad.lookup(EndToBegin[&MI]);
1500e8d8bef9SDimitry Andric         assert(EHPad);
1501e8d8bef9SDimitry Andric         auto NextIt =
1502e8d8bef9SDimitry Andric             std::next(WebAssembly::findCatch(EHPad)->getReverseIterator());
1503e8d8bef9SDimitry Andric         if (NextIt != EHPad->rend())
1504e8d8bef9SDimitry Andric           Worklist.push_back(NextIt);
1505bdd1243dSDimitry Andric         [[fallthrough]];
1506e8d8bef9SDimitry Andric       }
15078bcb0991SDimitry Andric       case WebAssembly::END_BLOCK:
15088bcb0991SDimitry Andric       case WebAssembly::END_LOOP:
1509fe6060f1SDimitry Andric       case WebAssembly::DELEGATE:
15100b57cec5SDimitry Andric         EndToBegin[&MI]->getOperand(0).setImm(int32_t(RetType));
15110b57cec5SDimitry Andric         continue;
15128bcb0991SDimitry Andric       default:
1513e8d8bef9SDimitry Andric         // Something other than an `end`. We're done for this BB.
15140b57cec5SDimitry Andric         return;
15150b57cec5SDimitry Andric       }
15160b57cec5SDimitry Andric     }
1517e8d8bef9SDimitry Andric     // We've reached the beginning of a BB. Continue the search in the previous
1518e8d8bef9SDimitry Andric     // BB.
1519e8d8bef9SDimitry Andric     Worklist.push_back(MBB->getPrevNode()->rbegin());
1520e8d8bef9SDimitry Andric   };
1521e8d8bef9SDimitry Andric 
1522e8d8bef9SDimitry Andric   while (!Worklist.empty())
1523e8d8bef9SDimitry Andric     Process(Worklist.pop_back_val());
15248bcb0991SDimitry Andric }
15250b57cec5SDimitry Andric 
15260b57cec5SDimitry Andric // WebAssembly functions end with an end instruction, as if the function body
15270b57cec5SDimitry Andric // were a block.
appendEndToFunction(MachineFunction & MF,const WebAssemblyInstrInfo & TII)15280b57cec5SDimitry Andric static void appendEndToFunction(MachineFunction &MF,
15290b57cec5SDimitry Andric                                 const WebAssemblyInstrInfo &TII) {
15300b57cec5SDimitry Andric   BuildMI(MF.back(), MF.back().end(),
15310b57cec5SDimitry Andric           MF.back().findPrevDebugLoc(MF.back().end()),
15320b57cec5SDimitry Andric           TII.get(WebAssembly::END_FUNCTION));
15330b57cec5SDimitry Andric }
15340b57cec5SDimitry Andric 
15350b57cec5SDimitry Andric /// Insert LOOP/TRY/BLOCK markers at appropriate places.
placeMarkers(MachineFunction & MF)15360b57cec5SDimitry Andric void WebAssemblyCFGStackify::placeMarkers(MachineFunction &MF) {
15370b57cec5SDimitry Andric   // We allocate one more than the number of blocks in the function to
15380b57cec5SDimitry Andric   // accommodate for the possible fake block we may insert at the end.
15390b57cec5SDimitry Andric   ScopeTops.resize(MF.getNumBlockIDs() + 1);
15400b57cec5SDimitry Andric   // Place the LOOP for MBB if MBB is the header of a loop.
15410b57cec5SDimitry Andric   for (auto &MBB : MF)
15420b57cec5SDimitry Andric     placeLoopMarker(MBB);
15430b57cec5SDimitry Andric 
15440b57cec5SDimitry Andric   const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
15450b57cec5SDimitry Andric   for (auto &MBB : MF) {
15460b57cec5SDimitry Andric     if (MBB.isEHPad()) {
15470b57cec5SDimitry Andric       // Place the TRY for MBB if MBB is the EH pad of an exception.
15480b57cec5SDimitry Andric       if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm &&
15490b57cec5SDimitry Andric           MF.getFunction().hasPersonalityFn())
15500b57cec5SDimitry Andric         placeTryMarker(MBB);
15510b57cec5SDimitry Andric     } else {
15520b57cec5SDimitry Andric       // Place the BLOCK for MBB if MBB is branched to from above.
15530b57cec5SDimitry Andric       placeBlockMarker(MBB);
15540b57cec5SDimitry Andric     }
15550b57cec5SDimitry Andric   }
15560b57cec5SDimitry Andric   // Fix mismatches in unwind destinations induced by linearizing the code.
15578bcb0991SDimitry Andric   if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm &&
1558fe6060f1SDimitry Andric       MF.getFunction().hasPersonalityFn()) {
1559fe6060f1SDimitry Andric     bool Changed = fixCallUnwindMismatches(MF);
1560fe6060f1SDimitry Andric     Changed |= fixCatchUnwindMismatches(MF);
1561fe6060f1SDimitry Andric     if (Changed)
1562fe6060f1SDimitry Andric       recalculateScopeTops(MF);
1563fe6060f1SDimitry Andric   }
1564fe6060f1SDimitry Andric }
1565fe6060f1SDimitry Andric 
getBranchDepth(const SmallVectorImpl<EndMarkerInfo> & Stack,const MachineBasicBlock * MBB)1566fe6060f1SDimitry Andric unsigned WebAssemblyCFGStackify::getBranchDepth(
1567fe6060f1SDimitry Andric     const SmallVectorImpl<EndMarkerInfo> &Stack, const MachineBasicBlock *MBB) {
1568fe6060f1SDimitry Andric   unsigned Depth = 0;
1569fe6060f1SDimitry Andric   for (auto X : reverse(Stack)) {
1570fe6060f1SDimitry Andric     if (X.first == MBB)
1571fe6060f1SDimitry Andric       break;
1572fe6060f1SDimitry Andric     ++Depth;
1573fe6060f1SDimitry Andric   }
1574fe6060f1SDimitry Andric   assert(Depth < Stack.size() && "Branch destination should be in scope");
1575fe6060f1SDimitry Andric   return Depth;
1576fe6060f1SDimitry Andric }
1577fe6060f1SDimitry Andric 
getDelegateDepth(const SmallVectorImpl<EndMarkerInfo> & Stack,const MachineBasicBlock * MBB)1578fe6060f1SDimitry Andric unsigned WebAssemblyCFGStackify::getDelegateDepth(
1579fe6060f1SDimitry Andric     const SmallVectorImpl<EndMarkerInfo> &Stack, const MachineBasicBlock *MBB) {
1580fe6060f1SDimitry Andric   if (MBB == FakeCallerBB)
1581fe6060f1SDimitry Andric     return Stack.size();
1582fe6060f1SDimitry Andric   // Delegate's destination is either a catch or a another delegate BB. When the
1583fe6060f1SDimitry Andric   // destination is another delegate, we can compute the argument in the same
1584fe6060f1SDimitry Andric   // way as branches, because the target delegate BB only contains the single
1585fe6060f1SDimitry Andric   // delegate instruction.
1586fe6060f1SDimitry Andric   if (!MBB->isEHPad()) // Target is a delegate BB
1587fe6060f1SDimitry Andric     return getBranchDepth(Stack, MBB);
1588fe6060f1SDimitry Andric 
1589fe6060f1SDimitry Andric   // When the delegate's destination is a catch BB, we need to use its
1590fe6060f1SDimitry Andric   // corresponding try's end_try BB because Stack contains each marker's end BB.
1591fe6060f1SDimitry Andric   // Also we need to check if the end marker instruction matches, because a
1592fe6060f1SDimitry Andric   // single BB can contain multiple end markers, like this:
1593fe6060f1SDimitry Andric   // bb:
1594fe6060f1SDimitry Andric   //   END_BLOCK
1595fe6060f1SDimitry Andric   //   END_TRY
1596fe6060f1SDimitry Andric   //   END_BLOCK
1597fe6060f1SDimitry Andric   //   END_TRY
1598fe6060f1SDimitry Andric   //   ...
1599fe6060f1SDimitry Andric   //
1600fe6060f1SDimitry Andric   // In case of branches getting the immediate that targets any of these is
1601fe6060f1SDimitry Andric   // fine, but delegate has to exactly target the correct try.
1602fe6060f1SDimitry Andric   unsigned Depth = 0;
1603fe6060f1SDimitry Andric   const MachineInstr *EndTry = BeginToEnd[EHPadToTry[MBB]];
1604fe6060f1SDimitry Andric   for (auto X : reverse(Stack)) {
1605fe6060f1SDimitry Andric     if (X.first == EndTry->getParent() && X.second == EndTry)
1606fe6060f1SDimitry Andric       break;
1607fe6060f1SDimitry Andric     ++Depth;
1608fe6060f1SDimitry Andric   }
1609fe6060f1SDimitry Andric   assert(Depth < Stack.size() && "Delegate destination should be in scope");
1610fe6060f1SDimitry Andric   return Depth;
1611fe6060f1SDimitry Andric }
1612fe6060f1SDimitry Andric 
getRethrowDepth(const SmallVectorImpl<EndMarkerInfo> & Stack,const SmallVectorImpl<const MachineBasicBlock * > & EHPadStack)1613fe6060f1SDimitry Andric unsigned WebAssemblyCFGStackify::getRethrowDepth(
1614fe6060f1SDimitry Andric     const SmallVectorImpl<EndMarkerInfo> &Stack,
1615fe6060f1SDimitry Andric     const SmallVectorImpl<const MachineBasicBlock *> &EHPadStack) {
1616fe6060f1SDimitry Andric   unsigned Depth = 0;
1617fe6060f1SDimitry Andric   // In our current implementation, rethrows always rethrow the exception caught
1618fe6060f1SDimitry Andric   // by the innermost enclosing catch. This means while traversing Stack in the
1619fe6060f1SDimitry Andric   // reverse direction, when we encounter END_TRY, we should check if the
1620fe6060f1SDimitry Andric   // END_TRY corresponds to the current innermost EH pad. For example:
1621fe6060f1SDimitry Andric   // try
1622fe6060f1SDimitry Andric   //   ...
1623fe6060f1SDimitry Andric   // catch         ;; (a)
1624fe6060f1SDimitry Andric   //   try
1625fe6060f1SDimitry Andric   //     rethrow 1 ;; (b)
1626fe6060f1SDimitry Andric   //   catch       ;; (c)
1627fe6060f1SDimitry Andric   //     rethrow 0 ;; (d)
1628fe6060f1SDimitry Andric   //   end         ;; (e)
1629fe6060f1SDimitry Andric   // end           ;; (f)
1630fe6060f1SDimitry Andric   //
1631fe6060f1SDimitry Andric   // When we are at 'rethrow' (d), while reversely traversing Stack the first
1632fe6060f1SDimitry Andric   // 'end' we encounter is the 'end' (e), which corresponds to the 'catch' (c).
1633fe6060f1SDimitry Andric   // And 'rethrow' (d) rethrows the exception caught by 'catch' (c), so we stop
1634fe6060f1SDimitry Andric   // there and the depth should be 0. But when we are at 'rethrow' (b), it
1635fe6060f1SDimitry Andric   // rethrows the exception caught by 'catch' (a), so when traversing Stack
1636fe6060f1SDimitry Andric   // reversely, we should skip the 'end' (e) and choose 'end' (f), which
1637fe6060f1SDimitry Andric   // corresponds to 'catch' (a).
1638fe6060f1SDimitry Andric   for (auto X : reverse(Stack)) {
1639fe6060f1SDimitry Andric     const MachineInstr *End = X.second;
1640fe6060f1SDimitry Andric     if (End->getOpcode() == WebAssembly::END_TRY) {
1641fe6060f1SDimitry Andric       auto *EHPad = TryToEHPad[EndToBegin[End]];
1642fe6060f1SDimitry Andric       if (EHPadStack.back() == EHPad)
1643fe6060f1SDimitry Andric         break;
1644fe6060f1SDimitry Andric     }
1645fe6060f1SDimitry Andric     ++Depth;
1646fe6060f1SDimitry Andric   }
1647fe6060f1SDimitry Andric   assert(Depth < Stack.size() && "Rethrow destination should be in scope");
1648fe6060f1SDimitry Andric   return Depth;
16490b57cec5SDimitry Andric }
16500b57cec5SDimitry Andric 
rewriteDepthImmediates(MachineFunction & MF)16510b57cec5SDimitry Andric void WebAssemblyCFGStackify::rewriteDepthImmediates(MachineFunction &MF) {
16520b57cec5SDimitry Andric   // Now rewrite references to basic blocks to be depth immediates.
1653fe6060f1SDimitry Andric   SmallVector<EndMarkerInfo, 8> Stack;
1654fe6060f1SDimitry Andric   SmallVector<const MachineBasicBlock *, 8> EHPadStack;
16550b57cec5SDimitry Andric   for (auto &MBB : reverse(MF)) {
1656349cc55cSDimitry Andric     for (MachineInstr &MI : llvm::reverse(MBB)) {
16570b57cec5SDimitry Andric       switch (MI.getOpcode()) {
16580b57cec5SDimitry Andric       case WebAssembly::BLOCK:
16590b57cec5SDimitry Andric       case WebAssembly::TRY:
1660fe6060f1SDimitry Andric         assert(ScopeTops[Stack.back().first->getNumber()]->getNumber() <=
16610b57cec5SDimitry Andric                    MBB.getNumber() &&
16620b57cec5SDimitry Andric                "Block/try marker should be balanced");
16630b57cec5SDimitry Andric         Stack.pop_back();
16640b57cec5SDimitry Andric         break;
16650b57cec5SDimitry Andric 
16660b57cec5SDimitry Andric       case WebAssembly::LOOP:
1667fe6060f1SDimitry Andric         assert(Stack.back().first == &MBB && "Loop top should be balanced");
16680b57cec5SDimitry Andric         Stack.pop_back();
16690b57cec5SDimitry Andric         break;
16700b57cec5SDimitry Andric 
16710b57cec5SDimitry Andric       case WebAssembly::END_BLOCK:
1672fe6060f1SDimitry Andric         Stack.push_back(std::make_pair(&MBB, &MI));
16730b57cec5SDimitry Andric         break;
16740b57cec5SDimitry Andric 
1675fe6060f1SDimitry Andric       case WebAssembly::END_TRY: {
1676fe6060f1SDimitry Andric         // We handle DELEGATE in the default level, because DELEGATE has
1677fe6060f1SDimitry Andric         // immediate operands to rewrite.
1678fe6060f1SDimitry Andric         Stack.push_back(std::make_pair(&MBB, &MI));
1679fe6060f1SDimitry Andric         auto *EHPad = TryToEHPad[EndToBegin[&MI]];
1680fe6060f1SDimitry Andric         EHPadStack.push_back(EHPad);
1681fe6060f1SDimitry Andric         break;
1682fe6060f1SDimitry Andric       }
1683fe6060f1SDimitry Andric 
16840b57cec5SDimitry Andric       case WebAssembly::END_LOOP:
1685fe6060f1SDimitry Andric         Stack.push_back(std::make_pair(EndToBegin[&MI]->getParent(), &MI));
1686fe6060f1SDimitry Andric         break;
1687fe6060f1SDimitry Andric 
1688fe6060f1SDimitry Andric       case WebAssembly::CATCH:
1689fe6060f1SDimitry Andric       case WebAssembly::CATCH_ALL:
1690fe6060f1SDimitry Andric         EHPadStack.pop_back();
1691fe6060f1SDimitry Andric         break;
1692fe6060f1SDimitry Andric 
1693fe6060f1SDimitry Andric       case WebAssembly::RETHROW:
1694fe6060f1SDimitry Andric         MI.getOperand(0).setImm(getRethrowDepth(Stack, EHPadStack));
16950b57cec5SDimitry Andric         break;
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric       default:
16980b57cec5SDimitry Andric         if (MI.isTerminator()) {
16990b57cec5SDimitry Andric           // Rewrite MBB operands to be depth immediates.
17000b57cec5SDimitry Andric           SmallVector<MachineOperand, 4> Ops(MI.operands());
17010b57cec5SDimitry Andric           while (MI.getNumOperands() > 0)
170281ad6265SDimitry Andric             MI.removeOperand(MI.getNumOperands() - 1);
17030b57cec5SDimitry Andric           for (auto MO : Ops) {
1704fe6060f1SDimitry Andric             if (MO.isMBB()) {
1705fe6060f1SDimitry Andric               if (MI.getOpcode() == WebAssembly::DELEGATE)
1706fe6060f1SDimitry Andric                 MO = MachineOperand::CreateImm(
1707fe6060f1SDimitry Andric                     getDelegateDepth(Stack, MO.getMBB()));
1708fe6060f1SDimitry Andric               else
1709fe6060f1SDimitry Andric                 MO = MachineOperand::CreateImm(
1710fe6060f1SDimitry Andric                     getBranchDepth(Stack, MO.getMBB()));
1711fe6060f1SDimitry Andric             }
17120b57cec5SDimitry Andric             MI.addOperand(MF, MO);
17130b57cec5SDimitry Andric           }
17140b57cec5SDimitry Andric         }
1715fe6060f1SDimitry Andric 
1716fe6060f1SDimitry Andric         if (MI.getOpcode() == WebAssembly::DELEGATE)
1717fe6060f1SDimitry Andric           Stack.push_back(std::make_pair(&MBB, &MI));
17180b57cec5SDimitry Andric         break;
17190b57cec5SDimitry Andric       }
17200b57cec5SDimitry Andric     }
17210b57cec5SDimitry Andric   }
17220b57cec5SDimitry Andric   assert(Stack.empty() && "Control flow should be balanced");
17230b57cec5SDimitry Andric }
17240b57cec5SDimitry Andric 
cleanupFunctionData(MachineFunction & MF)1725fe6060f1SDimitry Andric void WebAssemblyCFGStackify::cleanupFunctionData(MachineFunction &MF) {
1726fe6060f1SDimitry Andric   if (FakeCallerBB)
17270eae32dcSDimitry Andric     MF.deleteMachineBasicBlock(FakeCallerBB);
1728fe6060f1SDimitry Andric   AppendixBB = FakeCallerBB = nullptr;
1729fe6060f1SDimitry Andric }
1730fe6060f1SDimitry Andric 
releaseMemory()17310b57cec5SDimitry Andric void WebAssemblyCFGStackify::releaseMemory() {
17320b57cec5SDimitry Andric   ScopeTops.clear();
17330b57cec5SDimitry Andric   BeginToEnd.clear();
17340b57cec5SDimitry Andric   EndToBegin.clear();
17350b57cec5SDimitry Andric   TryToEHPad.clear();
17360b57cec5SDimitry Andric   EHPadToTry.clear();
17370b57cec5SDimitry Andric }
17380b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)17390b57cec5SDimitry Andric bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) {
17400b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** CFG Stackifying **********\n"
17410b57cec5SDimitry Andric                        "********** Function: "
17420b57cec5SDimitry Andric                     << MF.getName() << '\n');
17430b57cec5SDimitry Andric   const MCAsmInfo *MCAI = MF.getTarget().getMCAsmInfo();
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric   releaseMemory();
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric   // Liveness is not tracked for VALUE_STACK physreg.
17480b57cec5SDimitry Andric   MF.getRegInfo().invalidateLiveness();
17490b57cec5SDimitry Andric 
17500b57cec5SDimitry Andric   // Place the BLOCK/LOOP/TRY markers to indicate the beginnings of scopes.
17510b57cec5SDimitry Andric   placeMarkers(MF);
17520b57cec5SDimitry Andric 
17530b57cec5SDimitry Andric   // Remove unnecessary instructions possibly introduced by try/end_trys.
17540b57cec5SDimitry Andric   if (MCAI->getExceptionHandlingType() == ExceptionHandling::Wasm &&
17550b57cec5SDimitry Andric       MF.getFunction().hasPersonalityFn())
17560b57cec5SDimitry Andric     removeUnnecessaryInstrs(MF);
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric   // Convert MBB operands in terminators to relative depth immediates.
17590b57cec5SDimitry Andric   rewriteDepthImmediates(MF);
17600b57cec5SDimitry Andric 
17610b57cec5SDimitry Andric   // Fix up block/loop/try signatures at the end of the function to conform to
17620b57cec5SDimitry Andric   // WebAssembly's rules.
17630b57cec5SDimitry Andric   fixEndsAtEndOfFunction(MF);
17640b57cec5SDimitry Andric 
17650b57cec5SDimitry Andric   // Add an end instruction at the end of the function body.
17660b57cec5SDimitry Andric   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
17670b57cec5SDimitry Andric   if (!MF.getSubtarget<WebAssemblySubtarget>()
17680b57cec5SDimitry Andric            .getTargetTriple()
17690b57cec5SDimitry Andric            .isOSBinFormatELF())
17700b57cec5SDimitry Andric     appendEndToFunction(MF, TII);
17710b57cec5SDimitry Andric 
1772fe6060f1SDimitry Andric   cleanupFunctionData(MF);
1773fe6060f1SDimitry Andric 
17740b57cec5SDimitry Andric   MF.getInfo<WebAssemblyFunctionInfo>()->setCFGStackified();
17750b57cec5SDimitry Andric   return true;
17760b57cec5SDimitry Andric }
1777