diff options
author | Alon Zakai <alonzakai@gmail.com> | 2017-09-09 10:01:34 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2017-09-09 10:01:34 -0700 |
commit | 67133b57486339249f65872b5959873fef390455 (patch) | |
tree | c2ccac8f11d6e35ceb7e6f48e112e36627e58b75 | |
parent | cec102e94e594a9e74d6e5940a6899168daf6d5f (diff) | |
download | binaryen-67133b57486339249f65872b5959873fef390455.tar.gz binaryen-67133b57486339249f65872b5959873fef390455.tar.bz2 binaryen-67133b57486339249f65872b5959873fef390455.zip |
code-folding must propagate types when it optimizes something
-rw-r--r-- | src/passes/CodeFolding.cpp | 9 | ||||
-rw-r--r-- | test/passes/code-folding.txt | 24 | ||||
-rw-r--r-- | test/passes/code-folding.wast | 23 |
3 files changed, 53 insertions, 3 deletions
diff --git a/src/passes/CodeFolding.cpp b/src/passes/CodeFolding.cpp index ae2f81283..047eee452 100644 --- a/src/passes/CodeFolding.cpp +++ b/src/passes/CodeFolding.cpp @@ -226,13 +226,17 @@ struct CodeFolding : public WalkerPass<ControlFlowWalker<CodeFolding>> { // optimize returns at the end, so we can benefit from a fallthrough if there is a value TODO: separate passes for them? optimizeTerminatingTails(returnTails); // TODO add fallthrough for returns - // TODO optimzier returns not in blocks, a big return value can be worth it + // TODO optimize returns not in blocks, a big return value can be worth it // clean up breakTails.clear(); unreachableTails.clear(); returnTails.clear(); unoptimizables.clear(); modifieds.clear(); + // if we did any work, types may need to be propagated + if (anotherPass) { + ReFinalize().walkFunctionInModule(func, getModule()); + } } } @@ -371,8 +375,7 @@ private: if (!tail.isFallthrough()) { tail.block->list.push_back(last); } - // the blocks lose their endings, so any values are gone, and the blocks - // are now either none or unreachable + // the block type may change if we removed final values tail.block->finalize(); } // since we managed a merge, then it might open up more opportunities later diff --git a/test/passes/code-folding.txt b/test/passes/code-folding.txt new file mode 100644 index 000000000..9143b9615 --- /dev/null +++ b/test/passes/code-folding.txt @@ -0,0 +1,24 @@ +(module + (type $13 (func (param f32))) + (type $1 (func)) + (table 282 282 anyfunc) + (memory $0 1 1) + (func $0 (type $1) + (block $label$1 + (if + (i32.const 1) + (block + (block $label$3 + (call_indirect $13 + (block $label$4 + (br $label$3) + ) + (i32.const 105) + ) + ) + (nop) + ) + ) + ) + ) +) diff --git a/test/passes/code-folding.wast b/test/passes/code-folding.wast new file mode 100644 index 000000000..2310f9721 --- /dev/null +++ b/test/passes/code-folding.wast @@ -0,0 +1,23 @@ +(module + (type $13 (func (param f32))) + (table 282 282 anyfunc) + (memory $0 1 1) + (func $0 + (block $label$1 + (if + (i32.const 1) + (block $label$3 + (call_indirect $13 + (block $label$4 (result f32) ;; but this type may change dangerously + (nop) ;; fold this + (br $label$3) + ) + (i32.const 105) + ) + (nop) ;; with this + ) + ) + ) + ) +) + |