diff options
author | Heejin Ahn <aheejin@gmail.com> | 2020-02-26 18:20:24 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-26 18:20:24 -0800 |
commit | f79faeb14e4da75f131c00c26796ae25ee96a7b7 (patch) | |
tree | 5b954daf160f83ae8227520055fb5b32fc1272f3 /src | |
parent | b17e84b491a309c9f15e7a502f115ece19404b11 (diff) | |
download | binaryen-f79faeb14e4da75f131c00c26796ae25ee96a7b7.tar.gz binaryen-f79faeb14e4da75f131c00c26796ae25ee96a7b7.tar.bz2 binaryen-f79faeb14e4da75f131c00c26796ae25ee96a7b7.zip |
Add EH support for CodeFolding (#2665)
This does two things:
- Treats the target branch of `br_on_exn` as unoptimizables, because it
is a conditional branch.
- Makes sure we don't move expressions that contain `exnref.pop`, which
should follow right after `catch`.
- Adds `containsChild` utility function, which can search all children,
optionally with limited depth. This was actually added to be used in
CodeFolding but ended up not being used, but wasn't removed in case
there will be uses later.
Diffstat (limited to 'src')
-rw-r--r-- | src/ir/iteration.h | 24 | ||||
-rw-r--r-- | src/passes/CodeFolding.cpp | 11 |
2 files changed, 35 insertions, 0 deletions
diff --git a/src/ir/iteration.h b/src/ir/iteration.h index 289e5fa01..fd275f749 100644 --- a/src/ir/iteration.h +++ b/src/ir/iteration.h @@ -82,6 +82,30 @@ public: Iterator end() const { return Iterator(*this, children.size()); } }; +// Returns true if the current expression contains a certain kind of expression, +// within the given depth of BFS. If depth is -1, this searches all children. +template<typename T> bool containsChild(Expression* parent, int depth = -1) { + std::vector<Expression*> exprs; + std::vector<Expression*> nextExprs; + exprs.push_back(parent); + while (!exprs.empty() && depth > 0) { + for (auto* expr : exprs) { + for (auto* child : ChildIterator(expr)) { + if (child->is<T>()) { + return true; + } + nextExprs.push_back(child); + } + } + exprs.swap(nextExprs); + nextExprs.clear(); + if (depth > 0) { + depth--; + } + } + return false; +} + } // namespace wasm #endif // wasm_ir_iteration_h diff --git a/src/passes/CodeFolding.cpp b/src/passes/CodeFolding.cpp index e4d24e33d..70a357bbf 100644 --- a/src/passes/CodeFolding.cpp +++ b/src/passes/CodeFolding.cpp @@ -59,6 +59,7 @@ #include "ir/branch-utils.h" #include "ir/effects.h" +#include "ir/find_all.h" #include "ir/label-utils.h" #include "ir/utils.h" #include "pass.h" @@ -154,6 +155,8 @@ struct CodeFolding : public WalkerPass<ControlFlowWalker<CodeFolding>> { unoptimizables.insert(curr->default_); } + void visitBrOnExn(BrOnExn* curr) { unoptimizables.insert(curr->name); } + void visitUnreachable(Unreachable* curr) { // we can only optimize if we are at the end of the parent block if (!controlFlowStack.empty()) { @@ -301,6 +304,14 @@ private: // anything exiting that is in all targets is something bad return false; } + // Currently pop instructions are only used for exnref.pop, which is a + // pseudo instruction following a catch. We check if the current + // expression has a pop child. This can be overly conservative, because + // this can also exclude whole try-catches that contain a pop within them. + if (getModule()->features.hasExceptionHandling() && + !FindAll<Pop>(item).list.empty()) { + return false; + } } return true; } |