LLVM 24.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"
21#include "llvm/IR/Analysis.h"
22#include "llvm/IR/PassManager.h"
23#include "llvm/Pass.h"
24
25namespace llvm {
26
29
30// WebAssembly instructions for exception handling are structured as follows:
31// try
32// instructions*
33// catch ----|
34// instructions* | -> A WebAssemblyException consists of this region
35// end ----|
36//
37// A WebAssemblyException object contains BBs that belong to a 'catch' part of
38// the try-catch-end structure to be created later. 'try' and 'end' markers
39// are not present at this stage and will be generated in CFGStackify pass.
40// Because CFGSort requires all the BBs within a catch part to be sorted
41// together as it does for loops, this pass calculates the nesting structure of
42// catch part of exceptions in a function.
43//
44// An exception catch part is defined as a BB with catch instruction and all
45// other BBs dominated by this BB.
47 MachineBasicBlock *EHPad = nullptr;
48
49 WebAssemblyException *ParentException = nullptr;
50 std::vector<std::unique_ptr<WebAssemblyException>> SubExceptions;
51 std::vector<MachineBasicBlock *> Blocks;
53
54public:
55 WebAssemblyException(MachineBasicBlock *EHPad) : EHPad(EHPad) {}
58
59 MachineBasicBlock *getEHPad() const { return EHPad; }
60 MachineBasicBlock *getHeader() const { return EHPad; }
61 WebAssemblyException *getParentException() const { return ParentException; }
62 void setParentException(WebAssemblyException *WE) { ParentException = WE; }
63
64 bool contains(const WebAssemblyException *WE) const {
65 if (WE == this)
66 return true;
67 if (!WE)
68 return false;
69 return contains(WE->getParentException());
70 }
71 bool contains(const MachineBasicBlock *MBB) const {
72 return BlockSet.count(MBB);
73 }
74
75 void addToBlocksSet(MachineBasicBlock *MBB) { BlockSet.insert(MBB); }
76 void removeFromBlocksSet(MachineBasicBlock *MBB) { BlockSet.erase(MBB); }
77 void addToBlocksVector(MachineBasicBlock *MBB) { Blocks.push_back(MBB); }
79 Blocks.push_back(MBB);
80 BlockSet.insert(MBB);
81 }
82 ArrayRef<MachineBasicBlock *> getBlocks() const { return Blocks; }
84 block_iterator block_begin() const { return getBlocks().begin(); }
85 block_iterator block_end() const { return getBlocks().end(); }
89 unsigned getNumBlocks() const { return Blocks.size(); }
90 std::vector<MachineBasicBlock *> &getBlocksVector() { return Blocks; }
92
93 const std::vector<std::unique_ptr<WebAssemblyException>> &
95 return SubExceptions;
96 }
97 std::vector<std::unique_ptr<WebAssemblyException>> &getSubExceptions() {
98 return SubExceptions;
99 }
100 void addSubException(std::unique_ptr<WebAssemblyException> E) {
101 SubExceptions.push_back(std::move(E));
102 }
103 using iterator = decltype(SubExceptions)::const_iterator;
104 iterator begin() const { return SubExceptions.begin(); }
105 iterator end() const { return SubExceptions.end(); }
106
107 void reserveBlocks(unsigned Size) { Blocks.reserve(Size); }
108 void reverseBlock(unsigned From = 0) {
109 std::reverse(Blocks.begin() + From, Blocks.end());
110 }
111
112 // Return the nesting level. An outermost one has depth 1.
113 unsigned getExceptionDepth() const {
114 unsigned D = 1;
115 for (const WebAssemblyException *CurException = ParentException;
116 CurException; CurException = CurException->ParentException)
117 ++D;
118 return D;
119 }
120
121 void print(raw_ostream &OS, unsigned Depth = 0) const;
122 void dump() const;
123};
124
126
128 // Mapping of basic blocks to the innermost exception they occur in
130 std::vector<std::unique_ptr<WebAssemblyException>> TopLevelExceptions;
131
132 void discoverAndMapException(WebAssemblyException *WE,
133 const MachineDominatorTree &MDT,
134 const MachineDominanceFrontier &MDF);
135 WebAssemblyException *getOutermostException(MachineBasicBlock *MBB) const;
136
137public:
144
145 void releaseMemory();
147 const MachineDominanceFrontier &MDF);
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
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) const;
172
173 bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
174 MachineFunctionAnalysisManager::Invalidator &);
175};
176
178 WebAssemblyExceptionInfo WasmExceptionInfo;
179
180public:
181 static char ID;
183
184 void getAnalysisUsage(AnalysisUsage &AU) const override;
185 bool runOnMachineFunction(MachineFunction &MF) override;
186 void print(raw_ostream &OS, const Module *M = nullptr) const override {
187 WasmExceptionInfo.print(OS, M);
188 }
189 void releaseMemory() override { WasmExceptionInfo.releaseMemory(); }
190
191 WebAssemblyExceptionInfo &getWEI() { return WasmExceptionInfo; }
192 const WebAssemblyExceptionInfo &getWEI() const { return WasmExceptionInfo; }
193};
194
196 : public AnalysisInfoMixin<WebAssemblyExceptionAnalysis> {
198 static AnalysisKey Key;
199
200public:
202
205};
206
207} // end namespace llvm
208
209#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
This header defines various interfaces for pass management in LLVM.
This file defines the SmallPtrSet class.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const_pointer const_iterator
Definition ArrayRef.h:48
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const WebAssemblyExceptionInfo & getWEI() const
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void changeExceptionFor(const MachineBasicBlock *MBB, WebAssemblyException *WE)
void addTopLevelException(std::unique_ptr< WebAssemblyException > WE)
void recalculate(MachineFunction &MF, MachineDominatorTree &MDT, const MachineDominanceFrontier &MDF)
void print(raw_ostream &OS, const Module *M) const
WebAssemblyException * getExceptionFor(const MachineBasicBlock *MBB) const
WebAssemblyExceptionInfo & operator=(const WebAssemblyExceptionInfo &)=delete
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)
WebAssemblyExceptionInfo(WebAssemblyExceptionInfo &&)=default
WebAssemblyExceptionInfo(const WebAssemblyExceptionInfo &)=delete
void print(raw_ostream &OS, unsigned Depth=0) const
bool contains(const WebAssemblyException *WE) const
WebAssemblyException(const WebAssemblyException &)=delete
MachineBasicBlock * getEHPad() const
void addToBlocksSet(MachineBasicBlock *MBB)
bool contains(const MachineBasicBlock *MBB) const
iterator_range< block_iterator > blocks() const
const std::vector< std::unique_ptr< WebAssemblyException > > & getSubExceptions() const
SmallPtrSetImpl< MachineBasicBlock * > & getBlocksSet()
decltype(SubExceptions)::const_iterator iterator
ArrayRef< MachineBasicBlock * >::const_iterator block_iterator
std::vector< std::unique_ptr< WebAssemblyException > > & getSubExceptions()
void addSubException(std::unique_ptr< WebAssemblyException > E)
ArrayRef< MachineBasicBlock * > getBlocks() const
std::vector< MachineBasicBlock * > & getBlocksVector()
WebAssemblyException * getParentException() const
void removeFromBlocksSet(MachineBasicBlock *MBB)
WebAssemblyException(MachineBasicBlock *EHPad)
void addBlock(MachineBasicBlock *MBB)
MachineBasicBlock * getHeader() const
void setParentException(WebAssemblyException *WE)
void addToBlocksVector(MachineBasicBlock *MBB)
const WebAssemblyException & operator=(const WebAssemblyException &)=delete
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29