LLVM  7.0.0
WebAssemblyExceptionInfo.cpp
Go to the documentation of this file.
1 //===--- WebAssemblyExceptionInfo.cpp - Exception Infomation --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file implements WebAssemblyException information analysis.
12 ///
13 //===----------------------------------------------------------------------===//
14 
16 #include "WebAssemblyUtilities.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "wasm-exception-info"
25 
27 
29  "WebAssembly Exception Information", true, true)
33  "WebAssembly Exception Information", true, true)
34 
35 bool WebAssemblyExceptionInfo::runOnMachineFunction(MachineFunction &F) {
36  releaseMemory();
37  auto &MDT = getAnalysis<MachineDominatorTree>();
38  auto &MDF = getAnalysis<MachineDominanceFrontier>();
39  recalculate(MDT, MDF);
40  return false;
41 }
42 
45  // Postorder traversal of the dominator tree.
47  for (auto DomNode : post_order(&MDT)) {
48  MachineBasicBlock *EHPad = DomNode->getBlock();
49  if (!EHPad->isEHPad())
50  continue;
51  // We group catch & catch-all terminate pads together, so skip the second
52  // one
54  continue;
55  auto *WE = new WebAssemblyException(EHPad);
56  discoverAndMapException(WE, MDT, MDF);
57  Exceptions.push_back(WE);
58  }
59 
60  // Add BBs to exceptions
61  for (auto DomNode : post_order(&MDT)) {
62  MachineBasicBlock *MBB = DomNode->getBlock();
64  for (; WE; WE = WE->getParentException())
65  WE->addBlock(MBB);
66  }
67 
68  // Add subexceptions to exceptions
69  for (auto *WE : Exceptions) {
70  if (WE->getParentException())
71  WE->getParentException()->getSubExceptions().push_back(WE);
72  else
74  }
75 
76  // For convenience, Blocks and SubExceptions are inserted in postorder.
77  // Reverse the lists.
78  for (auto *WE : Exceptions) {
79  WE->reverseBlock();
80  std::reverse(WE->getSubExceptions().begin(), WE->getSubExceptions().end());
81  }
82 }
83 
85  BBMap.clear();
86  DeleteContainerPointers(TopLevelExceptions);
87  TopLevelExceptions.clear();
88 }
89 
91  AU.setPreservesAll();
95 }
96 
97 void WebAssemblyExceptionInfo::discoverAndMapException(
99  const MachineDominanceFrontier &MDF) {
100  unsigned NumBlocks = 0;
101  unsigned NumSubExceptions = 0;
102 
103  // Map blocks that belong to a catchpad / cleanuppad
104  MachineBasicBlock *EHPad = WE->getEHPad();
105 
106  // We group catch & catch-all terminate pads together within an exception
107  if (WebAssembly::isCatchTerminatePad(*EHPad)) {
108  assert(EHPad->succ_size() == 1 &&
109  "Catch terminate pad has more than one successors");
110  changeExceptionFor(EHPad, WE);
111  changeExceptionFor(*(EHPad->succ_begin()), WE);
112  return;
113  }
114 
116  WL.push_back(EHPad);
117  while (!WL.empty()) {
118  MachineBasicBlock *MBB = WL.pop_back_val();
119 
120  // Find its outermost discovered exception. If this is a discovered block,
121  // check if it is already discovered to be a subexception of this exception.
122  WebAssemblyException *SubE = getOutermostException(MBB);
123  if (SubE) {
124  if (SubE != WE) {
125  // Discover a subexception of this exception.
126  SubE->setParentException(WE);
127  ++NumSubExceptions;
128  NumBlocks += SubE->getBlocksVector().capacity();
129  // All blocks that belong to this subexception have been already
130  // discovered. Skip all of them. Add the subexception's landing pad's
131  // dominance frontier to the worklist.
132  for (auto &Frontier : MDF.find(SubE->getEHPad())->second)
133  if (MDT.dominates(EHPad, Frontier))
134  WL.push_back(Frontier);
135  }
136  continue;
137  }
138 
139  // This is an undiscovered block. Map it to the current exception.
140  changeExceptionFor(MBB, WE);
141  ++NumBlocks;
142 
143  // Add successors dominated by the current BB to the worklist.
144  for (auto *Succ : MBB->successors())
145  if (MDT.dominates(EHPad, Succ))
146  WL.push_back(Succ);
147  }
148 
149  WE->getSubExceptions().reserve(NumSubExceptions);
150  WE->reserveBlocks(NumBlocks);
151 }
152 
154 WebAssemblyExceptionInfo::getOutermostException(MachineBasicBlock *MBB) const {
156  if (WE) {
157  while (WebAssemblyException *Parent = WE->getParentException())
158  WE = Parent;
159  }
160  return WE;
161 }
162 
163 void WebAssemblyException::print(raw_ostream &OS, unsigned Depth) const {
164  OS.indent(Depth * 2) << "Exception at depth " << getExceptionDepth()
165  << " containing: ";
166 
167  for (unsigned I = 0; I < getBlocks().size(); ++I) {
168  MachineBasicBlock *MBB = getBlocks()[I];
169  if (I)
170  OS << ", ";
171  OS << "%bb." << MBB->getNumber();
172  if (const auto *BB = MBB->getBasicBlock())
173  if (BB->hasName())
174  OS << "." << BB->getName();
175 
176  if (getEHPad() == MBB)
177  OS << " (landing-pad)";
178  }
179  OS << "\n";
180 
181  for (auto &SubE : SubExceptions)
182  SubE->print(OS, Depth + 2);
183 }
184 
185 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
187 #endif
188 
190  WE.print(OS);
191  return OS;
192 }
193 
195  for (auto *WE : TopLevelExceptions)
196  WE->print(OS);
197 }
void DeleteContainerPointers(Container &C)
For a container of pointers, deletes the pointers and then clears the container.
Definition: STLExtras.h:896
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void print(raw_ostream &OS, unsigned Depth=0) const
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
unsigned second
F(f)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:452
#define DEBUG_TYPE
iterator_range< succ_iterator > successors()
void recalculate(MachineDominatorTree &MDT, const MachineDominanceFrontier &MDF)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
WebAssembly Exception true
std::vector< MachineBasicBlock * > & getBlocksVector()
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:250
void addBlock(MachineBasicBlock *MBB)
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they&#39;re not in a MachineFuncti...
This file implements WebAssemblyException information analysis.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This file contains the declaration of the WebAssembly-specific utility functions. ...
void changeExceptionFor(MachineBasicBlock *MBB, WebAssemblyException *WE)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This file provides WebAssembly-specific target descriptions.
MachineBasicBlock * getEHPad() const
Represent the analysis usage information of a pass.
iterator_range< po_iterator< T > > post_order(const T &G)
void addTopLevelException(WebAssemblyException *WE)
INITIALIZE_PASS_BEGIN(WebAssemblyExceptionInfo, DEBUG_TYPE, "WebAssembly Exception Information", true, true) INITIALIZE_PASS_END(WebAssemblyExceptionInfo
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
WebAssemblyException * getExceptionFor(const MachineBasicBlock *MBB) const
iterator find(MachineBasicBlock *B)
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
void setParentException(WebAssemblyException *WE)
bool isCatchTerminatePad(const MachineBasicBlock &MBB)
Returns if the given BB is a single BB terminate pad which starts with a &#39;catch&#39; instruction.
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:381
WebAssemblyException * getParentException() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
void setPreservesAll()
Set by analyses that do not transform their input at all.
unsigned succ_size() const
bool isEHPad() const
Returns true if the block is a landing pad.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
#define I(x, y, z)
Definition: MD5.cpp:58
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
WebAssembly Exception Information
const std::vector< WebAssemblyException * > & getSubExceptions() const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2032
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool isCatchAllTerminatePad(const MachineBasicBlock &MBB)
Returns if the given BB is a single BB terminate pad which starts with a &#39;catch_all&#39; insrtruction...