aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/js-optimizer.js26
-rw-r--r--tools/test-js-optimizer-output.js12
-rw-r--r--tools/test-js-optimizer.js14
3 files changed, 51 insertions, 1 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index c463970f..85a7b214 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -441,6 +441,32 @@ function simplifyExpressionsPre(ast) {
}
}, null, []);
}
+
+ // &-related optimizations
+ traverseGenerated(ast, function(node, type) {
+ if (type == 'binary' && node[1] == '&' && node[3][0] == 'num') {
+ var input = node[2];
+ var amount = node[3][1];
+ if (input[0] == 'binary' && input[1] == '&' && input[3][0] == 'num') {
+ // Collapse X & 255 & 1
+ node[3][1] = amount & input[3][1];
+ node[2] = input[2];
+ } else if (input[0] == 'sub' && input[1][0] == 'name') {
+ // HEAP8[..] & 255 => HEAPU8[..]
+ var name = input[1][1];
+ if (name.substr(0, 4) == 'HEAP') {
+ var unsigned = name[4] == 'U';
+ var bits = parseInt(name.substr(unsigned ? 5 : 4));
+ if (amount == Math.pow(2, bits)-1) {
+ if (!unsigned) {
+ input[1][1] = 'HEAPU' + bits; // make unsigned
+ }
+ return input;
+ }
+ }
+ }
+ }
+ });
}
// The most common mathop is addition, e.g. in getelementptr done repeatedly. We can join all of those,
diff --git a/tools/test-js-optimizer-output.js b/tools/test-js-optimizer-output.js
index b88db21d..c95a36ff 100644
--- a/tools/test-js-optimizer-output.js
+++ b/tools/test-js-optimizer-output.js
@@ -279,4 +279,16 @@ function notComps() {
function tricky() {
var $conv642 = $conv6374 - (($132 << 16 >> 16 | 0) / 2 & -1) & 65535;
}
+function asmy() {
+ f(HEAPU8[_buf + i6 & 16777215]);
+ f(HEAPU8[_buf + i6 & 16777215]);
+ f(HEAP8[_buf + i6 & 16777215] & 1);
+ f(HEAPU8[_buf + i6 & 16777215] & 1);
+ f(HEAP8[_buf + i6 & 16777215] & 1);
+ f(HEAPU8[_buf + i6 & 16777215] & 1);
+ f((HEAP8[_buf + i6 & 16777215] & 1) + i5 | 0);
+ f((HEAPU8[_buf + i6 & 16777215] & 1) + i5 | 0);
+ f((HEAP8[_buf + i6 & 16777215] & 1) + i5 | 0);
+ f((HEAPU8[_buf + i6 & 16777215] & 1) + i5 | 0);
+}
diff --git a/tools/test-js-optimizer.js b/tools/test-js-optimizer.js
index dc48575c..00d6c260 100644
--- a/tools/test-js-optimizer.js
+++ b/tools/test-js-optimizer.js
@@ -389,4 +389,16 @@ function tricky() {
// The &-1 is a rounding correction, and must not be removed
var $conv642 = ($conv6374 - (($132 << 16 >> 16 | 0) / 2 & -1) | 0) & 65535;
}
-// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps", "tricky"]
+function asmy() {
+ f(HEAP8[_buf + i6 & 16777215] & 255);
+ f(HEAPU8[_buf + i6 & 16777215] & 255);
+ f(HEAP8[_buf + i6 & 16777215] & 255 & 1);
+ f(HEAPU8[_buf + i6 & 16777215] & 255 & 1);
+ f(HEAP8[_buf + i6 & 16777215] & 1 & 255);
+ f(HEAPU8[_buf + i6 & 16777215] & 1 & 255);
+ f((HEAP8[_buf + i6 & 16777215] & 255 & 1) + i5 | 0);
+ f((HEAPU8[_buf + i6 & 16777215] & 255 & 1) + i5 | 0);
+ f((HEAP8[_buf + i6 & 16777215] & 1 & 255) + i5 | 0);
+ f((HEAPU8[_buf + i6 & 16777215] & 1 & 255) + i5 | 0);
+}
+// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps", "tricky", "asmy"]