LLVM  13.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 
73  void addToBlocksVector(MachineBasicBlock *MBB) { Blocks.push_back(MBB); }
75  Blocks.push_back(MBB);
76  BlockSet.insert(MBB);
77  }
78  ArrayRef<MachineBasicBlock *> getBlocks() const { return Blocks; }
80  block_iterator block_begin() const { return getBlocks().begin(); }
81  block_iterator block_end() const { return getBlocks().end(); }
83  return make_range(block_begin(), block_end());
84  }
85  unsigned getNumBlocks() const { return Blocks.size(); }
86  std::vector<MachineBasicBlock *> &getBlocksVector() { return Blocks; }
88 
89  const std::vector<std::unique_ptr<WebAssemblyException>> &
90  getSubExceptions() const {
91  return SubExceptions;
92  }
93  std::vector<std::unique_ptr<WebAssemblyException>> &getSubExceptions() {
94  return SubExceptions;
95  }
96  void addSubException(std::unique_ptr<WebAssemblyException> E) {
97  SubExceptions.push_back(std::move(E));
98  }
99  using iterator = typename decltype(SubExceptions)::const_iterator;
100  iterator begin() const { return SubExceptions.begin(); }
101  iterator end() const { return SubExceptions.end(); }
102 
103  void reserveBlocks(unsigned Size) { Blocks.reserve(Size); }
104  void reverseBlock(unsigned From = 0) {
105  std::reverse(Blocks.begin() + From, Blocks.end());
106  }
107 
108  // Return the nesting level. An outermost one has depth 1.
109  unsigned getExceptionDepth() const {
110  unsigned D = 1;
111  for (const WebAssemblyException *CurException = ParentException;
112  CurException; CurException = CurException->ParentException)
113  ++D;
114  return D;
115  }
116 
117  void print(raw_ostream &OS, unsigned Depth = 0) const;
118  void dump() const;
119 };
120 
121 raw_ostream &operator<<(raw_ostream &OS, const WebAssemblyException &WE);
122 
124  // Mapping of basic blocks to the innermost exception they occur in
126  std::vector<std::unique_ptr<WebAssemblyException>> TopLevelExceptions;
127 
128  void discoverAndMapException(WebAssemblyException *WE,
129  const MachineDominatorTree &MDT,
130  const MachineDominanceFrontier &MDF);
131  WebAssemblyException *getOutermostException(MachineBasicBlock *MBB) const;
132 
133 public:
134  static char ID;
137  }
141  operator=(const WebAssemblyExceptionInfo &) = delete;
142 
143  bool runOnMachineFunction(MachineFunction &) override;
144  void releaseMemory() override;
146  const MachineDominanceFrontier &MDF);
147  void getAnalysisUsage(AnalysisUsage &AU) const override;
148 
149  bool empty() const { return TopLevelExceptions.empty(); }
150 
151  // Return the innermost exception that MBB lives in. If the block is not in an
152  // exception, null is returned.
154  return BBMap.lookup(MBB);
155  }
156 
158  WebAssemblyException *WE) {
159  if (!WE) {
160  BBMap.erase(MBB);
161  return;
162  }
163  BBMap[MBB] = WE;
164  }
165 
166  void addTopLevelException(std::unique_ptr<WebAssemblyException> WE) {
167  assert(!WE->getParentException() && "Not a top level exception!");
168  TopLevelExceptions.push_back(std::move(WE));
169  }
170 
171  void print(raw_ostream &OS, const Module *M = nullptr) const override;
172 };
173 
174 } // end namespace llvm
175 
176 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::WebAssemblyException::getExceptionDepth
unsigned getExceptionDepth() const
Definition: WebAssemblyExceptionInfo.h:109
llvm
Definition: AllocatorList.h:23
WebAssembly.h
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
llvm::SmallPtrSetImpl::erase
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:378
llvm::WebAssemblyExceptionInfo::recalculate
void recalculate(MachineFunction &MF, MachineDominatorTree &MDT, const MachineDominanceFrontier &MDF)
Definition: WebAssemblyExceptionInfo.cpp:77
llvm::WebAssemblyException::print
void print(raw_ostream &OS, unsigned Depth=0) const
Definition: WebAssemblyExceptionInfo.cpp:335
llvm::WebAssemblyException::reverseBlock
void reverseBlock(unsigned From=0)
Definition: WebAssemblyExceptionInfo.h:104
llvm::WebAssemblyException::contains
bool contains(const MachineBasicBlock *MBB) const
Definition: WebAssemblyExceptionInfo.h:67
llvm::WebAssemblyExceptionInfo::operator=
WebAssemblyExceptionInfo & operator=(const WebAssemblyExceptionInfo &)=delete
llvm::WebAssemblyException::getSubExceptions
std::vector< std::unique_ptr< WebAssemblyException > > & getSubExceptions()
Definition: WebAssemblyExceptionInfo.h:93
llvm::WebAssemblyException::getParentException
WebAssemblyException * getParentException() const
Definition: WebAssemblyExceptionInfo.h:57
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::Depth
@ Depth
Definition: SIMachineScheduler.h:34
llvm::reverse
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:329
llvm::WebAssemblyExceptionInfo::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: WebAssemblyExceptionInfo.cpp:272
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
llvm::WebAssemblyExceptionInfo::WebAssemblyExceptionInfo
WebAssemblyExceptionInfo()
Definition: WebAssemblyExceptionInfo.h:135
llvm::WebAssemblyException
Definition: WebAssemblyExceptionInfo.h:42
llvm::ArrayRef::const_iterator
const_pointer const_iterator
Definition: ArrayRef.h:49
llvm::WebAssemblyException::addToBlocksSet
void addToBlocksSet(MachineBasicBlock *MBB)
Definition: WebAssemblyExceptionInfo.h:71
llvm::WebAssemblyException::getNumBlocks
unsigned getNumBlocks() const
Definition: WebAssemblyExceptionInfo.h:85
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::WebAssemblyExceptionInfo::ID
static char ID
Definition: WebAssemblyExceptionInfo.h:134
llvm::WebAssemblyExceptionInfo::~WebAssemblyExceptionInfo
~WebAssemblyExceptionInfo() override
Definition: WebAssemblyExceptionInfo.h:138
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
llvm::initializeWebAssemblyExceptionInfoPass
void initializeWebAssemblyExceptionInfoPass(PassRegistry &)
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::WebAssemblyException::contains
bool contains(const WebAssemblyException *WE) const
Definition: WebAssemblyExceptionInfo.h:60
llvm::WebAssemblyException::getEHPad
MachineBasicBlock * getEHPad() const
Definition: WebAssemblyExceptionInfo.h:55
llvm::WebAssemblyException::getSubExceptions
const std::vector< std::unique_ptr< WebAssemblyException > > & getSubExceptions() const
Definition: WebAssemblyExceptionInfo.h:90
llvm::WebAssemblyException::addBlock
void addBlock(MachineBasicBlock *MBB)
Definition: WebAssemblyExceptionInfo.h:74
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::WebAssemblyException::removeFromBlocksSet
void removeFromBlocksSet(MachineBasicBlock *MBB)
Definition: WebAssemblyExceptionInfo.h:72
llvm::MachineDominanceFrontier
Definition: MachineDominanceFrontier.h:20
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::WebAssemblyException::block_end
block_iterator block_end() const
Definition: WebAssemblyExceptionInfo.h:81
llvm::DenseMap
Definition: DenseMap.h:714
llvm::WebAssemblyExceptionInfo
Definition: WebAssemblyExceptionInfo.h:123
llvm::WebAssemblyExceptionInfo::changeExceptionFor
void changeExceptionFor(const MachineBasicBlock *MBB, WebAssemblyException *WE)
Definition: WebAssemblyExceptionInfo.h:157
llvm::WebAssemblyException::getHeader
MachineBasicBlock * getHeader() const
Definition: WebAssemblyExceptionInfo.h:56
llvm::WebAssemblyException::WebAssemblyException
WebAssemblyException(MachineBasicBlock *EHPad)
Definition: WebAssemblyExceptionInfo.h:51
llvm::WebAssemblyException::block_iterator
typename ArrayRef< MachineBasicBlock * >::const_iterator block_iterator
Definition: WebAssemblyExceptionInfo.h:79
MachineFunctionPass.h
llvm::WebAssemblyExceptionInfo::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: WebAssemblyExceptionInfo.cpp:38
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::WebAssemblyException::operator=
const WebAssemblyException & operator=(const WebAssemblyException &)=delete
llvm::WebAssemblyExceptionInfo::releaseMemory
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: WebAssemblyExceptionInfo.cpp:267
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::WebAssemblyExceptionInfo::addTopLevelException
void addTopLevelException(std::unique_ptr< WebAssemblyException > WE)
Definition: WebAssemblyExceptionInfo.h:166
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::WebAssemblyException::end
iterator end() const
Definition: WebAssemblyExceptionInfo.h:101
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::WebAssemblyException::setParentException
void setParentException(WebAssemblyException *WE)
Definition: WebAssemblyExceptionInfo.h:58
llvm::WebAssemblyException::blocks
iterator_range< block_iterator > blocks() const
Definition: WebAssemblyExceptionInfo.h:82
const_iterator
llvm::WebAssemblyExceptionInfo::getExceptionFor
WebAssemblyException * getExceptionFor(const MachineBasicBlock *MBB) const
Definition: WebAssemblyExceptionInfo.h:153
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::WebAssemblyException::dump
void dump() const
Definition: WebAssemblyExceptionInfo.cpp:358
llvm::WebAssemblyException::getBlocksVector
std::vector< MachineBasicBlock * > & getBlocksVector()
Definition: WebAssemblyExceptionInfo.h:86
llvm::WebAssemblyException::addSubException
void addSubException(std::unique_ptr< WebAssemblyException > E)
Definition: WebAssemblyExceptionInfo.h:96
llvm::WebAssemblyException::block_begin
block_iterator block_begin() const
Definition: WebAssemblyExceptionInfo.h:80
llvm::WebAssemblyException::begin
iterator begin() const
Definition: WebAssemblyExceptionInfo.h:100
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:343
llvm::WebAssemblyException::reserveBlocks
void reserveBlocks(unsigned Size)
Definition: WebAssemblyExceptionInfo.h:103
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
llvm::WebAssemblyException::addToBlocksVector
void addToBlocksVector(MachineBasicBlock *MBB)
Definition: WebAssemblyExceptionInfo.h:73
llvm::MachineDominatorTree
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Definition: MachineDominators.h:45
llvm::WebAssemblyException::getBlocks
ArrayRef< MachineBasicBlock * > getBlocks() const
Definition: WebAssemblyExceptionInfo.h:78
llvm::WebAssemblyExceptionInfo::print
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: WebAssemblyExceptionInfo.cpp:366
llvm::WebAssemblyException::iterator
typename decltype(SubExceptions)::const_iterator iterator
Definition: WebAssemblyExceptionInfo.h:99
SetVector.h
llvm::WebAssemblyException::getBlocksSet
SmallPtrSetImpl< MachineBasicBlock * > & getBlocksSet()
Definition: WebAssemblyExceptionInfo.h:87
llvm::WebAssemblyExceptionInfo::empty
bool empty() const
Definition: WebAssemblyExceptionInfo.h:149
llvm::SmallPtrSetImpl::insert
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