xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyExceptionInfo.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- WebAssemblyExceptionInfo.h - WebAssembly Exception Info -*- C++ -*-===//
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 /// \brief This file implements WebAssemblyException information analysis.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYEXCEPTIONINFO_H
150b57cec5SDimitry Andric #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYEXCEPTIONINFO_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "WebAssembly.h"
18*5f757f3fSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric namespace llvm {
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric class MachineDominatorTree;
240b57cec5SDimitry Andric class MachineDominanceFrontier;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric // WebAssembly instructions for exception handling are structured as follows:
270b57cec5SDimitry Andric //   try
280b57cec5SDimitry Andric //     instructions*
290b57cec5SDimitry Andric //   catch             ----|
300b57cec5SDimitry Andric //     instructions*       | -> A WebAssemblyException consists of this region
310b57cec5SDimitry Andric //   end               ----|
320b57cec5SDimitry Andric //
330b57cec5SDimitry Andric // A WebAssemblyException object contains BBs that belong to a 'catch' part of
340b57cec5SDimitry Andric // the try-catch-end structure to be created later. 'try' and 'end' markers
350b57cec5SDimitry Andric // are not present at this stage and will be generated in CFGStackify pass.
360b57cec5SDimitry Andric // Because CFGSort requires all the BBs within a catch part to be sorted
370b57cec5SDimitry Andric // together as it does for loops, this pass calculates the nesting structure of
380b57cec5SDimitry Andric // catch part of exceptions in a function.
390b57cec5SDimitry Andric //
400b57cec5SDimitry Andric // An exception catch part is defined as a BB with catch instruction and all
410b57cec5SDimitry Andric // other BBs dominated by this BB.
420b57cec5SDimitry Andric class WebAssemblyException {
430b57cec5SDimitry Andric   MachineBasicBlock *EHPad = nullptr;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   WebAssemblyException *ParentException = nullptr;
465ffd83dbSDimitry Andric   std::vector<std::unique_ptr<WebAssemblyException>> SubExceptions;
470b57cec5SDimitry Andric   std::vector<MachineBasicBlock *> Blocks;
48fe6060f1SDimitry Andric   SmallPtrSet<MachineBasicBlock *, 8> BlockSet;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric public:
510b57cec5SDimitry Andric   WebAssemblyException(MachineBasicBlock *EHPad) : EHPad(EHPad) {}
520b57cec5SDimitry Andric   WebAssemblyException(const WebAssemblyException &) = delete;
530b57cec5SDimitry Andric   const WebAssemblyException &operator=(const WebAssemblyException &) = delete;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   MachineBasicBlock *getEHPad() const { return EHPad; }
560b57cec5SDimitry Andric   MachineBasicBlock *getHeader() const { return EHPad; }
570b57cec5SDimitry Andric   WebAssemblyException *getParentException() const { return ParentException; }
580b57cec5SDimitry Andric   void setParentException(WebAssemblyException *WE) { ParentException = WE; }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   bool contains(const WebAssemblyException *WE) const {
610b57cec5SDimitry Andric     if (WE == this)
620b57cec5SDimitry Andric       return true;
630b57cec5SDimitry Andric     if (!WE)
640b57cec5SDimitry Andric       return false;
650b57cec5SDimitry Andric     return contains(WE->getParentException());
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric   bool contains(const MachineBasicBlock *MBB) const {
680b57cec5SDimitry Andric     return BlockSet.count(MBB);
690b57cec5SDimitry Andric   }
700b57cec5SDimitry Andric 
71fe6060f1SDimitry Andric   void addToBlocksSet(MachineBasicBlock *MBB) { BlockSet.insert(MBB); }
72fe6060f1SDimitry Andric   void removeFromBlocksSet(MachineBasicBlock *MBB) { BlockSet.erase(MBB); }
73fe6060f1SDimitry Andric   void addToBlocksVector(MachineBasicBlock *MBB) { Blocks.push_back(MBB); }
740b57cec5SDimitry Andric   void addBlock(MachineBasicBlock *MBB) {
750b57cec5SDimitry Andric     Blocks.push_back(MBB);
760b57cec5SDimitry Andric     BlockSet.insert(MBB);
770b57cec5SDimitry Andric   }
780b57cec5SDimitry Andric   ArrayRef<MachineBasicBlock *> getBlocks() const { return Blocks; }
790b57cec5SDimitry Andric   using block_iterator = typename ArrayRef<MachineBasicBlock *>::const_iterator;
800b57cec5SDimitry Andric   block_iterator block_begin() const { return getBlocks().begin(); }
810b57cec5SDimitry Andric   block_iterator block_end() const { return getBlocks().end(); }
820b57cec5SDimitry Andric   inline iterator_range<block_iterator> blocks() const {
830b57cec5SDimitry Andric     return make_range(block_begin(), block_end());
840b57cec5SDimitry Andric   }
850b57cec5SDimitry Andric   unsigned getNumBlocks() const { return Blocks.size(); }
860b57cec5SDimitry Andric   std::vector<MachineBasicBlock *> &getBlocksVector() { return Blocks; }
87fe6060f1SDimitry Andric   SmallPtrSetImpl<MachineBasicBlock *> &getBlocksSet() { return BlockSet; }
880b57cec5SDimitry Andric 
89fe6060f1SDimitry Andric   const std::vector<std::unique_ptr<WebAssemblyException>> &
90fe6060f1SDimitry Andric   getSubExceptions() const {
910b57cec5SDimitry Andric     return SubExceptions;
920b57cec5SDimitry Andric   }
935ffd83dbSDimitry Andric   std::vector<std::unique_ptr<WebAssemblyException>> &getSubExceptions() {
940b57cec5SDimitry Andric     return SubExceptions;
950b57cec5SDimitry Andric   }
965ffd83dbSDimitry Andric   void addSubException(std::unique_ptr<WebAssemblyException> E) {
975ffd83dbSDimitry Andric     SubExceptions.push_back(std::move(E));
985ffd83dbSDimitry Andric   }
995ffd83dbSDimitry Andric   using iterator = typename decltype(SubExceptions)::const_iterator;
1000b57cec5SDimitry Andric   iterator begin() const { return SubExceptions.begin(); }
1010b57cec5SDimitry Andric   iterator end() const { return SubExceptions.end(); }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   void reserveBlocks(unsigned Size) { Blocks.reserve(Size); }
1040b57cec5SDimitry Andric   void reverseBlock(unsigned From = 0) {
1050b57cec5SDimitry Andric     std::reverse(Blocks.begin() + From, Blocks.end());
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   // Return the nesting level. An outermost one has depth 1.
1090b57cec5SDimitry Andric   unsigned getExceptionDepth() const {
1100b57cec5SDimitry Andric     unsigned D = 1;
1110b57cec5SDimitry Andric     for (const WebAssemblyException *CurException = ParentException;
1120b57cec5SDimitry Andric          CurException; CurException = CurException->ParentException)
1130b57cec5SDimitry Andric       ++D;
1140b57cec5SDimitry Andric     return D;
1150b57cec5SDimitry Andric   }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   void print(raw_ostream &OS, unsigned Depth = 0) const;
1180b57cec5SDimitry Andric   void dump() const;
1190b57cec5SDimitry Andric };
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const WebAssemblyException &WE);
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric class WebAssemblyExceptionInfo final : public MachineFunctionPass {
1240b57cec5SDimitry Andric   // Mapping of basic blocks to the innermost exception they occur in
1250b57cec5SDimitry Andric   DenseMap<const MachineBasicBlock *, WebAssemblyException *> BBMap;
1265ffd83dbSDimitry Andric   std::vector<std::unique_ptr<WebAssemblyException>> TopLevelExceptions;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   void discoverAndMapException(WebAssemblyException *WE,
1290b57cec5SDimitry Andric                                const MachineDominatorTree &MDT,
1300b57cec5SDimitry Andric                                const MachineDominanceFrontier &MDF);
1310b57cec5SDimitry Andric   WebAssemblyException *getOutermostException(MachineBasicBlock *MBB) const;
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric public:
1340b57cec5SDimitry Andric   static char ID;
1350b57cec5SDimitry Andric   WebAssemblyExceptionInfo() : MachineFunctionPass(ID) {
1360b57cec5SDimitry Andric     initializeWebAssemblyExceptionInfoPass(*PassRegistry::getPassRegistry());
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric   ~WebAssemblyExceptionInfo() override { releaseMemory(); }
1390b57cec5SDimitry Andric   WebAssemblyExceptionInfo(const WebAssemblyExceptionInfo &) = delete;
1400b57cec5SDimitry Andric   WebAssemblyExceptionInfo &
1410b57cec5SDimitry Andric   operator=(const WebAssemblyExceptionInfo &) = delete;
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &) override;
1440b57cec5SDimitry Andric   void releaseMemory() override;
145fe6060f1SDimitry Andric   void recalculate(MachineFunction &MF, MachineDominatorTree &MDT,
1460b57cec5SDimitry Andric                    const MachineDominanceFrontier &MDF);
1470b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override;
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   bool empty() const { return TopLevelExceptions.empty(); }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   // Return the innermost exception that MBB lives in. If the block is not in an
1520b57cec5SDimitry Andric   // exception, null is returned.
1530b57cec5SDimitry Andric   WebAssemblyException *getExceptionFor(const MachineBasicBlock *MBB) const {
1540b57cec5SDimitry Andric     return BBMap.lookup(MBB);
1550b57cec5SDimitry Andric   }
1560b57cec5SDimitry Andric 
157fe6060f1SDimitry Andric   void changeExceptionFor(const MachineBasicBlock *MBB,
158fe6060f1SDimitry Andric                           WebAssemblyException *WE) {
1590b57cec5SDimitry Andric     if (!WE) {
1600b57cec5SDimitry Andric       BBMap.erase(MBB);
1610b57cec5SDimitry Andric       return;
1620b57cec5SDimitry Andric     }
1630b57cec5SDimitry Andric     BBMap[MBB] = WE;
1640b57cec5SDimitry Andric   }
1650b57cec5SDimitry Andric 
1665ffd83dbSDimitry Andric   void addTopLevelException(std::unique_ptr<WebAssemblyException> WE) {
1670b57cec5SDimitry Andric     assert(!WE->getParentException() && "Not a top level exception!");
1685ffd83dbSDimitry Andric     TopLevelExceptions.push_back(std::move(WE));
1690b57cec5SDimitry Andric   }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   void print(raw_ostream &OS, const Module *M = nullptr) const override;
1720b57cec5SDimitry Andric };
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric } // end namespace llvm
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric #endif
177