summaryrefslogtreecommitdiff
path: root/src/tools/wasm2js.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2019-05-13 16:55:06 -0700
committerGitHub <noreply@github.com>2019-05-13 16:55:06 -0700
commitb4eb90ce9bc43d9f3897bd13d6fd11f40f706b5a (patch)
tree0b3b5f9b8974dea18ace6b059e1729635a8ff571 /src/tools/wasm2js.cpp
parent67019f9a72175bc7a098d72aa14a6f6afeb2efce (diff)
downloadbinaryen-b4eb90ce9bc43d9f3897bd13d6fd11f40f706b5a.tar.gz
binaryen-b4eb90ce9bc43d9f3897bd13d6fd11f40f706b5a.tar.bz2
binaryen-b4eb90ce9bc43d9f3897bd13d6fd11f40f706b5a.zip
wasm2js: precompute bitwise operations (#2101)
This happens on e.g. an i32 load of a constant offset, then we have constant >> 2.
Diffstat (limited to 'src/tools/wasm2js.cpp')
-rw-r--r--src/tools/wasm2js.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp
index 00ea23e7d..71e7a28fd 100644
--- a/src/tools/wasm2js.cpp
+++ b/src/tools/wasm2js.cpp
@@ -128,6 +128,16 @@ static void traversePost(Ref node, std::function<void(Ref)> visit) {
traversePrePost(node, [](Ref node) {}, visit);
}
+#if 0
+static void replaceInPlace(Ref target, Ref value) {
+ assert(target->isArray() && value->isArray());
+ target->resize(value->size());
+ for (size_t i = 0; i < value->size(); i++) {
+ target[i] = value[i];
+ }
+}
+#endif
+
static void optimizeJS(Ref ast) {
// Helpers
@@ -277,6 +287,26 @@ static void optimizeJS(Ref ast) {
}
}
}
+ // Pre-compute constant [op] constant, which the lowering can generate
+ // in loads etc.
+ if (node[2]->isNumber() && node[3]->isNumber()) {
+ int32_t left = node[2]->getNumber();
+ int32_t right = node[3]->getNumber();
+ if (node[1] == OR) {
+ node->setNumber(left | right);
+ } else if (node[1] == AND) {
+ node->setNumber(left & right);
+ } else if (node[1] == XOR) {
+ node->setNumber(left ^ right);
+ } else if (node[1] == LSHIFT) {
+ node->setNumber(left << (right & 31));
+ } else if (node[1] == RSHIFT) {
+ node->setNumber(int32_t(left) >> int32_t(right & 31));
+ } else if (node[1] == TRSHIFT) {
+ node->setNumber(uint32_t(left) >> uint32_t(right & 31));
+ }
+ return;
+ }
}
// +(+x) => +x
else if (isPlus(node)) {