summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt2
-rwxr-xr-xbuild.sh3
-rw-r--r--src/passes/SimplifyLocals.cpp43
-rw-r--r--test/passes/simplify-locals.txt35
-rw-r--r--test/passes/simplify-locals.wast31
5 files changed, 113 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 124a93fd0..64e675afb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -39,6 +39,7 @@ SET(binaryen-shell_SOURCES
src/passes/RemoveImports.cpp
src/passes/RemoveUnusedBrs.cpp
src/passes/RemoveUnusedNames.cpp
+ src/passes/SimplifyLocals.cpp
)
ADD_EXECUTABLE(binaryen-shell
${binaryen-shell_SOURCES})
@@ -53,6 +54,7 @@ SET(asm2wasm_SOURCES
src/passes/MergeBlocks.cpp
src/passes/RemoveUnusedBrs.cpp
src/passes/RemoveUnusedNames.cpp
+ src/passes/SimplifyLocals.cpp
src/emscripten-optimizer/parser.cpp
src/emscripten-optimizer/simple_ast.cpp
src/emscripten-optimizer/optimizer-shared.cpp
diff --git a/build.sh b/build.sh
index 2191bca0c..71c111c93 100755
--- a/build.sh
+++ b/build.sh
@@ -11,6 +11,7 @@ make -j
#echo "building s2wasm"
#g++ -O2 -std=c++11 src/s2wasm-main.cpp src/support/command-line.cpp src/support/file.cpp src/support/colors.cpp -Isrc/ -o bin/s2wasm
echo "building interpreter/js"
-em++ -std=c++11 src/wasm-js.cpp src/pass.cpp src/passes/MergeBlocks.cpp src/passes/RemoveUnusedBrs.cpp src/passes/RemoveUnusedNames.cpp src/emscripten-optimizer/parser.cpp src/emscripten-optimizer/simple_ast.cpp src/emscripten-optimizer/optimizer-shared.cpp src/support/colors.cpp -Isrc/ -o bin/wasm.js -s MODULARIZE=1 -s 'EXPORT_NAME="WasmJS"' --memory-init-file 0 -Oz -s ALLOW_MEMORY_GROWTH=1 -profiling -s DEMANGLE_SUPPORT=1 #-DWASM_JS_DEBUG -DWASM_INTERPRETER_DEBUG=2
+em++ -std=c++11 src/wasm-js.cpp src/pass.cpp src/passes/MergeBlocks.cpp src/passes/RemoveUnusedBrs.cpp src/passes/RemoveUnusedNames.cpp src/passes/SimplifyLocals.cpp
+ src/emscripten-optimizer/parser.cpp src/emscripten-optimizer/simple_ast.cpp src/emscripten-optimizer/optimizer-shared.cpp src/support/colors.cpp -Isrc/ -o bin/wasm.js -s MODULARIZE=1 -s 'EXPORT_NAME="WasmJS"' --memory-init-file 0 -Oz -s ALLOW_MEMORY_GROWTH=1 -profiling -s DEMANGLE_SUPPORT=1 #-DWASM_JS_DEBUG -DWASM_INTERPRETER_DEBUG=2
cat src/js/wasm.js-post.js >> bin/wasm.js
diff --git a/src/passes/SimplifyLocals.cpp b/src/passes/SimplifyLocals.cpp
new file mode 100644
index 000000000..232c5e2a9
--- /dev/null
+++ b/src/passes/SimplifyLocals.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2015 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//
+// Miscellaneous locals-related optimizations
+//
+
+#include <wasm.h>
+#include <pass.h>
+
+namespace wasm {
+
+struct SimplifyLocals : public Pass {
+ void visitBlock(Block *curr) override {
+ // look for pairs of setlocal-getlocal, which can be just a setlocal (since it returns a value)
+ for (size_t i = 0; i < curr->list.size() - 1; i++) {
+ auto set = curr->list[i]->dyn_cast<SetLocal>();
+ if (!set) continue;
+ auto get = curr->list[i + 1]->dyn_cast<GetLocal>();
+ if (!get) continue;
+ if (set->name != get->name) continue;
+ curr->list.erase(curr->list.begin() + i + 1);
+ i -= 1;
+ }
+ }
+};
+
+static RegisterPass<SimplifyLocals> registerPass("simplify-locals", "miscellaneous locals-related optimizations");
+
+} // namespace wasm
diff --git a/test/passes/simplify-locals.txt b/test/passes/simplify-locals.txt
new file mode 100644
index 000000000..773215ad7
--- /dev/null
+++ b/test/passes/simplify-locals.txt
@@ -0,0 +1,35 @@
+(module
+ (memory 16777216 16777216)
+ (func $b0-yes (param $i1 i32)
+ (local $x i32)
+ (local $y i32)
+ (set_local $x
+ (i32.const 5)
+ )
+ (block $block0
+ (set_local $x
+ (i32.const 7)
+ )
+ )
+ (set_local $x
+ (i32.const 11)
+ )
+ (set_local $x
+ (i32.const 5)
+ )
+ (get_local $y)
+ (block $block1
+ (set_local $x
+ (i32.const 7)
+ )
+ (get_local $y)
+ )
+ (set_local $x
+ (i32.const 11)
+ )
+ (get_local $y)
+ (set_local $x
+ (i32.const 17)
+ )
+ )
+)
diff --git a/test/passes/simplify-locals.wast b/test/passes/simplify-locals.wast
new file mode 100644
index 000000000..fca2f24c7
--- /dev/null
+++ b/test/passes/simplify-locals.wast
@@ -0,0 +1,31 @@
+(module
+ (memory 16777216 16777216)
+ (func $b0-yes (param $i1 i32)
+ (local $x i32)
+ (local $y i32)
+ (set_local $x (i32.const 5))
+ (get_local $x)
+ (block
+ (set_local $x (i32.const 7))
+ (get_local $x)
+ )
+ (set_local $x (i32.const 11))
+ (get_local $x)
+ (set_local $x (i32.const 5))
+ (get_local $y)
+ (block
+ (set_local $x (i32.const 7))
+ (get_local $y)
+ )
+ (set_local $x (i32.const 11))
+ (get_local $y)
+ (set_local $x (i32.const 17))
+ (get_local $x)
+ (get_local $x)
+ (get_local $x)
+ (get_local $x)
+ (get_local $x)
+ (get_local $x)
+ )
+)
+