1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 20:23:11 +01:00
llvm-mirror/lib/Target/WebAssembly/WebAssemblyExceptionInfo.h
Heejin Ahn af019c3b48 [WebAssembly] Fix more ExceptionInfo grouping bugs
This fixes two bugs in `WebAssemblyExceptionInfo` grouping, created by
D97247. These two bugs are not easy to split into two different CLs,
because tests that fail for one also tend to fail for the other.

- In D97247, when fixing `ExceptionInfo` grouping by taking out
  the unwind destination' exception from the unwind src's exception, we
  just iterated the BBs in the function order, but this was incorrect;
  this changes it to dominator tree preorder. Please refer to the
  comments in the code for the reason and an example.

- After this subexception-taking-out fix, there still can be remaining
  BBs we have to take out. When Exception B is taken out of Exception A
  (because EHPad B is the unwind destination of EHPad A), there can
  still be BBs within Exception A that are reachable from Exception B,
  which also should be taken out. Please refer to the comments in the
  code for more detailed explanation on why this can happen. To make
  this possible, this splits `WebAssemblyException::addBlock` into two
  parts: adding to a set and adding to a vector. We need to iterate on
  BBs within a `WebAssemblyException` to fix this, so we add BBs to sets
  first. But we add BBs to vectors later after we fix all incorrectness
  because deleting BBs from vectors is expensive. I considered removing
  the vector from `WebAssemblyException`, but it was not easy because
  this class has to maintain a similar interface with `MachineLoop` to
  be wrapped into a single interface `SortRegion`, which is used in
  CFGSort.

Other misc. drive-by fixes:
- Make `WebAssemblyExceptionInfo` do not even run when wasm EH is not
  used or the function doesn't have any EH pads, not to waste time
- Add `LLVM_DEBUG` lines for easy debugging
- Fix `preds` comments in cfg-stackify-eh.ll
- Fix `__cxa_throw`'s signature in cfg-stackify-eh.ll

Fixes https://github.com/emscripten-core/emscripten/issues/13554.

Reviewed By: dschuff, tlively

Differential Revision: https://reviews.llvm.org/D97677
2021-03-02 13:44:09 -08:00

177 lines
6.6 KiB
C++

//===-- 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<std::unique_ptr<WebAssemblyException>> SubExceptions;
std::vector<MachineBasicBlock *> Blocks;
SmallPtrSet<MachineBasicBlock *, 8> BlockSet;
public:
WebAssemblyException(MachineBasicBlock *EHPad) : EHPad(EHPad) {}
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 addToBlocksSet(MachineBasicBlock *MBB) { BlockSet.insert(MBB); }
void removeFromBlocksSet(MachineBasicBlock *MBB) { BlockSet.erase(MBB); }
void addToBlocksVector(MachineBasicBlock *MBB) { Blocks.push_back(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; }
SmallPtrSetImpl<MachineBasicBlock *> &getBlocksSet() { return BlockSet; }
const std::vector<std::unique_ptr<WebAssemblyException>> &
getSubExceptions() const {
return SubExceptions;
}
std::vector<std::unique_ptr<WebAssemblyException>> &getSubExceptions() {
return SubExceptions;
}
void addSubException(std::unique_ptr<WebAssemblyException> E) {
SubExceptions.push_back(std::move(E));
}
using iterator = typename decltype(SubExceptions)::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<std::unique_ptr<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(MachineFunction &MF, 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(const MachineBasicBlock *MBB,
WebAssemblyException *WE) {
if (!WE) {
BBMap.erase(MBB);
return;
}
BBMap[MBB] = WE;
}
void addTopLevelException(std::unique_ptr<WebAssemblyException> WE) {
assert(!WE->getParentException() && "Not a top level exception!");
TopLevelExceptions.push_back(std::move(WE));
}
void print(raw_ostream &OS, const Module *M = nullptr) const override;
};
} // end namespace llvm
#endif