2017-02-28 06:38:58 +08:00
|
|
|
//===-- WebAssemblyCFGSort.cpp - CFG Sorting ------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-02-28 06:38:58 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file implements a CFG sorting pass.
|
2017-02-28 06:38:58 +08:00
|
|
|
///
|
|
|
|
/// This pass reorders the blocks in a function to put them into topological
|
2018-08-08 04:19:23 +08:00
|
|
|
/// order, ignoring loop backedges, and without any loop or exception being
|
|
|
|
/// interrupted by a block not dominated by the its header, with special care
|
|
|
|
/// to keep the order as similar as possible to the original order.
|
2017-02-28 06:38:58 +08:00
|
|
|
///
|
|
|
|
////===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "WebAssembly.h"
|
2018-08-08 04:19:23 +08:00
|
|
|
#include "WebAssemblyExceptionInfo.h"
|
2017-02-28 06:38:58 +08:00
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "WebAssemblyUtilities.h"
|
|
|
|
#include "llvm/ADT/PriorityQueue.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "wasm-cfg-sort"
|
|
|
|
|
[WebAssembly] Don't analyze branches after CFGStackify
Summary:
`WebAssembly::analyzeBranch` now does not analyze anything if the
function is CFG stackified. We were previously doing similar things by
checking if a branch's operand is whether an integer or an MBB, but this
failed to bail out when a BB did not have any terminators.
Consider this case:
```
bb0:
try $label0
call @foo // unwinds to %ehpad
bb1:
...
br $label0 // jumps to %cont. can be deleted
ehpad:
catch
...
cont:
end_try
```
Here `br $label0` will be deleted in CFGStackify's
`removeUnnecessaryInstrs` function, because we jump to the %cont block
even without the branch. But in this case, MachineVerifier fails to
verify this, because `ehpad` is not a successor of `bb1` even if `bb1`
does not have any terminators. MachineVerifier incorrectly thinks `bb1`
falls through to the next block.
This pass now consistently rejects all analysis after CFGStackify
whether a BB has terminators or not, also making the MachineVerifier
work. (MachineVerifier does not try to verify relationships between BBs
if `analyzeBranch` fails, the behavior we want after CFGStackify.)
This also adds a new option `-wasm-disable-ehpad-sort` for testing. This
option helps create the sorted order we want to test, and without the
fix in this patch, the tests in cfg-stackify-eh.ll fail at
MachineVerifier with `-wasm-disable-ehpad-sort`.
Reviewers: dschuff
Subscribers: sunfish, sbc100, jgravelle-google, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59740
llvm-svn: 357015
2019-03-27 02:21:20 +08:00
|
|
|
// Option to disable EH pad first sorting. Only for testing unwind destination
|
|
|
|
// mismatches in CFGStackify.
|
|
|
|
static cl::opt<bool> WasmDisableEHPadSort(
|
|
|
|
"wasm-disable-ehpad-sort", cl::ReallyHidden,
|
|
|
|
cl::desc(
|
|
|
|
"WebAssembly: Disable EH pad-first sort order. Testing purpose only."),
|
|
|
|
cl::init(false));
|
|
|
|
|
2017-02-28 06:38:58 +08:00
|
|
|
namespace {
|
2018-08-08 04:19:23 +08:00
|
|
|
|
|
|
|
// Wrapper for loops and exceptions
|
|
|
|
class Region {
|
|
|
|
public:
|
|
|
|
virtual ~Region() = default;
|
|
|
|
virtual MachineBasicBlock *getHeader() const = 0;
|
|
|
|
virtual bool contains(const MachineBasicBlock *MBB) const = 0;
|
|
|
|
virtual unsigned getNumBlocks() const = 0;
|
|
|
|
using block_iterator = typename ArrayRef<MachineBasicBlock *>::const_iterator;
|
|
|
|
virtual iterator_range<block_iterator> blocks() const = 0;
|
|
|
|
virtual bool isLoop() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> class ConcreteRegion : public Region {
|
|
|
|
const T *Region;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ConcreteRegion(const T *Region) : Region(Region) {}
|
|
|
|
MachineBasicBlock *getHeader() const override { return Region->getHeader(); }
|
|
|
|
bool contains(const MachineBasicBlock *MBB) const override {
|
|
|
|
return Region->contains(MBB);
|
|
|
|
}
|
|
|
|
unsigned getNumBlocks() const override { return Region->getNumBlocks(); }
|
|
|
|
iterator_range<block_iterator> blocks() const override {
|
|
|
|
return Region->blocks();
|
|
|
|
}
|
|
|
|
bool isLoop() const override { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> bool ConcreteRegion<MachineLoop>::isLoop() const { return true; }
|
|
|
|
|
|
|
|
// This class has information of nested Regions; this is analogous to what
|
|
|
|
// LoopInfo is for loops.
|
|
|
|
class RegionInfo {
|
|
|
|
const MachineLoopInfo &MLI;
|
|
|
|
const WebAssemblyExceptionInfo &WEI;
|
|
|
|
DenseMap<const MachineLoop *, std::unique_ptr<Region>> LoopMap;
|
|
|
|
DenseMap<const WebAssemblyException *, std::unique_ptr<Region>> ExceptionMap;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RegionInfo(const MachineLoopInfo &MLI, const WebAssemblyExceptionInfo &WEI)
|
|
|
|
: MLI(MLI), WEI(WEI) {}
|
|
|
|
|
|
|
|
// Returns a smallest loop or exception that contains MBB
|
|
|
|
const Region *getRegionFor(const MachineBasicBlock *MBB) {
|
|
|
|
const auto *ML = MLI.getLoopFor(MBB);
|
|
|
|
const auto *WE = WEI.getExceptionFor(MBB);
|
|
|
|
if (!ML && !WE)
|
|
|
|
return nullptr;
|
[WebAssembly] Fix subregion relationship in CFGSort
Summary:
The previous code for determining the innermost region in CFGSort was
not correct. We determine subregion relationship by domination of their
headers, i.e., if region A's header dominates region B's header, B is a
subregion of A. Previously we assumed that if a BB belongs to both a
loop and an exception, the region with fewer number of BBs is the
innermost one. This may not be true, because while WebAssemblyException
contains BBs in all its subregions (loops or exceptions), MachineLoop
may not, because MachineLoop does not contain BBs that don't have a path
to its header even if they are dominated by its header.
Loop header <---|
| |
Exception header |
| \ |
A B |
| \ |
| C |
| |
Loop latch |
| |
-------------|
For example, in this CFG, the loop does not contain B and C, because
they don't have a path back to the loops header. But for CFGSort we
consider the exception here belongs to the loop and the exception should
be a subregion of the loop and scheduled together.
So here we should use `WE->contains(ML->getHeader())` (but not
`ML->contains(WE->getHeader())`, for the stated region above).
This also fixes some comments and deletes `Regions` vector in
`RegionInfo` class, which was not used anywere.
Reviewers: dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77181
2020-03-30 18:42:40 +08:00
|
|
|
// We determine subregion relationship by domination of their headers, i.e.,
|
|
|
|
// if region A's header dominates region B's header, B is a subregion of A.
|
|
|
|
// WebAssemblyException contains BBs in all its subregions (loops or
|
|
|
|
// exceptions), but MachineLoop may not, because MachineLoop does not contain
|
|
|
|
// BBs that don't have a path to its header even if they are dominated by
|
|
|
|
// its header. So here we should use WE->contains(ML->getHeader()), but not
|
|
|
|
// ML->contains(WE->getHeader()).
|
|
|
|
if ((ML && !WE) || (ML && WE && WE->contains(ML->getHeader()))) {
|
2018-08-08 04:19:23 +08:00
|
|
|
// If the smallest region containing MBB is a loop
|
|
|
|
if (LoopMap.count(ML))
|
|
|
|
return LoopMap[ML].get();
|
2019-08-15 23:54:37 +08:00
|
|
|
LoopMap[ML] = std::make_unique<ConcreteRegion<MachineLoop>>(ML);
|
2018-08-08 04:19:23 +08:00
|
|
|
return LoopMap[ML].get();
|
|
|
|
} else {
|
|
|
|
// If the smallest region containing MBB is an exception
|
|
|
|
if (ExceptionMap.count(WE))
|
|
|
|
return ExceptionMap[WE].get();
|
|
|
|
ExceptionMap[WE] =
|
2019-08-15 23:54:37 +08:00
|
|
|
std::make_unique<ConcreteRegion<WebAssemblyException>>(WE);
|
2018-08-08 04:19:23 +08:00
|
|
|
return ExceptionMap[WE].get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-28 06:38:58 +08:00
|
|
|
class WebAssemblyCFGSort final : public MachineFunctionPass {
|
|
|
|
StringRef getPassName() const override { return "WebAssembly CFG Sort"; }
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<MachineDominatorTree>();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
2018-08-08 04:19:23 +08:00
|
|
|
AU.addRequired<WebAssemblyExceptionInfo>();
|
|
|
|
AU.addPreserved<WebAssemblyExceptionInfo>();
|
2017-02-28 06:38:58 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
WebAssemblyCFGSort() : MachineFunctionPass(ID) {}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char WebAssemblyCFGSort::ID = 0;
|
2018-03-31 04:36:58 +08:00
|
|
|
INITIALIZE_PASS(WebAssemblyCFGSort, DEBUG_TYPE,
|
|
|
|
"Reorders blocks in topological order", false, false)
|
|
|
|
|
2017-02-28 06:38:58 +08:00
|
|
|
FunctionPass *llvm::createWebAssemblyCFGSort() {
|
|
|
|
return new WebAssemblyCFGSort();
|
|
|
|
}
|
|
|
|
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
static void maybeUpdateTerminator(MachineBasicBlock *MBB) {
|
2017-02-28 06:38:58 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
bool AnyBarrier = false;
|
|
|
|
#endif
|
|
|
|
bool AllAnalyzable = true;
|
|
|
|
for (const MachineInstr &Term : MBB->terminators()) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
AnyBarrier |= Term.isBarrier();
|
|
|
|
#endif
|
|
|
|
AllAnalyzable &= Term.isBranch() && !Term.isIndirectBranch();
|
|
|
|
}
|
|
|
|
assert((AnyBarrier || AllAnalyzable) &&
|
2020-01-21 23:47:35 +08:00
|
|
|
"analyzeBranch needs to analyze any block with a fallthrough");
|
2017-02-28 06:38:58 +08:00
|
|
|
if (AllAnalyzable)
|
|
|
|
MBB->updateTerminator();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2018-08-08 04:19:23 +08:00
|
|
|
// EH pads are selected first regardless of the block comparison order.
|
|
|
|
// When only one of the BBs is an EH pad, we give a higher priority to it, to
|
|
|
|
// prevent common mismatches between possibly throwing calls and ehpads they
|
|
|
|
// unwind to, as in the example below:
|
|
|
|
//
|
|
|
|
// bb0:
|
|
|
|
// call @foo // If this throws, unwind to bb2
|
|
|
|
// bb1:
|
|
|
|
// call @bar // If this throws, unwind to bb3
|
|
|
|
// bb2 (ehpad):
|
|
|
|
// handler_bb2
|
|
|
|
// bb3 (ehpad):
|
|
|
|
// handler_bb3
|
|
|
|
// continuing code
|
|
|
|
//
|
|
|
|
// Because this pass tries to preserve the original BB order, this order will
|
|
|
|
// not change. But this will result in this try-catch structure in CFGStackify,
|
|
|
|
// resulting in a mismatch:
|
|
|
|
// try
|
|
|
|
// try
|
|
|
|
// call @foo
|
|
|
|
// call @bar // This should unwind to bb3, not bb2!
|
|
|
|
// catch
|
|
|
|
// handler_bb2
|
|
|
|
// end
|
|
|
|
// catch
|
|
|
|
// handler_bb3
|
|
|
|
// end
|
|
|
|
// continuing code
|
|
|
|
//
|
|
|
|
// If we give a higher priority to an EH pad whenever it is ready in this
|
|
|
|
// example, when both bb1 and bb2 are ready, we would pick up bb2 first.
|
|
|
|
|
2017-02-28 06:38:58 +08:00
|
|
|
/// Sort blocks by their number.
|
|
|
|
struct CompareBlockNumbers {
|
|
|
|
bool operator()(const MachineBasicBlock *A,
|
|
|
|
const MachineBasicBlock *B) const {
|
[WebAssembly] Don't analyze branches after CFGStackify
Summary:
`WebAssembly::analyzeBranch` now does not analyze anything if the
function is CFG stackified. We were previously doing similar things by
checking if a branch's operand is whether an integer or an MBB, but this
failed to bail out when a BB did not have any terminators.
Consider this case:
```
bb0:
try $label0
call @foo // unwinds to %ehpad
bb1:
...
br $label0 // jumps to %cont. can be deleted
ehpad:
catch
...
cont:
end_try
```
Here `br $label0` will be deleted in CFGStackify's
`removeUnnecessaryInstrs` function, because we jump to the %cont block
even without the branch. But in this case, MachineVerifier fails to
verify this, because `ehpad` is not a successor of `bb1` even if `bb1`
does not have any terminators. MachineVerifier incorrectly thinks `bb1`
falls through to the next block.
This pass now consistently rejects all analysis after CFGStackify
whether a BB has terminators or not, also making the MachineVerifier
work. (MachineVerifier does not try to verify relationships between BBs
if `analyzeBranch` fails, the behavior we want after CFGStackify.)
This also adds a new option `-wasm-disable-ehpad-sort` for testing. This
option helps create the sorted order we want to test, and without the
fix in this patch, the tests in cfg-stackify-eh.ll fail at
MachineVerifier with `-wasm-disable-ehpad-sort`.
Reviewers: dschuff
Subscribers: sunfish, sbc100, jgravelle-google, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59740
llvm-svn: 357015
2019-03-27 02:21:20 +08:00
|
|
|
if (!WasmDisableEHPadSort) {
|
|
|
|
if (A->isEHPad() && !B->isEHPad())
|
|
|
|
return false;
|
|
|
|
if (!A->isEHPad() && B->isEHPad())
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-08 04:19:23 +08:00
|
|
|
|
2017-02-28 06:38:58 +08:00
|
|
|
return A->getNumber() > B->getNumber();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/// Sort blocks by their number in the opposite order..
|
|
|
|
struct CompareBlockNumbersBackwards {
|
|
|
|
bool operator()(const MachineBasicBlock *A,
|
|
|
|
const MachineBasicBlock *B) const {
|
[WebAssembly] Don't analyze branches after CFGStackify
Summary:
`WebAssembly::analyzeBranch` now does not analyze anything if the
function is CFG stackified. We were previously doing similar things by
checking if a branch's operand is whether an integer or an MBB, but this
failed to bail out when a BB did not have any terminators.
Consider this case:
```
bb0:
try $label0
call @foo // unwinds to %ehpad
bb1:
...
br $label0 // jumps to %cont. can be deleted
ehpad:
catch
...
cont:
end_try
```
Here `br $label0` will be deleted in CFGStackify's
`removeUnnecessaryInstrs` function, because we jump to the %cont block
even without the branch. But in this case, MachineVerifier fails to
verify this, because `ehpad` is not a successor of `bb1` even if `bb1`
does not have any terminators. MachineVerifier incorrectly thinks `bb1`
falls through to the next block.
This pass now consistently rejects all analysis after CFGStackify
whether a BB has terminators or not, also making the MachineVerifier
work. (MachineVerifier does not try to verify relationships between BBs
if `analyzeBranch` fails, the behavior we want after CFGStackify.)
This also adds a new option `-wasm-disable-ehpad-sort` for testing. This
option helps create the sorted order we want to test, and without the
fix in this patch, the tests in cfg-stackify-eh.ll fail at
MachineVerifier with `-wasm-disable-ehpad-sort`.
Reviewers: dschuff
Subscribers: sunfish, sbc100, jgravelle-google, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59740
llvm-svn: 357015
2019-03-27 02:21:20 +08:00
|
|
|
if (!WasmDisableEHPadSort) {
|
|
|
|
if (A->isEHPad() && !B->isEHPad())
|
|
|
|
return false;
|
|
|
|
if (!A->isEHPad() && B->isEHPad())
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-08 04:19:23 +08:00
|
|
|
|
2017-02-28 06:38:58 +08:00
|
|
|
return A->getNumber() < B->getNumber();
|
|
|
|
}
|
|
|
|
};
|
2018-08-08 04:19:23 +08:00
|
|
|
/// Bookkeeping for a region to help ensure that we don't mix blocks not
|
|
|
|
/// dominated by the its header among its blocks.
|
2017-02-28 06:38:58 +08:00
|
|
|
struct Entry {
|
2018-08-10 06:35:23 +08:00
|
|
|
const Region *TheRegion;
|
2017-02-28 06:38:58 +08:00
|
|
|
unsigned NumBlocksLeft;
|
|
|
|
|
|
|
|
/// List of blocks not dominated by Loop's header that are deferred until
|
|
|
|
/// after all of Loop's blocks have been seen.
|
|
|
|
std::vector<MachineBasicBlock *> Deferred;
|
|
|
|
|
2018-08-08 04:19:23 +08:00
|
|
|
explicit Entry(const class Region *R)
|
2018-08-10 06:35:23 +08:00
|
|
|
: TheRegion(R), NumBlocksLeft(R->getNumBlocks()) {}
|
2017-02-28 06:38:58 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-08-08 04:19:23 +08:00
|
|
|
/// Sort the blocks, taking special care to make sure that regions are not
|
2017-02-28 06:38:58 +08:00
|
|
|
/// interrupted by blocks not dominated by their header.
|
|
|
|
/// TODO: There are many opportunities for improving the heuristics here.
|
|
|
|
/// Explore them.
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
|
2018-08-08 04:19:23 +08:00
|
|
|
const WebAssemblyExceptionInfo &WEI,
|
2017-02-28 06:38:58 +08:00
|
|
|
const MachineDominatorTree &MDT) {
|
|
|
|
// Prepare for a topological sort: Record the number of predecessors each
|
|
|
|
// block has, ignoring loop backedges.
|
|
|
|
MF.RenumberBlocks();
|
|
|
|
SmallVector<unsigned, 16> NumPredsLeft(MF.getNumBlockIDs(), 0);
|
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
|
|
unsigned N = MBB.pred_size();
|
|
|
|
if (MachineLoop *L = MLI.getLoopFor(&MBB))
|
|
|
|
if (L->getHeader() == &MBB)
|
|
|
|
for (const MachineBasicBlock *Pred : MBB.predecessors())
|
|
|
|
if (L->contains(Pred))
|
|
|
|
--N;
|
|
|
|
NumPredsLeft[MBB.getNumber()] = N;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Topological sort the CFG, with additional constraints:
|
2018-08-08 04:19:23 +08:00
|
|
|
// - Between a region header and the last block in the region, there can be
|
|
|
|
// no blocks not dominated by its header.
|
2017-02-28 06:38:58 +08:00
|
|
|
// - It's desirable to preserve the original block order when possible.
|
|
|
|
// We use two ready lists; Preferred and Ready. Preferred has recently
|
2017-07-09 13:54:44 +08:00
|
|
|
// processed successors, to help preserve block sequences from the original
|
2018-08-08 04:19:23 +08:00
|
|
|
// order. Ready has the remaining ready blocks. EH blocks are picked first
|
|
|
|
// from both queues.
|
2017-02-28 06:38:58 +08:00
|
|
|
PriorityQueue<MachineBasicBlock *, std::vector<MachineBasicBlock *>,
|
|
|
|
CompareBlockNumbers>
|
|
|
|
Preferred;
|
|
|
|
PriorityQueue<MachineBasicBlock *, std::vector<MachineBasicBlock *>,
|
|
|
|
CompareBlockNumbersBackwards>
|
|
|
|
Ready;
|
2018-08-08 04:19:23 +08:00
|
|
|
|
2019-03-25 01:34:40 +08:00
|
|
|
RegionInfo RI(MLI, WEI);
|
2018-08-08 04:19:23 +08:00
|
|
|
SmallVector<Entry, 4> Entries;
|
2017-02-28 06:38:58 +08:00
|
|
|
for (MachineBasicBlock *MBB = &MF.front();;) {
|
2019-03-25 01:34:40 +08:00
|
|
|
const Region *R = RI.getRegionFor(MBB);
|
2018-08-08 04:19:23 +08:00
|
|
|
if (R) {
|
|
|
|
// If MBB is a region header, add it to the active region list. We can't
|
|
|
|
// put any blocks that it doesn't dominate until we see the end of the
|
|
|
|
// region.
|
|
|
|
if (R->getHeader() == MBB)
|
|
|
|
Entries.push_back(Entry(R));
|
|
|
|
// For each active region the block is in, decrement the count. If MBB is
|
|
|
|
// the last block in an active region, take it off the list and pick up
|
|
|
|
// any blocks deferred because the header didn't dominate them.
|
|
|
|
for (Entry &E : Entries)
|
2018-08-10 06:35:23 +08:00
|
|
|
if (E.TheRegion->contains(MBB) && --E.NumBlocksLeft == 0)
|
2017-02-28 06:38:58 +08:00
|
|
|
for (auto DeferredBlock : E.Deferred)
|
|
|
|
Ready.push(DeferredBlock);
|
2018-08-08 04:19:23 +08:00
|
|
|
while (!Entries.empty() && Entries.back().NumBlocksLeft == 0)
|
|
|
|
Entries.pop_back();
|
2017-02-28 06:38:58 +08:00
|
|
|
}
|
|
|
|
// The main topological sort logic.
|
|
|
|
for (MachineBasicBlock *Succ : MBB->successors()) {
|
|
|
|
// Ignore backedges.
|
|
|
|
if (MachineLoop *SuccL = MLI.getLoopFor(Succ))
|
|
|
|
if (SuccL->getHeader() == Succ && SuccL->contains(MBB))
|
|
|
|
continue;
|
|
|
|
// Decrement the predecessor count. If it's now zero, it's ready.
|
|
|
|
if (--NumPredsLeft[Succ->getNumber()] == 0)
|
|
|
|
Preferred.push(Succ);
|
|
|
|
}
|
|
|
|
// Determine the block to follow MBB. First try to find a preferred block,
|
|
|
|
// to preserve the original block order when possible.
|
|
|
|
MachineBasicBlock *Next = nullptr;
|
|
|
|
while (!Preferred.empty()) {
|
|
|
|
Next = Preferred.top();
|
|
|
|
Preferred.pop();
|
2018-08-08 04:19:23 +08:00
|
|
|
// If X isn't dominated by the top active region header, defer it until
|
|
|
|
// that region is done.
|
|
|
|
if (!Entries.empty() &&
|
2018-08-10 06:35:23 +08:00
|
|
|
!MDT.dominates(Entries.back().TheRegion->getHeader(), Next)) {
|
2018-08-08 04:19:23 +08:00
|
|
|
Entries.back().Deferred.push_back(Next);
|
2017-02-28 06:38:58 +08:00
|
|
|
Next = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If Next was originally ordered before MBB, and it isn't because it was
|
|
|
|
// loop-rotated above the header, it's not preferred.
|
|
|
|
if (Next->getNumber() < MBB->getNumber() &&
|
2019-10-01 14:53:28 +08:00
|
|
|
(WasmDisableEHPadSort || !Next->isEHPad()) &&
|
2018-08-08 04:19:23 +08:00
|
|
|
(!R || !R->contains(Next) ||
|
|
|
|
R->getHeader()->getNumber() < Next->getNumber())) {
|
2017-02-28 06:38:58 +08:00
|
|
|
Ready.push(Next);
|
|
|
|
Next = nullptr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// If we didn't find a suitable block in the Preferred list, check the
|
|
|
|
// general Ready list.
|
|
|
|
if (!Next) {
|
|
|
|
// If there are no more blocks to process, we're done.
|
|
|
|
if (Ready.empty()) {
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
maybeUpdateTerminator(MBB);
|
2017-02-28 06:38:58 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
Next = Ready.top();
|
|
|
|
Ready.pop();
|
2018-08-08 04:19:23 +08:00
|
|
|
// If Next isn't dominated by the top active region header, defer it
|
|
|
|
// until that region is done.
|
|
|
|
if (!Entries.empty() &&
|
2018-08-10 06:35:23 +08:00
|
|
|
!MDT.dominates(Entries.back().TheRegion->getHeader(), Next)) {
|
2018-08-08 04:19:23 +08:00
|
|
|
Entries.back().Deferred.push_back(Next);
|
2017-02-28 06:38:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Move the next block into place and iterate.
|
|
|
|
Next->moveAfter(MBB);
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
maybeUpdateTerminator(MBB);
|
2017-02-28 06:38:58 +08:00
|
|
|
MBB = Next;
|
|
|
|
}
|
2018-08-08 04:19:23 +08:00
|
|
|
assert(Entries.empty() && "Active sort region list not finished");
|
2017-02-28 06:38:58 +08:00
|
|
|
MF.RenumberBlocks();
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2018-08-08 04:19:23 +08:00
|
|
|
SmallSetVector<const Region *, 8> OnStack;
|
2017-02-28 06:38:58 +08:00
|
|
|
|
|
|
|
// Insert a sentinel representing the degenerate loop that starts at the
|
|
|
|
// function entry block and includes the entire function as a "loop" that
|
|
|
|
// executes once.
|
|
|
|
OnStack.insert(nullptr);
|
|
|
|
|
|
|
|
for (auto &MBB : MF) {
|
|
|
|
assert(MBB.getNumber() >= 0 && "Renumbered blocks should be non-negative.");
|
2019-03-25 01:34:40 +08:00
|
|
|
const Region *Region = RI.getRegionFor(&MBB);
|
2018-08-08 04:19:23 +08:00
|
|
|
|
|
|
|
if (Region && &MBB == Region->getHeader()) {
|
[WebAssembly] Fix subregion relationship in CFGSort
Summary:
The previous code for determining the innermost region in CFGSort was
not correct. We determine subregion relationship by domination of their
headers, i.e., if region A's header dominates region B's header, B is a
subregion of A. Previously we assumed that if a BB belongs to both a
loop and an exception, the region with fewer number of BBs is the
innermost one. This may not be true, because while WebAssemblyException
contains BBs in all its subregions (loops or exceptions), MachineLoop
may not, because MachineLoop does not contain BBs that don't have a path
to its header even if they are dominated by its header.
Loop header <---|
| |
Exception header |
| \ |
A B |
| \ |
| C |
| |
Loop latch |
| |
-------------|
For example, in this CFG, the loop does not contain B and C, because
they don't have a path back to the loops header. But for CFGSort we
consider the exception here belongs to the loop and the exception should
be a subregion of the loop and scheduled together.
So here we should use `WE->contains(ML->getHeader())` (but not
`ML->contains(WE->getHeader())`, for the stated region above).
This also fixes some comments and deletes `Regions` vector in
`RegionInfo` class, which was not used anywere.
Reviewers: dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77181
2020-03-30 18:42:40 +08:00
|
|
|
// Region header.
|
2018-08-08 04:19:23 +08:00
|
|
|
if (Region->isLoop()) {
|
|
|
|
// Loop header. The loop predecessor should be sorted above, and the
|
|
|
|
// other predecessors should be backedges below.
|
|
|
|
for (auto Pred : MBB.predecessors())
|
|
|
|
assert(
|
|
|
|
(Pred->getNumber() < MBB.getNumber() || Region->contains(Pred)) &&
|
|
|
|
"Loop header predecessors must be loop predecessors or "
|
|
|
|
"backedges");
|
|
|
|
} else {
|
[WebAssembly] Fix subregion relationship in CFGSort
Summary:
The previous code for determining the innermost region in CFGSort was
not correct. We determine subregion relationship by domination of their
headers, i.e., if region A's header dominates region B's header, B is a
subregion of A. Previously we assumed that if a BB belongs to both a
loop and an exception, the region with fewer number of BBs is the
innermost one. This may not be true, because while WebAssemblyException
contains BBs in all its subregions (loops or exceptions), MachineLoop
may not, because MachineLoop does not contain BBs that don't have a path
to its header even if they are dominated by its header.
Loop header <---|
| |
Exception header |
| \ |
A B |
| \ |
| C |
| |
Loop latch |
| |
-------------|
For example, in this CFG, the loop does not contain B and C, because
they don't have a path back to the loops header. But for CFGSort we
consider the exception here belongs to the loop and the exception should
be a subregion of the loop and scheduled together.
So here we should use `WE->contains(ML->getHeader())` (but not
`ML->contains(WE->getHeader())`, for the stated region above).
This also fixes some comments and deletes `Regions` vector in
`RegionInfo` class, which was not used anywere.
Reviewers: dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77181
2020-03-30 18:42:40 +08:00
|
|
|
// Exception header. All predecessors should be sorted above.
|
2018-08-08 04:19:23 +08:00
|
|
|
for (auto Pred : MBB.predecessors())
|
|
|
|
assert(Pred->getNumber() < MBB.getNumber() &&
|
|
|
|
"Non-loop-header predecessors should be topologically sorted");
|
|
|
|
}
|
|
|
|
assert(OnStack.insert(Region) &&
|
|
|
|
"Regions should be declared at most once.");
|
2017-02-28 06:38:58 +08:00
|
|
|
|
|
|
|
} else {
|
[WebAssembly] Fix subregion relationship in CFGSort
Summary:
The previous code for determining the innermost region in CFGSort was
not correct. We determine subregion relationship by domination of their
headers, i.e., if region A's header dominates region B's header, B is a
subregion of A. Previously we assumed that if a BB belongs to both a
loop and an exception, the region with fewer number of BBs is the
innermost one. This may not be true, because while WebAssemblyException
contains BBs in all its subregions (loops or exceptions), MachineLoop
may not, because MachineLoop does not contain BBs that don't have a path
to its header even if they are dominated by its header.
Loop header <---|
| |
Exception header |
| \ |
A B |
| \ |
| C |
| |
Loop latch |
| |
-------------|
For example, in this CFG, the loop does not contain B and C, because
they don't have a path back to the loops header. But for CFGSort we
consider the exception here belongs to the loop and the exception should
be a subregion of the loop and scheduled together.
So here we should use `WE->contains(ML->getHeader())` (but not
`ML->contains(WE->getHeader())`, for the stated region above).
This also fixes some comments and deletes `Regions` vector in
`RegionInfo` class, which was not used anywere.
Reviewers: dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77181
2020-03-30 18:42:40 +08:00
|
|
|
// Not a region header. All predecessors should be sorted above.
|
2017-02-28 06:38:58 +08:00
|
|
|
for (auto Pred : MBB.predecessors())
|
|
|
|
assert(Pred->getNumber() < MBB.getNumber() &&
|
|
|
|
"Non-loop-header predecessors should be topologically sorted");
|
2019-03-25 01:34:40 +08:00
|
|
|
assert(OnStack.count(RI.getRegionFor(&MBB)) &&
|
2018-08-08 04:19:23 +08:00
|
|
|
"Blocks must be nested in their regions");
|
2017-02-28 06:38:58 +08:00
|
|
|
}
|
2018-06-19 08:32:03 +08:00
|
|
|
while (OnStack.size() > 1 && &MBB == WebAssembly::getBottom(OnStack.back()))
|
2017-02-28 06:38:58 +08:00
|
|
|
OnStack.pop_back();
|
|
|
|
}
|
|
|
|
assert(OnStack.pop_back_val() == nullptr &&
|
2018-08-08 04:19:23 +08:00
|
|
|
"The function entry block shouldn't actually be a region header");
|
2017-02-28 06:38:58 +08:00
|
|
|
assert(OnStack.empty() &&
|
|
|
|
"Control flow stack pushes and pops should be balanced.");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyCFGSort::runOnMachineFunction(MachineFunction &MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** CFG Sorting **********\n"
|
|
|
|
"********** Function: "
|
|
|
|
<< MF.getName() << '\n');
|
2017-02-28 06:38:58 +08:00
|
|
|
|
|
|
|
const auto &MLI = getAnalysis<MachineLoopInfo>();
|
2018-08-08 04:19:23 +08:00
|
|
|
const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
|
2017-02-28 06:38:58 +08:00
|
|
|
auto &MDT = getAnalysis<MachineDominatorTree>();
|
|
|
|
// Liveness is not tracked for VALUE_STACK physreg.
|
|
|
|
MF.getRegInfo().invalidateLiveness();
|
|
|
|
|
2018-08-08 04:19:23 +08:00
|
|
|
// Sort the blocks, with contiguous sort regions.
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
sortBlocks(MF, MLI, WEI, MDT);
|
2017-02-28 06:38:58 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|