From b66a85cb7379b1ec604cceed7af101227550d710 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Sun, 1 Nov 2015 15:50:59 -0800 Subject: optimize away breaks at the end of a block to that same block --- src/asm2wasm.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') 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(); + 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); } } -- cgit v1.2.3