LLVM 23.0.0git
WebAssemblyExceptionInfo.cpp
Go to the documentation of this file.
1//===--- WebAssemblyExceptionInfo.cpp - Exception Infomation --------------===//
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
19#include "llvm/IR/Function.h"
21#include "llvm/MC/MCAsmInfo.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "wasm-exception-info"
27
29
31 "WebAssembly Exception Information", true, true)
35 "WebAssembly Exception Information", true, true)
36
38 LLVM_DEBUG(dbgs() << "********** Exception Info Calculation **********\n"
39 "********** Function: "
40 << MF.getName() << '\n');
42 if (MF.getTarget().getMCAsmInfo()->getExceptionHandlingType() !=
44 !MF.getFunction().hasPersonalityFn())
45 return false;
46 auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
48 recalculate(MF, MDT, MDF);
50 return false;
51}
52
55 const MachineDominanceFrontier &MDF) {
56 // Postorder traversal of the dominator tree.
58 for (auto *DomNode : post_order(&MDT)) {
59 MachineBasicBlock *EHPad = DomNode->getBlock();
60 if (!EHPad->isEHPad())
61 continue;
62 auto WE = std::make_unique<WebAssemblyException>(EHPad);
63 discoverAndMapException(WE.get(), MDT, MDF);
64 Exceptions.push_back(std::move(WE));
65 }
66
67 // Add BBs to exceptions' block set. This is a preparation to take out
68 // remaining incorect BBs from exceptions, because we need to iterate over BBs
69 // for each exception.
70 for (auto *DomNode : post_order(&MDT)) {
71 MachineBasicBlock *MBB = DomNode->getBlock();
73 for (; WE; WE = WE->getParentException())
75 }
76
77 // Add BBs to exceptions' block vector
78 for (auto *DomNode : post_order(&MDT)) {
79 MachineBasicBlock *MBB = DomNode->getBlock();
81 for (; WE; WE = WE->getParentException())
83 }
84
86 ExceptionPointers.reserve(Exceptions.size());
87
88 // Add subexceptions to exceptions
89 for (auto &WE : Exceptions) {
90 ExceptionPointers.push_back(WE.get());
91 if (WE->getParentException())
92 WE->getParentException()->getSubExceptions().push_back(std::move(WE));
93 else
94 addTopLevelException(std::move(WE));
95 }
96
97 // For convenience, Blocks and SubExceptions are inserted in postorder.
98 // Reverse the lists.
99 for (auto *WE : ExceptionPointers) {
100 WE->reverseBlock();
101 std::reverse(WE->getSubExceptions().begin(), WE->getSubExceptions().end());
102 }
103}
104
106 BBMap.clear();
107 TopLevelExceptions.clear();
108}
109
116
117void WebAssemblyExceptionInfo::discoverAndMapException(
119 const MachineDominanceFrontier &MDF) {
120 unsigned NumBlocks = 0;
121 unsigned NumSubExceptions = 0;
122
123 // Map blocks that belong to a catchpad / cleanuppad
124 MachineBasicBlock *EHPad = WE->getEHPad();
126 WL.push_back(EHPad);
127 while (!WL.empty()) {
129
130 // Find its outermost discovered exception. If this is a discovered block,
131 // check if it is already discovered to be a subexception of this exception.
132 WebAssemblyException *SubE = getOutermostException(MBB);
133 if (SubE) {
134 if (SubE != WE) {
135 // Discover a subexception of this exception.
136 SubE->setParentException(WE);
137 ++NumSubExceptions;
138 NumBlocks += SubE->getBlocksVector().capacity();
139 // All blocks that belong to this subexception have been already
140 // discovered. Skip all of them. Add the subexception's landing pad's
141 // dominance frontier to the worklist.
142 for (auto &Frontier : MDF.find(SubE->getEHPad())->second)
143 if (MDT.dominates(EHPad, Frontier))
144 WL.push_back(Frontier);
145 }
146 continue;
147 }
148
149 // This is an undiscovered block. Map it to the current exception.
151 ++NumBlocks;
152
153 // Add successors dominated by the current BB to the worklist.
154 for (auto *Succ : MBB->successors())
155 if (MDT.dominates(EHPad, Succ))
156 WL.push_back(Succ);
157 }
158
159 WE->getSubExceptions().reserve(NumSubExceptions);
160 WE->reserveBlocks(NumBlocks);
161}
162
164WebAssemblyExceptionInfo::getOutermostException(MachineBasicBlock *MBB) const {
165 WebAssemblyException *WE = getExceptionFor(MBB);
166 if (WE) {
167 while (WebAssemblyException *Parent = WE->getParentException())
168 WE = Parent;
169 }
170 return WE;
171}
172
174 OS.indent(Depth * 2) << "Exception at depth " << getExceptionDepth()
175 << " containing: ";
176
177 for (unsigned I = 0; I < getBlocks().size(); ++I) {
179 if (I)
180 OS << ", ";
181 OS << "%bb." << MBB->getNumber();
182 if (const auto *BB = MBB->getBasicBlock())
183 if (BB->hasName())
184 OS << "." << BB->getName();
185
186 if (getEHPad() == MBB)
187 OS << " (landing-pad)";
188 }
189 OS << "\n";
190
191 for (auto &SubE : SubExceptions)
192 SubE->print(OS, Depth + 2);
193}
194
195#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
197#endif
198
200 WE.print(OS);
201 return OS;
202}
203
205 for (auto &WE : TopLevelExceptions)
206 WE->print(OS);
207}
MachineBasicBlock & MBB
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
#define DEBUG_TYPE
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
#define LLVM_DEBUG(...)
Definition Debug.h:114
This file implements WebAssemblyException information analysis.
This file contains the declaration of the WebAssembly-specific utility functions.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool isEHPad() const
Returns true if the block is a landing pad.
iterator_range< succ_iterator > successors()
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
void changeExceptionFor(const MachineBasicBlock *MBB, WebAssemblyException *WE)
bool runOnMachineFunction(MachineFunction &) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void addTopLevelException(std::unique_ptr< WebAssemblyException > WE)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void recalculate(MachineFunction &MF, MachineDominatorTree &MDT, const MachineDominanceFrontier &MDF)
WebAssemblyException * getExceptionFor(const MachineBasicBlock *MBB) 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 print(raw_ostream &OS, unsigned Depth=0) const
MachineBasicBlock * getEHPad() const
void addToBlocksSet(MachineBasicBlock *MBB)
const std::vector< std::unique_ptr< WebAssemblyException > > & getSubExceptions() const
ArrayRef< MachineBasicBlock * > getBlocks() const
std::vector< MachineBasicBlock * > & getBlocksVector()
WebAssemblyException * getParentException() const
void setParentException(WebAssemblyException *WE)
void addToBlocksVector(MachineBasicBlock *MBB)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
@ Wasm
WebAssembly Exception Handling.
Definition CodeGen.h:59
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
auto post_order(const T &G)
Post-order traversal of a graph.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)