WebAssemblyExceptionInfo.h
6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//===-- WebAssemblyExceptionInfo.h - WebAssembly Exception Info -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file implements WebAssemblyException information analysis.
///
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYEXCEPTIONINFO_H
#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYEXCEPTIONINFO_H
#include "WebAssembly.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
namespace llvm {
class MachineDominatorTree;
class MachineDominanceFrontier;
// WebAssembly instructions for exception handling are structured as follows:
// try
// instructions*
// catch ----|
// instructions* | -> A WebAssemblyException consists of this region
// end ----|
//
// A WebAssemblyException object contains BBs that belong to a 'catch' part of
// the try-catch-end structure to be created later. 'try' and 'end' markers
// are not present at this stage and will be generated in CFGStackify pass.
// Because CFGSort requires all the BBs within a catch part to be sorted
// together as it does for loops, this pass calculates the nesting structure of
// catch part of exceptions in a function.
//
// An exception catch part is defined as a BB with catch instruction and all
// other BBs dominated by this BB.
class WebAssemblyException {
MachineBasicBlock *EHPad = nullptr;
WebAssemblyException *ParentException = nullptr;
std::vector<WebAssemblyException *> SubExceptions;
std::vector<MachineBasicBlock *> Blocks;
SmallPtrSet<const MachineBasicBlock *, 8> BlockSet;
public:
WebAssemblyException(MachineBasicBlock *EHPad) : EHPad(EHPad) {}
~WebAssemblyException() { DeleteContainerPointers(SubExceptions); }
WebAssemblyException(const WebAssemblyException &) = delete;
const WebAssemblyException &operator=(const WebAssemblyException &) = delete;
MachineBasicBlock *getEHPad() const { return EHPad; }
MachineBasicBlock *getHeader() const { return EHPad; }
WebAssemblyException *getParentException() const { return ParentException; }
void setParentException(WebAssemblyException *WE) { ParentException = WE; }
bool contains(const WebAssemblyException *WE) const {
if (WE == this)
return true;
if (!WE)
return false;
return contains(WE->getParentException());
}
bool contains(const MachineBasicBlock *MBB) const {
return BlockSet.count(MBB);
}
void addBlock(MachineBasicBlock *MBB) {
Blocks.push_back(MBB);
BlockSet.insert(MBB);
}
ArrayRef<MachineBasicBlock *> getBlocks() const { return Blocks; }
using block_iterator = typename ArrayRef<MachineBasicBlock *>::const_iterator;
block_iterator block_begin() const { return getBlocks().begin(); }
block_iterator block_end() const { return getBlocks().end(); }
inline iterator_range<block_iterator> blocks() const {
return make_range(block_begin(), block_end());
}
unsigned getNumBlocks() const { return Blocks.size(); }
std::vector<MachineBasicBlock *> &getBlocksVector() { return Blocks; }
const std::vector<WebAssemblyException *> &getSubExceptions() const {
return SubExceptions;
}
std::vector<WebAssemblyException *> &getSubExceptions() {
return SubExceptions;
}
void addSubException(WebAssemblyException *E) { SubExceptions.push_back(E); }
using iterator = typename std::vector<WebAssemblyException *>::const_iterator;
iterator begin() const { return SubExceptions.begin(); }
iterator end() const { return SubExceptions.end(); }
void reserveBlocks(unsigned Size) { Blocks.reserve(Size); }
void reverseBlock(unsigned From = 0) {
std::reverse(Blocks.begin() + From, Blocks.end());
}
// Return the nesting level. An outermost one has depth 1.
unsigned getExceptionDepth() const {
unsigned D = 1;
for (const WebAssemblyException *CurException = ParentException;
CurException; CurException = CurException->ParentException)
++D;
return D;
}
void print(raw_ostream &OS, unsigned Depth = 0) const;
void dump() const;
};
raw_ostream &operator<<(raw_ostream &OS, const WebAssemblyException &WE);
class WebAssemblyExceptionInfo final : public MachineFunctionPass {
// Mapping of basic blocks to the innermost exception they occur in
DenseMap<const MachineBasicBlock *, WebAssemblyException *> BBMap;
std::vector<WebAssemblyException *> TopLevelExceptions;
void discoverAndMapException(WebAssemblyException *WE,
const MachineDominatorTree &MDT,
const MachineDominanceFrontier &MDF);
WebAssemblyException *getOutermostException(MachineBasicBlock *MBB) const;
public:
static char ID;
WebAssemblyExceptionInfo() : MachineFunctionPass(ID) {
initializeWebAssemblyExceptionInfoPass(*PassRegistry::getPassRegistry());
}
~WebAssemblyExceptionInfo() override { releaseMemory(); }
WebAssemblyExceptionInfo(const WebAssemblyExceptionInfo &) = delete;
WebAssemblyExceptionInfo &
operator=(const WebAssemblyExceptionInfo &) = delete;
bool runOnMachineFunction(MachineFunction &) override;
void releaseMemory() override;
void recalculate(MachineDominatorTree &MDT,
const MachineDominanceFrontier &MDF);
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool empty() const { return TopLevelExceptions.empty(); }
// Return the innermost exception that MBB lives in. If the block is not in an
// exception, null is returned.
WebAssemblyException *getExceptionFor(const MachineBasicBlock *MBB) const {
return BBMap.lookup(MBB);
}
void changeExceptionFor(MachineBasicBlock *MBB, WebAssemblyException *WE) {
if (!WE) {
BBMap.erase(MBB);
return;
}
BBMap[MBB] = WE;
}
void addTopLevelException(WebAssemblyException *WE) {
assert(!WE->getParentException() && "Not a top level exception!");
TopLevelExceptions.push_back(WE);
}
void print(raw_ostream &OS, const Module *M = nullptr) const override;
};
} // end namespace llvm
#endif