From dd778824cfc60c4478bbe7690769f444dad92803 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 26 Apr 2016 17:39:14 -0700 Subject: ast_utils improvements (#399) * make EffectAnalyzer a little more fun to use * create a convert() method that can turn a node into a smaller node, reusing its memory, and use that in nop() * use convert in wasm-linker --- src/ast_utils.h | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/ast_utils.h') diff --git a/src/ast_utils.h b/src/ast_utils.h index 55499d808..66dbf039a 100644 --- a/src/ast_utils.h +++ b/src/ast_utils.h @@ -49,6 +49,11 @@ struct BreakSeeker : public PostWalker> { // TODO: optimize struct EffectAnalyzer : public PostWalker> { + EffectAnalyzer() {} + EffectAnalyzer(Expression *ast) { + walk(ast); + } + bool branches = false; bool calls = false; std::set localsRead; @@ -115,9 +120,21 @@ struct EffectAnalyzer : public PostWalker(target) = Nop(); + // Re-use a node's memory. This helps avoid allocation when optimizing. + template + static OutputType* convert(InputType *input) { + static_assert(sizeof(OutputType) <= sizeof(InputType), + "Can only convert to a smaller size Expression node"); + input->~InputType(); // arena-allocaed, so no destructor, but avoid UB. + OutputType* output = (OutputType*)(input); + new (output) OutputType; + return output; + } + + // Convenience method for nop, which is a common conversion + template + static void nop(InputType* target) { + convert(target); } }; -- cgit v1.2.3