diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-01 15:50:59 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-01 15:50:59 -0800 |
commit | b66a85cb7379b1ec604cceed7af101227550d710 (patch) | |
tree | ca82d3b17e2d6a9cd277c54ac8fb9417bdd1f1b7 /src/asm2wasm.h | |
parent | b2e067893efbe22132b210d5af9026b7bd59c270 (diff) | |
download | binaryen-b66a85cb7379b1ec604cceed7af101227550d710.tar.gz binaryen-b66a85cb7379b1ec604cceed7af101227550d710.tar.bz2 binaryen-b66a85cb7379b1ec604cceed7af101227550d710.zip |
optimize away breaks at the end of a block to that same block
Diffstat (limited to 'src/asm2wasm.h')
-rw-r--r-- | src/asm2wasm.h | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 94107ae78..448cf2f0f 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -1033,11 +1033,18 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { } void Asm2WasmBuilder::optimize() { - struct BlockRemover : public WasmWalker { - BlockRemover() : WasmWalker(nullptr) {} + struct BlockBreakOptimizer : public WasmWalker { + BlockBreakOptimizer() : WasmWalker(nullptr) {} Expression* visitBlock(Block *curr) override { - if (curr->list.size() != 1) return curr; + if (curr->list.size() > 1) { + // we can't remove the block, but if it ends in a break on this very block, then just put the value there + Break *last = curr->list[curr->list.size()-1]->dyn_cast<Break>(); + if (last && last->value && last->name == curr->name) { + curr->list[curr->list.size()-1] = last->value; + } + return curr; + } // just one element; maybe we can return just the element if (curr->name.isNull()) return curr->list[0]; // we might be broken to, but if it's a trivial singleton child break, we can optimize here as well @@ -1064,9 +1071,9 @@ void Asm2WasmBuilder::optimize() { } }; - BlockRemover blockRemover; + BlockBreakOptimizer blockBreakOptimizer; for (auto function : wasm.functions) { - blockRemover.startWalk(function); + blockBreakOptimizer.startWalk(function); } } |