diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-04-20 18:39:19 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-04-20 18:39:19 -0700 |
commit | 34a74e9e72d2a572bb8ae9f820f4b553fa06632a (patch) | |
tree | 245a75228bcda9d9ab627186c7252454fcb9ac70 /src/ast_utils.h | |
parent | c2d9d476b853212e96f301106cf00ceb19baefca (diff) | |
parent | 7ce2aefd8e14d6507fbe4abdcbbba9b0507944f0 (diff) | |
download | binaryen-34a74e9e72d2a572bb8ae9f820f4b553fa06632a.tar.gz binaryen-34a74e9e72d2a572bb8ae9f820f4b553fa06632a.tar.bz2 binaryen-34a74e9e72d2a572bb8ae9f820f4b553fa06632a.zip |
Merge pull request #374 from WebAssembly/opts
A few tiny optimization tweaks
Diffstat (limited to 'src/ast_utils.h')
-rw-r--r-- | src/ast_utils.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/ast_utils.h b/src/ast_utils.h index f91cfab24..9336d2b3a 100644 --- a/src/ast_utils.h +++ b/src/ast_utils.h @@ -117,6 +117,37 @@ struct ExpressionManipulator { } }; +struct ExpressionAnalyzer { + // Given a stack of expressions, checks if the topmost is used as a result. + // For example, if the parent is a block and the node is before the last position, + // it is not used. + static bool isResultUsed(std::vector<Expression*> stack, Function* func) { + for (int i = int(stack.size()) - 2; i >= 0; i--) { + auto* curr = stack[i]; + auto* above = stack[i + 1]; + // only if and block can drop values + if (curr->is<Block>()) { + auto* block = curr->cast<Block>(); + for (size_t j = 0; j < block->list.size() - 1; j++) { + if (block->list[j] == above) return false; + } + assert(block->list.back() == above); + // continue down + } else if (curr->is<If>()) { + auto* iff = curr->cast<If>(); + if (!iff->ifFalse) return false; + if (above == iff->condition) return true; + assert(above == iff->ifTrue || above == iff->ifFalse); + // continue down + } else { + return true; // all other node types use the result + } + } + // The value might be used, so it depends on if the function returns + return func->result != none; + } +}; + } // namespace wasm #endif // wasm_ast_utils_h |