1 //===--- WebAssemblyExceptionInfo.cpp - Exception Infomation --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements WebAssemblyException information analysis.
13 //===----------------------------------------------------------------------===//
15 #include "WebAssemblyExceptionInfo.h"
16 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "WebAssemblyUtilities.h"
18 #include "llvm/ADT/PostOrderIterator.h"
19 #include "llvm/CodeGen/MachineDominanceFrontier.h"
20 #include "llvm/CodeGen/MachineDominators.h"
24 #define DEBUG_TYPE "wasm-exception-info"
26 char WebAssemblyExceptionInfo::ID = 0;
28 INITIALIZE_PASS_BEGIN(WebAssemblyExceptionInfo, DEBUG_TYPE,
29 "WebAssembly Exception Information", true, true)
30 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
31 INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier)
32 INITIALIZE_PASS_END(WebAssemblyExceptionInfo, DEBUG_TYPE,
33 "WebAssembly Exception Information", true, true)
35 bool WebAssemblyExceptionInfo::runOnMachineFunction(MachineFunction &MF) {
36 LLVM_DEBUG(dbgs() << "********** Exception Info Calculation **********\n"
37 "********** Function: "
38 << MF.getName() << '\n');
40 auto &MDT = getAnalysis<MachineDominatorTree>();
41 auto &MDF = getAnalysis<MachineDominanceFrontier>();
42 recalculate(MDT, MDF);
46 void WebAssemblyExceptionInfo::recalculate(
47 MachineDominatorTree &MDT, const MachineDominanceFrontier &MDF) {
48 // Postorder traversal of the dominator tree.
49 SmallVector<WebAssemblyException *, 8> Exceptions;
50 for (auto DomNode : post_order(&MDT)) {
51 MachineBasicBlock *EHPad = DomNode->getBlock();
52 if (!EHPad->isEHPad())
54 // We group catch & catch-all terminate pads together, so skip the second
56 if (WebAssembly::isCatchAllTerminatePad(*EHPad))
58 auto *WE = new WebAssemblyException(EHPad);
59 discoverAndMapException(WE, MDT, MDF);
60 Exceptions.push_back(WE);
63 // Add BBs to exceptions
64 for (auto DomNode : post_order(&MDT)) {
65 MachineBasicBlock *MBB = DomNode->getBlock();
66 WebAssemblyException *WE = getExceptionFor(MBB);
67 for (; WE; WE = WE->getParentException())
71 // Add subexceptions to exceptions
72 for (auto *WE : Exceptions) {
73 if (WE->getParentException())
74 WE->getParentException()->getSubExceptions().push_back(WE);
76 addTopLevelException(WE);
79 // For convenience, Blocks and SubExceptions are inserted in postorder.
81 for (auto *WE : Exceptions) {
83 std::reverse(WE->getSubExceptions().begin(), WE->getSubExceptions().end());
87 void WebAssemblyExceptionInfo::releaseMemory() {
89 DeleteContainerPointers(TopLevelExceptions);
90 TopLevelExceptions.clear();
93 void WebAssemblyExceptionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
95 AU.addRequired<MachineDominatorTree>();
96 AU.addRequired<MachineDominanceFrontier>();
97 MachineFunctionPass::getAnalysisUsage(AU);
100 void WebAssemblyExceptionInfo::discoverAndMapException(
101 WebAssemblyException *WE, const MachineDominatorTree &MDT,
102 const MachineDominanceFrontier &MDF) {
103 unsigned NumBlocks = 0;
104 unsigned NumSubExceptions = 0;
106 // Map blocks that belong to a catchpad / cleanuppad
107 MachineBasicBlock *EHPad = WE->getEHPad();
109 // We group catch & catch-all terminate pads together within an exception
110 if (WebAssembly::isCatchTerminatePad(*EHPad)) {
111 assert(EHPad->succ_size() == 1 &&
112 "Catch terminate pad has more than one successors");
113 changeExceptionFor(EHPad, WE);
114 changeExceptionFor(*(EHPad->succ_begin()), WE);
118 SmallVector<MachineBasicBlock *, 8> WL;
120 while (!WL.empty()) {
121 MachineBasicBlock *MBB = WL.pop_back_val();
123 // Find its outermost discovered exception. If this is a discovered block,
124 // check if it is already discovered to be a subexception of this exception.
125 WebAssemblyException *SubE = getOutermostException(MBB);
128 // Discover a subexception of this exception.
129 SubE->setParentException(WE);
131 NumBlocks += SubE->getBlocksVector().capacity();
132 // All blocks that belong to this subexception have been already
133 // discovered. Skip all of them. Add the subexception's landing pad's
134 // dominance frontier to the worklist.
135 for (auto &Frontier : MDF.find(SubE->getEHPad())->second)
136 if (MDT.dominates(EHPad, Frontier))
137 WL.push_back(Frontier);
142 // This is an undiscovered block. Map it to the current exception.
143 changeExceptionFor(MBB, WE);
146 // Add successors dominated by the current BB to the worklist.
147 for (auto *Succ : MBB->successors())
148 if (MDT.dominates(EHPad, Succ))
152 WE->getSubExceptions().reserve(NumSubExceptions);
153 WE->reserveBlocks(NumBlocks);
156 WebAssemblyException *
157 WebAssemblyExceptionInfo::getOutermostException(MachineBasicBlock *MBB) const {
158 WebAssemblyException *WE = getExceptionFor(MBB);
160 while (WebAssemblyException *Parent = WE->getParentException())
166 void WebAssemblyException::print(raw_ostream &OS, unsigned Depth) const {
167 OS.indent(Depth * 2) << "Exception at depth " << getExceptionDepth()
170 for (unsigned I = 0; I < getBlocks().size(); ++I) {
171 MachineBasicBlock *MBB = getBlocks()[I];
174 OS << "%bb." << MBB->getNumber();
175 if (const auto *BB = MBB->getBasicBlock())
177 OS << "." << BB->getName();
179 if (getEHPad() == MBB)
180 OS << " (landing-pad)";
184 for (auto &SubE : SubExceptions)
185 SubE->print(OS, Depth + 2);
188 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
189 LLVM_DUMP_METHOD void WebAssemblyException::dump() const { print(dbgs()); }
192 raw_ostream &operator<<(raw_ostream &OS, const WebAssemblyException &WE) {
197 void WebAssemblyExceptionInfo::print(raw_ostream &OS, const Module *) const {
198 for (auto *WE : TopLevelExceptions)