LLVM  12.0.0git
WebAssemblyExceptionInfo.h
Go to the documentation of this file.
1 //===-- WebAssemblyExceptionInfo.h - WebAssembly Exception Info -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// \brief This file implements WebAssemblyException information analysis.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYEXCEPTIONINFO_H
15 #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYEXCEPTIONINFO_H
16 
17 #include "WebAssembly.h"
18 #include "llvm/ADT/SetVector.h"
20 
21 namespace llvm {
22 
23 class MachineDominatorTree;
24 class MachineDominanceFrontier;
25 
26 // WebAssembly instructions for exception handling are structured as follows:
27 // try
28 // instructions*
29 // catch ----|
30 // instructions* | -> A WebAssemblyException consists of this region
31 // end ----|
32 //
33 // A WebAssemblyException object contains BBs that belong to a 'catch' part of
34 // the try-catch-end structure to be created later. 'try' and 'end' markers
35 // are not present at this stage and will be generated in CFGStackify pass.
36 // Because CFGSort requires all the BBs within a catch part to be sorted
37 // together as it does for loops, this pass calculates the nesting structure of
38 // catch part of exceptions in a function.
39 //
40 // An exception catch part is defined as a BB with catch instruction and all
41 // other BBs dominated by this BB.
43  MachineBasicBlock *EHPad = nullptr;
44 
45  WebAssemblyException *ParentException = nullptr;
46  std::vector<std::unique_ptr<WebAssemblyException>> SubExceptions;
47  std::vector<MachineBasicBlock *> Blocks;
49 
50 public:
51  WebAssemblyException(MachineBasicBlock *EHPad) : EHPad(EHPad) {}
53  const WebAssemblyException &operator=(const WebAssemblyException &) = delete;
54 
55  MachineBasicBlock *getEHPad() const { return EHPad; }
56  MachineBasicBlock *getHeader() const { return EHPad; }
57  WebAssemblyException *getParentException() const { return ParentException; }
58  void setParentException(WebAssemblyException *WE) { ParentException = WE; }
59 
60  bool contains(const WebAssemblyException *WE) const {
61  if (WE == this)
62  return true;
63  if (!WE)
64  return false;
65  return contains(WE->getParentException());
66  }
67  bool contains(const MachineBasicBlock *MBB) const {
68  return BlockSet.count(MBB);
69  }
70 
72  Blocks.push_back(MBB);
73  BlockSet.insert(MBB);
74  }
75  ArrayRef<MachineBasicBlock *> getBlocks() const { return Blocks; }
77  block_iterator block_begin() const { return getBlocks().begin(); }
78  block_iterator block_end() const { return getBlocks().end(); }
80  return make_range(block_begin(), block_end());
81  }
82  unsigned getNumBlocks() const { return Blocks.size(); }
83  std::vector<MachineBasicBlock *> &getBlocksVector() { return Blocks; }
84 
85  const std::vector<std::unique_ptr<WebAssemblyException>> &getSubExceptions() const {
86  return SubExceptions;
87  }
88  std::vector<std::unique_ptr<WebAssemblyException>> &getSubExceptions() {
89  return SubExceptions;
90  }
91  void addSubException(std::unique_ptr<WebAssemblyException> E) {
92  SubExceptions.push_back(std::move(E));
93  }
94  using iterator = typename decltype(SubExceptions)::const_iterator;
95  iterator begin() const { return SubExceptions.begin(); }
96  iterator end() const { return SubExceptions.end(); }
97 
98  void reserveBlocks(unsigned Size) { Blocks.reserve(Size); }
99  void reverseBlock(unsigned From = 0) {
100  std::reverse(Blocks.begin() + From, Blocks.end());
101  }
102 
103  // Return the nesting level. An outermost one has depth 1.
104  unsigned getExceptionDepth() const {
105  unsigned D = 1;
106  for (const WebAssemblyException *CurException = ParentException;
107  CurException; CurException = CurException->ParentException)
108  ++D;
109  return D;
110  }
111 
112  void print(raw_ostream &OS, unsigned Depth = 0) const;
113  void dump() const;
114 };
115 
117 
119  // Mapping of basic blocks to the innermost exception they occur in
121  std::vector<std::unique_ptr<WebAssemblyException>> TopLevelExceptions;
122 
123  void discoverAndMapException(WebAssemblyException *WE,
124  const MachineDominatorTree &MDT,
125  const MachineDominanceFrontier &MDF);
126  WebAssemblyException *getOutermostException(MachineBasicBlock *MBB) const;
127 
128 public:
129  static char ID;
132  }
133  ~WebAssemblyExceptionInfo() override { releaseMemory(); }
136  operator=(const WebAssemblyExceptionInfo &) = delete;
137 
138  bool runOnMachineFunction(MachineFunction &) override;
139  void releaseMemory() override;
140  void recalculate(MachineDominatorTree &MDT,
141  const MachineDominanceFrontier &MDF);
142  void getAnalysisUsage(AnalysisUsage &AU) const override;
143 
144  bool empty() const { return TopLevelExceptions.empty(); }
145 
146  // Return the innermost exception that MBB lives in. If the block is not in an
147  // exception, null is returned.
149  return BBMap.lookup(MBB);
150  }
151 
153  if (!WE) {
154  BBMap.erase(MBB);
155  return;
156  }
157  BBMap[MBB] = WE;
158  }
159 
160  void addTopLevelException(std::unique_ptr<WebAssemblyException> WE) {
161  assert(!WE->getParentException() && "Not a top level exception!");
162  TopLevelExceptions.push_back(std::move(WE));
163  }
164 
165  void print(raw_ostream &OS, const Module *M = nullptr) const override;
166 };
167 
168 } // end namespace llvm
169 
170 #endif
const std::vector< std::unique_ptr< WebAssemblyException > > & getSubExceptions() const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void reverseBlock(unsigned From=0)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
void print(raw_ostream &OS, unsigned Depth=0) const
std::vector< std::unique_ptr< WebAssemblyException > > & getSubExceptions()
MachineBasicBlock * getHeader() const
block_iterator block_begin() const
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
iterator_range< block_iterator > blocks() const
MachineBasicBlock & MBB
WebAssemblyException(MachineBasicBlock *EHPad)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const WebAssemblyException & operator=(const WebAssemblyException &)=delete
std::vector< MachineBasicBlock * > & getBlocksVector()
void initializeWebAssemblyExceptionInfoPass(PassRegistry &)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
void addBlock(MachineBasicBlock *MBB)
ArrayRef< MachineBasicBlock * > getBlocks() const
void changeExceptionFor(MachineBasicBlock *MBB, WebAssemblyException *WE)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:364
MachineBasicBlock * getEHPad() const
Represent the analysis usage information of a pass.
void addSubException(std::unique_ptr< WebAssemblyException > E)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:375
WebAssemblyException * getExceptionFor(const MachineBasicBlock *MBB) const
block_iterator block_end() const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
BlockVerifier::State From
bool contains(const MachineBasicBlock *MBB) const
void setParentException(WebAssemblyException *WE)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
WebAssemblyException * getParentException() const
A range adaptor for a pair of iterators.
void addTopLevelException(std::unique_ptr< WebAssemblyException > WE)
bool contains(const WebAssemblyException *WE) const
uint32_t Size
Definition: Profile.cpp:46
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2099
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
typename ArrayRef< MachineBasicBlock * >::const_iterator block_iterator
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:341
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
typename decltype(SubExceptions)::const_iterator iterator