diff options
author | Alon Zakai <alonzakai@gmail.com> | 2018-11-20 09:25:16 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-20 09:25:16 -0800 |
commit | 7ca9e24aa22bc57a4d37d3018cd02cf39cd9957a (patch) | |
tree | 4290dc66afe4c20697186216d9911397bfa2e872 /src/ir/branch-utils.h | |
parent | 801ff52bd0e7696ff105efd2a46932fa5f076708 (diff) | |
download | binaryen-7ca9e24aa22bc57a4d37d3018cd02cf39cd9957a.tar.gz binaryen-7ca9e24aa22bc57a4d37d3018cd02cf39cd9957a.tar.bz2 binaryen-7ca9e24aa22bc57a4d37d3018cd02cf39cd9957a.zip |
Switch optimizations in remove-unused-brs (#1753)
* Switch optimizations in remove-unused-brs: thread switch jumps, and turn a switch with all identical targets into a br
* refinalize in interm operations in remove-unused-brs, as we can be confused by it
Diffstat (limited to 'src/ir/branch-utils.h')
-rw-r--r-- | src/ir/branch-utils.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/ir/branch-utils.h b/src/ir/branch-utils.h index 84be9f897..6f9299bf5 100644 --- a/src/ir/branch-utils.h +++ b/src/ir/branch-utils.h @@ -45,6 +45,40 @@ inline bool isBranchReachable(Expression* expr) { WASM_UNREACHABLE(); } +inline std::set<Name> getUniqueTargets(Switch* sw) { + std::set<Name> ret; + for (auto target : sw->targets) { + ret.insert(target); + } + ret.insert(sw->default_); + return ret; +} + +// If we branch to 'from', change that to 'to' instead. +inline bool replacePossibleTarget(Expression* branch, Name from, Name to) { + bool worked = false; + if (auto* br = branch->dynCast<Break>()) { + if (br->name == from) { + br->name = to; + worked = true; + } + } else if (auto* sw = branch->dynCast<Switch>()) { + for (auto& target : sw->targets) { + if (target == from) { + target = to; + worked = true; + } + } + if (sw->default_ == from) { + sw->default_ = to; + worked = true; + } + } else { + WASM_UNREACHABLE(); + } + return worked; +} + // returns the set of targets to which we branch that are // outside of a node inline std::set<Name> getExitingBranches(Expression* ast) { |