17d523365SDimitry Andric //===-- WebAssemblyCFGStackify.cpp - CFG Stackification -------------------===//
27d523365SDimitry Andric //
37d523365SDimitry Andric //                     The LLVM Compiler Infrastructure
47d523365SDimitry Andric //
57d523365SDimitry Andric // This file is distributed under the University of Illinois Open Source
67d523365SDimitry Andric // License. See LICENSE.TXT for details.
77d523365SDimitry Andric //
87d523365SDimitry Andric //===----------------------------------------------------------------------===//
97d523365SDimitry Andric ///
107d523365SDimitry Andric /// \file
117d523365SDimitry Andric /// \brief This file implements a CFG stacking pass.
127d523365SDimitry Andric ///
133ca95b02SDimitry Andric /// This pass reorders the blocks in a function to put them into topological
143ca95b02SDimitry Andric /// order, ignoring loop backedges, and without any loop being interrupted
153ca95b02SDimitry Andric /// by a block not dominated by the loop header, with special care to keep the
163ca95b02SDimitry Andric /// order as similar as possible to the original order.
177d523365SDimitry Andric ///
187d523365SDimitry Andric /// Then, it inserts BLOCK and LOOP markers to mark the start of scopes, since
197d523365SDimitry Andric /// scope boundaries serve as the labels for WebAssembly's control transfers.
207d523365SDimitry Andric ///
217d523365SDimitry Andric /// This is sufficient to convert arbitrary CFGs into a form that works on
227d523365SDimitry Andric /// WebAssembly, provided that all loops are single-entry.
237d523365SDimitry Andric ///
247d523365SDimitry Andric //===----------------------------------------------------------------------===//
257d523365SDimitry Andric 
267d523365SDimitry Andric #include "WebAssembly.h"
277d523365SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
283ca95b02SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
297d523365SDimitry Andric #include "WebAssemblySubtarget.h"
303ca95b02SDimitry Andric #include "llvm/ADT/PriorityQueue.h"
317d523365SDimitry Andric #include "llvm/ADT/SetVector.h"
327d523365SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
337d523365SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
347d523365SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
357d523365SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
36444ed5c5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
377d523365SDimitry Andric #include "llvm/CodeGen/Passes.h"
387d523365SDimitry Andric #include "llvm/Support/Debug.h"
397d523365SDimitry Andric #include "llvm/Support/raw_ostream.h"
407d523365SDimitry Andric using namespace llvm;
417d523365SDimitry Andric 
427d523365SDimitry Andric #define DEBUG_TYPE "wasm-cfg-stackify"
437d523365SDimitry Andric 
447d523365SDimitry Andric namespace {
457d523365SDimitry Andric class WebAssemblyCFGStackify final : public MachineFunctionPass {
467d523365SDimitry Andric   const char *getPassName() const override {
477d523365SDimitry Andric     return "WebAssembly CFG Stackify";
487d523365SDimitry Andric   }
497d523365SDimitry Andric 
507d523365SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
517d523365SDimitry Andric     AU.setPreservesCFG();
527d523365SDimitry Andric     AU.addRequired<MachineDominatorTree>();
537d523365SDimitry Andric     AU.addPreserved<MachineDominatorTree>();
547d523365SDimitry Andric     AU.addRequired<MachineLoopInfo>();
557d523365SDimitry Andric     AU.addPreserved<MachineLoopInfo>();
567d523365SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
577d523365SDimitry Andric   }
587d523365SDimitry Andric 
597d523365SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
607d523365SDimitry Andric 
617d523365SDimitry Andric public:
627d523365SDimitry Andric   static char ID; // Pass identification, replacement for typeid
637d523365SDimitry Andric   WebAssemblyCFGStackify() : MachineFunctionPass(ID) {}
647d523365SDimitry Andric };
657d523365SDimitry Andric } // end anonymous namespace
667d523365SDimitry Andric 
677d523365SDimitry Andric char WebAssemblyCFGStackify::ID = 0;
687d523365SDimitry Andric FunctionPass *llvm::createWebAssemblyCFGStackify() {
697d523365SDimitry Andric   return new WebAssemblyCFGStackify();
707d523365SDimitry Andric }
717d523365SDimitry Andric 
727d523365SDimitry Andric /// Return the "bottom" block of a loop. This differs from
737d523365SDimitry Andric /// MachineLoop::getBottomBlock in that it works even if the loop is
747d523365SDimitry Andric /// discontiguous.
757d523365SDimitry Andric static MachineBasicBlock *LoopBottom(const MachineLoop *Loop) {
767d523365SDimitry Andric   MachineBasicBlock *Bottom = Loop->getHeader();
777d523365SDimitry Andric   for (MachineBasicBlock *MBB : Loop->blocks())
787d523365SDimitry Andric     if (MBB->getNumber() > Bottom->getNumber())
797d523365SDimitry Andric       Bottom = MBB;
807d523365SDimitry Andric   return Bottom;
817d523365SDimitry Andric }
827d523365SDimitry Andric 
833ca95b02SDimitry Andric static void MaybeUpdateTerminator(MachineBasicBlock *MBB) {
843ca95b02SDimitry Andric #ifndef NDEBUG
853ca95b02SDimitry Andric   bool AnyBarrier = false;
863ca95b02SDimitry Andric #endif
873ca95b02SDimitry Andric   bool AllAnalyzable = true;
883ca95b02SDimitry Andric   for (const MachineInstr &Term : MBB->terminators()) {
893ca95b02SDimitry Andric #ifndef NDEBUG
903ca95b02SDimitry Andric     AnyBarrier |= Term.isBarrier();
913ca95b02SDimitry Andric #endif
923ca95b02SDimitry Andric     AllAnalyzable &= Term.isBranch() && !Term.isIndirectBranch();
933ca95b02SDimitry Andric   }
943ca95b02SDimitry Andric   assert((AnyBarrier || AllAnalyzable) &&
953ca95b02SDimitry Andric          "AnalyzeBranch needs to analyze any block with a fallthrough");
963ca95b02SDimitry Andric   if (AllAnalyzable)
973ca95b02SDimitry Andric     MBB->updateTerminator();
983ca95b02SDimitry Andric }
997d523365SDimitry Andric 
1003ca95b02SDimitry Andric namespace {
1013ca95b02SDimitry Andric /// Sort blocks by their number.
1023ca95b02SDimitry Andric struct CompareBlockNumbers {
1033ca95b02SDimitry Andric   bool operator()(const MachineBasicBlock *A,
1043ca95b02SDimitry Andric                   const MachineBasicBlock *B) const {
1053ca95b02SDimitry Andric     return A->getNumber() > B->getNumber();
1063ca95b02SDimitry Andric   }
1073ca95b02SDimitry Andric };
1083ca95b02SDimitry Andric /// Sort blocks by their number in the opposite order..
1093ca95b02SDimitry Andric struct CompareBlockNumbersBackwards {
1103ca95b02SDimitry Andric   bool operator()(const MachineBasicBlock *A,
1113ca95b02SDimitry Andric                   const MachineBasicBlock *B) const {
1123ca95b02SDimitry Andric     return A->getNumber() < B->getNumber();
1133ca95b02SDimitry Andric   }
1143ca95b02SDimitry Andric };
1153ca95b02SDimitry Andric /// Bookkeeping for a loop to help ensure that we don't mix blocks not dominated
1163ca95b02SDimitry Andric /// by the loop header among the loop's blocks.
1173ca95b02SDimitry Andric struct Entry {
1183ca95b02SDimitry Andric   const MachineLoop *Loop;
1193ca95b02SDimitry Andric   unsigned NumBlocksLeft;
1207d523365SDimitry Andric 
1213ca95b02SDimitry Andric   /// List of blocks not dominated by Loop's header that are deferred until
1223ca95b02SDimitry Andric   /// after all of Loop's blocks have been seen.
1233ca95b02SDimitry Andric   std::vector<MachineBasicBlock *> Deferred;
1243ca95b02SDimitry Andric 
1253ca95b02SDimitry Andric   explicit Entry(const MachineLoop *L)
1263ca95b02SDimitry Andric       : Loop(L), NumBlocksLeft(L->getNumBlocks()) {}
1273ca95b02SDimitry Andric };
1283ca95b02SDimitry Andric }
1293ca95b02SDimitry Andric 
1303ca95b02SDimitry Andric /// Sort the blocks, taking special care to make sure that loops are not
1313ca95b02SDimitry Andric /// interrupted by blocks not dominated by their header.
1323ca95b02SDimitry Andric /// TODO: There are many opportunities for improving the heuristics here.
1333ca95b02SDimitry Andric /// Explore them.
1343ca95b02SDimitry Andric static void SortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
1353ca95b02SDimitry Andric                        const MachineDominatorTree &MDT) {
1363ca95b02SDimitry Andric   // Prepare for a topological sort: Record the number of predecessors each
1373ca95b02SDimitry Andric   // block has, ignoring loop backedges.
1383ca95b02SDimitry Andric   MF.RenumberBlocks();
1393ca95b02SDimitry Andric   SmallVector<unsigned, 16> NumPredsLeft(MF.getNumBlockIDs(), 0);
1403ca95b02SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
1413ca95b02SDimitry Andric     unsigned N = MBB.pred_size();
1423ca95b02SDimitry Andric     if (MachineLoop *L = MLI.getLoopFor(&MBB))
1433ca95b02SDimitry Andric       if (L->getHeader() == &MBB)
1443ca95b02SDimitry Andric         for (const MachineBasicBlock *Pred : MBB.predecessors())
1453ca95b02SDimitry Andric           if (L->contains(Pred))
1463ca95b02SDimitry Andric             --N;
1473ca95b02SDimitry Andric     NumPredsLeft[MBB.getNumber()] = N;
1483ca95b02SDimitry Andric   }
1493ca95b02SDimitry Andric 
1503ca95b02SDimitry Andric   // Topological sort the CFG, with additional constraints:
1513ca95b02SDimitry Andric   //  - Between a loop header and the last block in the loop, there can be
1523ca95b02SDimitry Andric   //    no blocks not dominated by the loop header.
1533ca95b02SDimitry Andric   //  - It's desirable to preserve the original block order when possible.
1543ca95b02SDimitry Andric   // We use two ready lists; Preferred and Ready. Preferred has recently
1553ca95b02SDimitry Andric   // processed sucessors, to help preserve block sequences from the original
1563ca95b02SDimitry Andric   // order. Ready has the remaining ready blocks.
1573ca95b02SDimitry Andric   PriorityQueue<MachineBasicBlock *, std::vector<MachineBasicBlock *>,
1583ca95b02SDimitry Andric                 CompareBlockNumbers>
1593ca95b02SDimitry Andric       Preferred;
1603ca95b02SDimitry Andric   PriorityQueue<MachineBasicBlock *, std::vector<MachineBasicBlock *>,
1613ca95b02SDimitry Andric                 CompareBlockNumbersBackwards>
1623ca95b02SDimitry Andric       Ready;
1633ca95b02SDimitry Andric   SmallVector<Entry, 4> Loops;
1643ca95b02SDimitry Andric   for (MachineBasicBlock *MBB = &MF.front();;) {
1653ca95b02SDimitry Andric     const MachineLoop *L = MLI.getLoopFor(MBB);
1663ca95b02SDimitry Andric     if (L) {
1673ca95b02SDimitry Andric       // If MBB is a loop header, add it to the active loop list. We can't put
1683ca95b02SDimitry Andric       // any blocks that it doesn't dominate until we see the end of the loop.
1693ca95b02SDimitry Andric       if (L->getHeader() == MBB)
1703ca95b02SDimitry Andric         Loops.push_back(Entry(L));
1713ca95b02SDimitry Andric       // For each active loop the block is in, decrement the count. If MBB is
1723ca95b02SDimitry Andric       // the last block in an active loop, take it off the list and pick up any
1733ca95b02SDimitry Andric       // blocks deferred because the header didn't dominate them.
1743ca95b02SDimitry Andric       for (Entry &E : Loops)
1753ca95b02SDimitry Andric         if (E.Loop->contains(MBB) && --E.NumBlocksLeft == 0)
1763ca95b02SDimitry Andric           for (auto DeferredBlock : E.Deferred)
1773ca95b02SDimitry Andric             Ready.push(DeferredBlock);
1783ca95b02SDimitry Andric       while (!Loops.empty() && Loops.back().NumBlocksLeft == 0)
1793ca95b02SDimitry Andric         Loops.pop_back();
1803ca95b02SDimitry Andric     }
1813ca95b02SDimitry Andric     // The main topological sort logic.
1823ca95b02SDimitry Andric     for (MachineBasicBlock *Succ : MBB->successors()) {
1833ca95b02SDimitry Andric       // Ignore backedges.
1843ca95b02SDimitry Andric       if (MachineLoop *SuccL = MLI.getLoopFor(Succ))
1853ca95b02SDimitry Andric         if (SuccL->getHeader() == Succ && SuccL->contains(MBB))
1863ca95b02SDimitry Andric           continue;
1873ca95b02SDimitry Andric       // Decrement the predecessor count. If it's now zero, it's ready.
1883ca95b02SDimitry Andric       if (--NumPredsLeft[Succ->getNumber()] == 0)
1893ca95b02SDimitry Andric         Preferred.push(Succ);
1903ca95b02SDimitry Andric     }
1913ca95b02SDimitry Andric     // Determine the block to follow MBB. First try to find a preferred block,
1923ca95b02SDimitry Andric     // to preserve the original block order when possible.
1933ca95b02SDimitry Andric     MachineBasicBlock *Next = nullptr;
1943ca95b02SDimitry Andric     while (!Preferred.empty()) {
1953ca95b02SDimitry Andric       Next = Preferred.top();
1963ca95b02SDimitry Andric       Preferred.pop();
1973ca95b02SDimitry Andric       // If X isn't dominated by the top active loop header, defer it until that
1983ca95b02SDimitry Andric       // loop is done.
1993ca95b02SDimitry Andric       if (!Loops.empty() &&
2003ca95b02SDimitry Andric           !MDT.dominates(Loops.back().Loop->getHeader(), Next)) {
2013ca95b02SDimitry Andric         Loops.back().Deferred.push_back(Next);
2023ca95b02SDimitry Andric         Next = nullptr;
2037d523365SDimitry Andric         continue;
2047d523365SDimitry Andric       }
2053ca95b02SDimitry Andric       // If Next was originally ordered before MBB, and it isn't because it was
2063ca95b02SDimitry Andric       // loop-rotated above the header, it's not preferred.
2073ca95b02SDimitry Andric       if (Next->getNumber() < MBB->getNumber() &&
2083ca95b02SDimitry Andric           (!L || !L->contains(Next) ||
2093ca95b02SDimitry Andric            L->getHeader()->getNumber() < Next->getNumber())) {
2103ca95b02SDimitry Andric         Ready.push(Next);
2113ca95b02SDimitry Andric         Next = nullptr;
2123ca95b02SDimitry Andric         continue;
2133ca95b02SDimitry Andric       }
2147d523365SDimitry Andric       break;
2157d523365SDimitry Andric     }
2163ca95b02SDimitry Andric     // If we didn't find a suitable block in the Preferred list, check the
2173ca95b02SDimitry Andric     // general Ready list.
2183ca95b02SDimitry Andric     if (!Next) {
2193ca95b02SDimitry Andric       // If there are no more blocks to process, we're done.
2203ca95b02SDimitry Andric       if (Ready.empty()) {
2213ca95b02SDimitry Andric         MaybeUpdateTerminator(MBB);
2223ca95b02SDimitry Andric         break;
2233ca95b02SDimitry Andric       }
2243ca95b02SDimitry Andric       for (;;) {
2253ca95b02SDimitry Andric         Next = Ready.top();
2263ca95b02SDimitry Andric         Ready.pop();
2273ca95b02SDimitry Andric         // If Next isn't dominated by the top active loop header, defer it until
2283ca95b02SDimitry Andric         // that loop is done.
2293ca95b02SDimitry Andric         if (!Loops.empty() &&
2303ca95b02SDimitry Andric             !MDT.dominates(Loops.back().Loop->getHeader(), Next)) {
2313ca95b02SDimitry Andric           Loops.back().Deferred.push_back(Next);
2323ca95b02SDimitry Andric           continue;
2333ca95b02SDimitry Andric         }
2343ca95b02SDimitry Andric         break;
2353ca95b02SDimitry Andric       }
2363ca95b02SDimitry Andric     }
2373ca95b02SDimitry Andric     // Move the next block into place and iterate.
2383ca95b02SDimitry Andric     Next->moveAfter(MBB);
2393ca95b02SDimitry Andric     MaybeUpdateTerminator(MBB);
2403ca95b02SDimitry Andric     MBB = Next;
2413ca95b02SDimitry Andric   }
2423ca95b02SDimitry Andric   assert(Loops.empty() && "Active loop list not finished");
2437d523365SDimitry Andric   MF.RenumberBlocks();
2447d523365SDimitry Andric 
2457d523365SDimitry Andric #ifndef NDEBUG
2467d523365SDimitry Andric   SmallSetVector<MachineLoop *, 8> OnStack;
2477d523365SDimitry Andric 
2487d523365SDimitry Andric   // Insert a sentinel representing the degenerate loop that starts at the
2497d523365SDimitry Andric   // function entry block and includes the entire function as a "loop" that
2507d523365SDimitry Andric   // executes once.
2517d523365SDimitry Andric   OnStack.insert(nullptr);
2527d523365SDimitry Andric 
2537d523365SDimitry Andric   for (auto &MBB : MF) {
2547d523365SDimitry Andric     assert(MBB.getNumber() >= 0 && "Renumbered blocks should be non-negative.");
2557d523365SDimitry Andric 
2567d523365SDimitry Andric     MachineLoop *Loop = MLI.getLoopFor(&MBB);
2577d523365SDimitry Andric     if (Loop && &MBB == Loop->getHeader()) {
2587d523365SDimitry Andric       // Loop header. The loop predecessor should be sorted above, and the other
2597d523365SDimitry Andric       // predecessors should be backedges below.
2607d523365SDimitry Andric       for (auto Pred : MBB.predecessors())
2617d523365SDimitry Andric         assert(
2627d523365SDimitry Andric             (Pred->getNumber() < MBB.getNumber() || Loop->contains(Pred)) &&
2637d523365SDimitry Andric             "Loop header predecessors must be loop predecessors or backedges");
2647d523365SDimitry Andric       assert(OnStack.insert(Loop) && "Loops should be declared at most once.");
2657d523365SDimitry Andric     } else {
2667d523365SDimitry Andric       // Not a loop header. All predecessors should be sorted above.
2677d523365SDimitry Andric       for (auto Pred : MBB.predecessors())
2687d523365SDimitry Andric         assert(Pred->getNumber() < MBB.getNumber() &&
2697d523365SDimitry Andric                "Non-loop-header predecessors should be topologically sorted");
2707d523365SDimitry Andric       assert(OnStack.count(MLI.getLoopFor(&MBB)) &&
2717d523365SDimitry Andric              "Blocks must be nested in their loops");
2727d523365SDimitry Andric     }
2737d523365SDimitry Andric     while (OnStack.size() > 1 && &MBB == LoopBottom(OnStack.back()))
2747d523365SDimitry Andric       OnStack.pop_back();
2757d523365SDimitry Andric   }
2767d523365SDimitry Andric   assert(OnStack.pop_back_val() == nullptr &&
2777d523365SDimitry Andric          "The function entry block shouldn't actually be a loop header");
2787d523365SDimitry Andric   assert(OnStack.empty() &&
2797d523365SDimitry Andric          "Control flow stack pushes and pops should be balanced.");
2807d523365SDimitry Andric #endif
2817d523365SDimitry Andric }
2827d523365SDimitry Andric 
2837d523365SDimitry Andric /// Test whether Pred has any terminators explicitly branching to MBB, as
2847d523365SDimitry Andric /// opposed to falling through. Note that it's possible (eg. in unoptimized
2857d523365SDimitry Andric /// code) for a branch instruction to both branch to a block and fallthrough
2867d523365SDimitry Andric /// to it, so we check the actual branch operands to see if there are any
2877d523365SDimitry Andric /// explicit mentions.
288444ed5c5SDimitry Andric static bool ExplicitlyBranchesTo(MachineBasicBlock *Pred,
289444ed5c5SDimitry Andric                                  MachineBasicBlock *MBB) {
2907d523365SDimitry Andric   for (MachineInstr &MI : Pred->terminators())
2917d523365SDimitry Andric     for (MachineOperand &MO : MI.explicit_operands())
2927d523365SDimitry Andric       if (MO.isMBB() && MO.getMBB() == MBB)
2937d523365SDimitry Andric         return true;
2947d523365SDimitry Andric   return false;
2957d523365SDimitry Andric }
2967d523365SDimitry Andric 
2973ca95b02SDimitry Andric /// Test whether MI is a child of some other node in an expression tree.
2983ca95b02SDimitry Andric static bool IsChild(const MachineInstr &MI,
2993ca95b02SDimitry Andric                     const WebAssemblyFunctionInfo &MFI) {
3003ca95b02SDimitry Andric   if (MI.getNumOperands() == 0)
3013ca95b02SDimitry Andric     return false;
3023ca95b02SDimitry Andric   const MachineOperand &MO = MI.getOperand(0);
3033ca95b02SDimitry Andric   if (!MO.isReg() || MO.isImplicit() || !MO.isDef())
3043ca95b02SDimitry Andric     return false;
3053ca95b02SDimitry Andric   unsigned Reg = MO.getReg();
3063ca95b02SDimitry Andric   return TargetRegisterInfo::isVirtualRegister(Reg) &&
3073ca95b02SDimitry Andric          MFI.isVRegStackified(Reg);
3083ca95b02SDimitry Andric }
3093ca95b02SDimitry Andric 
3107d523365SDimitry Andric /// Insert a BLOCK marker for branches to MBB (if needed).
3117d523365SDimitry Andric static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF,
3127d523365SDimitry Andric                              SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
3137d523365SDimitry Andric                              const WebAssemblyInstrInfo &TII,
3147d523365SDimitry Andric                              const MachineLoopInfo &MLI,
3153ca95b02SDimitry Andric                              MachineDominatorTree &MDT,
3163ca95b02SDimitry Andric                              WebAssemblyFunctionInfo &MFI) {
3177d523365SDimitry Andric   // First compute the nearest common dominator of all forward non-fallthrough
3187d523365SDimitry Andric   // predecessors so that we minimize the time that the BLOCK is on the stack,
3197d523365SDimitry Andric   // which reduces overall stack height.
3207d523365SDimitry Andric   MachineBasicBlock *Header = nullptr;
3217d523365SDimitry Andric   bool IsBranchedTo = false;
3227d523365SDimitry Andric   int MBBNumber = MBB.getNumber();
3237d523365SDimitry Andric   for (MachineBasicBlock *Pred : MBB.predecessors())
3247d523365SDimitry Andric     if (Pred->getNumber() < MBBNumber) {
3257d523365SDimitry Andric       Header = Header ? MDT.findNearestCommonDominator(Header, Pred) : Pred;
3267d523365SDimitry Andric       if (ExplicitlyBranchesTo(Pred, &MBB))
3277d523365SDimitry Andric         IsBranchedTo = true;
3287d523365SDimitry Andric     }
3297d523365SDimitry Andric   if (!Header)
3307d523365SDimitry Andric     return;
3317d523365SDimitry Andric   if (!IsBranchedTo)
3327d523365SDimitry Andric     return;
3337d523365SDimitry Andric 
3347d523365SDimitry Andric   assert(&MBB != &MF.front() && "Header blocks shouldn't have predecessors");
3357d523365SDimitry Andric   MachineBasicBlock *LayoutPred = &*prev(MachineFunction::iterator(&MBB));
3367d523365SDimitry Andric 
3377d523365SDimitry Andric   // If the nearest common dominator is inside a more deeply nested context,
3387d523365SDimitry Andric   // walk out to the nearest scope which isn't more deeply nested.
3397d523365SDimitry Andric   for (MachineFunction::iterator I(LayoutPred), E(Header); I != E; --I) {
3407d523365SDimitry Andric     if (MachineBasicBlock *ScopeTop = ScopeTops[I->getNumber()]) {
3417d523365SDimitry Andric       if (ScopeTop->getNumber() > Header->getNumber()) {
3427d523365SDimitry Andric         // Skip over an intervening scope.
3437d523365SDimitry Andric         I = next(MachineFunction::iterator(ScopeTop));
3447d523365SDimitry Andric       } else {
3457d523365SDimitry Andric         // We found a scope level at an appropriate depth.
3467d523365SDimitry Andric         Header = ScopeTop;
3477d523365SDimitry Andric         break;
3487d523365SDimitry Andric       }
3497d523365SDimitry Andric     }
3507d523365SDimitry Andric   }
3517d523365SDimitry Andric 
3527d523365SDimitry Andric   // If there's a loop which ends just before MBB which contains Header, we can
3537d523365SDimitry Andric   // reuse its label instead of inserting a new BLOCK.
3547d523365SDimitry Andric   for (MachineLoop *Loop = MLI.getLoopFor(LayoutPred);
3557d523365SDimitry Andric        Loop && Loop->contains(LayoutPred); Loop = Loop->getParentLoop())
3567d523365SDimitry Andric     if (Loop && LoopBottom(Loop) == LayoutPred && Loop->contains(Header))
3577d523365SDimitry Andric       return;
3587d523365SDimitry Andric 
3597d523365SDimitry Andric   // Decide where in Header to put the BLOCK.
3607d523365SDimitry Andric   MachineBasicBlock::iterator InsertPos;
3617d523365SDimitry Andric   MachineLoop *HeaderLoop = MLI.getLoopFor(Header);
3627d523365SDimitry Andric   if (HeaderLoop && MBB.getNumber() > LoopBottom(HeaderLoop)->getNumber()) {
3637d523365SDimitry Andric     // Header is the header of a loop that does not lexically contain MBB, so
3643ca95b02SDimitry Andric     // the BLOCK needs to be above the LOOP, after any END constructs.
3657d523365SDimitry Andric     InsertPos = Header->begin();
3663ca95b02SDimitry Andric     while (InsertPos->getOpcode() != WebAssembly::LOOP)
3673ca95b02SDimitry Andric       ++InsertPos;
3687d523365SDimitry Andric   } else {
3697d523365SDimitry Andric     // Otherwise, insert the BLOCK as late in Header as we can, but before the
3707d523365SDimitry Andric     // beginning of the local expression tree and any nested BLOCKs.
3717d523365SDimitry Andric     InsertPos = Header->getFirstTerminator();
3723ca95b02SDimitry Andric     while (InsertPos != Header->begin() && IsChild(*prev(InsertPos), MFI) &&
373444ed5c5SDimitry Andric            prev(InsertPos)->getOpcode() != WebAssembly::LOOP &&
374444ed5c5SDimitry Andric            prev(InsertPos)->getOpcode() != WebAssembly::END_BLOCK &&
375444ed5c5SDimitry Andric            prev(InsertPos)->getOpcode() != WebAssembly::END_LOOP)
3767d523365SDimitry Andric       --InsertPos;
3777d523365SDimitry Andric   }
3787d523365SDimitry Andric 
3797d523365SDimitry Andric   // Add the BLOCK.
380444ed5c5SDimitry Andric   BuildMI(*Header, InsertPos, DebugLoc(), TII.get(WebAssembly::BLOCK));
381444ed5c5SDimitry Andric 
382444ed5c5SDimitry Andric   // Mark the end of the block.
383444ed5c5SDimitry Andric   InsertPos = MBB.begin();
384444ed5c5SDimitry Andric   while (InsertPos != MBB.end() &&
385444ed5c5SDimitry Andric          InsertPos->getOpcode() == WebAssembly::END_LOOP)
386444ed5c5SDimitry Andric     ++InsertPos;
387444ed5c5SDimitry Andric   BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::END_BLOCK));
3887d523365SDimitry Andric 
3897d523365SDimitry Andric   // Track the farthest-spanning scope that ends at this point.
3907d523365SDimitry Andric   int Number = MBB.getNumber();
3917d523365SDimitry Andric   if (!ScopeTops[Number] ||
3927d523365SDimitry Andric       ScopeTops[Number]->getNumber() > Header->getNumber())
3937d523365SDimitry Andric     ScopeTops[Number] = Header;
3947d523365SDimitry Andric }
3957d523365SDimitry Andric 
3967d523365SDimitry Andric /// Insert a LOOP marker for a loop starting at MBB (if it's a loop header).
397444ed5c5SDimitry Andric static void PlaceLoopMarker(
398444ed5c5SDimitry Andric     MachineBasicBlock &MBB, MachineFunction &MF,
3997d523365SDimitry Andric     SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
400444ed5c5SDimitry Andric     DenseMap<const MachineInstr *, const MachineBasicBlock *> &LoopTops,
401444ed5c5SDimitry Andric     const WebAssemblyInstrInfo &TII, const MachineLoopInfo &MLI) {
4027d523365SDimitry Andric   MachineLoop *Loop = MLI.getLoopFor(&MBB);
4037d523365SDimitry Andric   if (!Loop || Loop->getHeader() != &MBB)
4047d523365SDimitry Andric     return;
4057d523365SDimitry Andric 
4067d523365SDimitry Andric   // The operand of a LOOP is the first block after the loop. If the loop is the
4077d523365SDimitry Andric   // bottom of the function, insert a dummy block at the end.
4087d523365SDimitry Andric   MachineBasicBlock *Bottom = LoopBottom(Loop);
4097d523365SDimitry Andric   auto Iter = next(MachineFunction::iterator(Bottom));
4107d523365SDimitry Andric   if (Iter == MF.end()) {
4117d523365SDimitry Andric     MachineBasicBlock *Label = MF.CreateMachineBasicBlock();
4127d523365SDimitry Andric     // Give it a fake predecessor so that AsmPrinter prints its label.
4137d523365SDimitry Andric     Label->addSuccessor(Label);
4147d523365SDimitry Andric     MF.push_back(Label);
4157d523365SDimitry Andric     Iter = next(MachineFunction::iterator(Bottom));
4167d523365SDimitry Andric   }
4177d523365SDimitry Andric   MachineBasicBlock *AfterLoop = &*Iter;
4187d523365SDimitry Andric 
419444ed5c5SDimitry Andric   // Mark the beginning of the loop (after the end of any existing loop that
420444ed5c5SDimitry Andric   // ends here).
421444ed5c5SDimitry Andric   auto InsertPos = MBB.begin();
422444ed5c5SDimitry Andric   while (InsertPos != MBB.end() &&
423444ed5c5SDimitry Andric          InsertPos->getOpcode() == WebAssembly::END_LOOP)
424444ed5c5SDimitry Andric     ++InsertPos;
425444ed5c5SDimitry Andric   BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::LOOP));
426444ed5c5SDimitry Andric 
427444ed5c5SDimitry Andric   // Mark the end of the loop.
428444ed5c5SDimitry Andric   MachineInstr *End = BuildMI(*AfterLoop, AfterLoop->begin(), DebugLoc(),
429444ed5c5SDimitry Andric                               TII.get(WebAssembly::END_LOOP));
430444ed5c5SDimitry Andric   LoopTops[End] = &MBB;
4317d523365SDimitry Andric 
4327d523365SDimitry Andric   assert((!ScopeTops[AfterLoop->getNumber()] ||
4337d523365SDimitry Andric           ScopeTops[AfterLoop->getNumber()]->getNumber() < MBB.getNumber()) &&
4343ca95b02SDimitry Andric          "With block sorting the outermost loop for a block should be first.");
4357d523365SDimitry Andric   if (!ScopeTops[AfterLoop->getNumber()])
4367d523365SDimitry Andric     ScopeTops[AfterLoop->getNumber()] = &MBB;
4377d523365SDimitry Andric }
4387d523365SDimitry Andric 
439444ed5c5SDimitry Andric static unsigned
440444ed5c5SDimitry Andric GetDepth(const SmallVectorImpl<const MachineBasicBlock *> &Stack,
441444ed5c5SDimitry Andric          const MachineBasicBlock *MBB) {
442444ed5c5SDimitry Andric   unsigned Depth = 0;
443444ed5c5SDimitry Andric   for (auto X : reverse(Stack)) {
444444ed5c5SDimitry Andric     if (X == MBB)
445444ed5c5SDimitry Andric       break;
446444ed5c5SDimitry Andric     ++Depth;
447444ed5c5SDimitry Andric   }
448444ed5c5SDimitry Andric   assert(Depth < Stack.size() && "Branch destination should be in scope");
449444ed5c5SDimitry Andric   return Depth;
450444ed5c5SDimitry Andric }
451444ed5c5SDimitry Andric 
4527d523365SDimitry Andric /// Insert LOOP and BLOCK markers at appropriate places.
4537d523365SDimitry Andric static void PlaceMarkers(MachineFunction &MF, const MachineLoopInfo &MLI,
4547d523365SDimitry Andric                          const WebAssemblyInstrInfo &TII,
4553ca95b02SDimitry Andric                          MachineDominatorTree &MDT,
4563ca95b02SDimitry Andric                          WebAssemblyFunctionInfo &MFI) {
4577d523365SDimitry Andric   // For each block whose label represents the end of a scope, record the block
4587d523365SDimitry Andric   // which holds the beginning of the scope. This will allow us to quickly skip
4597d523365SDimitry Andric   // over scoped regions when walking blocks. We allocate one more than the
4607d523365SDimitry Andric   // number of blocks in the function to accommodate for the possible fake block
4617d523365SDimitry Andric   // we may insert at the end.
4627d523365SDimitry Andric   SmallVector<MachineBasicBlock *, 8> ScopeTops(MF.getNumBlockIDs() + 1);
4637d523365SDimitry Andric 
464444ed5c5SDimitry Andric   // For eacn LOOP_END, the corresponding LOOP.
465444ed5c5SDimitry Andric   DenseMap<const MachineInstr *, const MachineBasicBlock *> LoopTops;
466444ed5c5SDimitry Andric 
4677d523365SDimitry Andric   for (auto &MBB : MF) {
4687d523365SDimitry Andric     // Place the LOOP for MBB if MBB is the header of a loop.
469444ed5c5SDimitry Andric     PlaceLoopMarker(MBB, MF, ScopeTops, LoopTops, TII, MLI);
4707d523365SDimitry Andric 
4717d523365SDimitry Andric     // Place the BLOCK for MBB if MBB is branched to from above.
4723ca95b02SDimitry Andric     PlaceBlockMarker(MBB, MF, ScopeTops, TII, MLI, MDT, MFI);
4737d523365SDimitry Andric   }
4747d523365SDimitry Andric 
475444ed5c5SDimitry Andric   // Now rewrite references to basic blocks to be depth immediates.
476444ed5c5SDimitry Andric   SmallVector<const MachineBasicBlock *, 8> Stack;
477444ed5c5SDimitry Andric   for (auto &MBB : reverse(MF)) {
478444ed5c5SDimitry Andric     for (auto &MI : reverse(MBB)) {
479444ed5c5SDimitry Andric       switch (MI.getOpcode()) {
480444ed5c5SDimitry Andric       case WebAssembly::BLOCK:
481444ed5c5SDimitry Andric         assert(ScopeTops[Stack.back()->getNumber()] == &MBB &&
482444ed5c5SDimitry Andric                "Block should be balanced");
483444ed5c5SDimitry Andric         Stack.pop_back();
484444ed5c5SDimitry Andric         break;
485444ed5c5SDimitry Andric       case WebAssembly::LOOP:
486444ed5c5SDimitry Andric         assert(Stack.back() == &MBB && "Loop top should be balanced");
487444ed5c5SDimitry Andric         Stack.pop_back();
488444ed5c5SDimitry Andric         Stack.pop_back();
489444ed5c5SDimitry Andric         break;
490444ed5c5SDimitry Andric       case WebAssembly::END_BLOCK:
491444ed5c5SDimitry Andric         Stack.push_back(&MBB);
492444ed5c5SDimitry Andric         break;
493444ed5c5SDimitry Andric       case WebAssembly::END_LOOP:
494444ed5c5SDimitry Andric         Stack.push_back(&MBB);
495444ed5c5SDimitry Andric         Stack.push_back(LoopTops[&MI]);
496444ed5c5SDimitry Andric         break;
497444ed5c5SDimitry Andric       default:
498444ed5c5SDimitry Andric         if (MI.isTerminator()) {
499444ed5c5SDimitry Andric           // Rewrite MBB operands to be depth immediates.
500444ed5c5SDimitry Andric           SmallVector<MachineOperand, 4> Ops(MI.operands());
501444ed5c5SDimitry Andric           while (MI.getNumOperands() > 0)
502444ed5c5SDimitry Andric             MI.RemoveOperand(MI.getNumOperands() - 1);
503444ed5c5SDimitry Andric           for (auto MO : Ops) {
504444ed5c5SDimitry Andric             if (MO.isMBB())
505444ed5c5SDimitry Andric               MO = MachineOperand::CreateImm(GetDepth(Stack, MO.getMBB()));
506444ed5c5SDimitry Andric             MI.addOperand(MF, MO);
5077d523365SDimitry Andric           }
508444ed5c5SDimitry Andric         }
509444ed5c5SDimitry Andric         break;
510444ed5c5SDimitry Andric       }
511444ed5c5SDimitry Andric     }
512444ed5c5SDimitry Andric   }
513444ed5c5SDimitry Andric   assert(Stack.empty() && "Control flow should be balanced");
514444ed5c5SDimitry Andric }
5157d523365SDimitry Andric 
5167d523365SDimitry Andric bool WebAssemblyCFGStackify::runOnMachineFunction(MachineFunction &MF) {
5177d523365SDimitry Andric   DEBUG(dbgs() << "********** CFG Stackifying **********\n"
5187d523365SDimitry Andric                   "********** Function: "
5197d523365SDimitry Andric                << MF.getName() << '\n');
5207d523365SDimitry Andric 
5217d523365SDimitry Andric   const auto &MLI = getAnalysis<MachineLoopInfo>();
5227d523365SDimitry Andric   auto &MDT = getAnalysis<MachineDominatorTree>();
523444ed5c5SDimitry Andric   // Liveness is not tracked for EXPR_STACK physreg.
5247d523365SDimitry Andric   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
5253ca95b02SDimitry Andric   WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
526444ed5c5SDimitry Andric   MF.getRegInfo().invalidateLiveness();
5277d523365SDimitry Andric 
5283ca95b02SDimitry Andric   // Sort the blocks, with contiguous loops.
5293ca95b02SDimitry Andric   SortBlocks(MF, MLI, MDT);
5307d523365SDimitry Andric 
5317d523365SDimitry Andric   // Place the BLOCK and LOOP markers to indicate the beginnings of scopes.
5323ca95b02SDimitry Andric   PlaceMarkers(MF, MLI, TII, MDT, MFI);
5337d523365SDimitry Andric 
5347d523365SDimitry Andric   return true;
5357d523365SDimitry Andric }
536