diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-04-07 17:54:02 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-04-07 17:54:02 -0700 |
commit | 0d267afdf948de326055e58a94e5f48d9310a52a (patch) | |
tree | 0234141cfa340ebd5ba2a263d025d837a98ab992 /src/wasm-traversal.h | |
parent | 24b0cf11a4b2482ceae7d9e64af576ce2f472479 (diff) | |
download | binaryen-0d267afdf948de326055e58a94e5f48d9310a52a.tar.gz binaryen-0d267afdf948de326055e58a94e5f48d9310a52a.tar.bz2 binaryen-0d267afdf948de326055e58a94e5f48d9310a52a.zip |
derecurse blocks helper, and use it in SimplifyLocals
Diffstat (limited to 'src/wasm-traversal.h')
-rw-r--r-- | src/wasm-traversal.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/wasm-traversal.h b/src/wasm-traversal.h index 04db3cb35..24ec4905c 100644 --- a/src/wasm-traversal.h +++ b/src/wasm-traversal.h @@ -98,6 +98,32 @@ struct WasmVisitor { #undef DELEGATE + // Helper method to de-recurse blocks, which often nest in their first position very heavily + void derecurseBlocks(Block* block, std::function<void (Block*)> preBlock, + std::function<void (Block*, Expression*&)> onChild, + std::function<void (Block*)> postBlock) { + std::vector<Block*> stack; + stack.push_back(block); + while (block->list.size() > 0 && block->list[0]->is<Block>()) { + block = block->list[0]->cast<Block>(); + stack.push_back(block); + } + for (size_t i = 0; i < stack.size(); i++) { + preBlock(stack[i]); + } + for (int i = int(stack.size()) - 1; i >= 0; i--) { + auto* block = stack[i]; + auto& list = block->list; + for (size_t j = 0; j < list.size(); j++) { + if (i < int(stack.size()) - 1 && j == 0) { + // nested block, we already called its pre + } else { + onChild(block, list[j]); + } + } + postBlock(block); + } + } }; // |