aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/js-optimizer.js7
-rw-r--r--tools/test-js-optimizer-output.js13
-rw-r--r--tools/test-js-optimizer.js13
3 files changed, 30 insertions, 3 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index 99d4581a..ce23b068 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -266,9 +266,12 @@ function simplifyExpressionsPre(ast) {
// When there is a bunch of math like (((8+5)|0)+12)|0, only the external |0 is needed, one correction is enough.
// At each node, ((X|0)+Y)|0 can be transformed into (X+Y): The inner corrections are not needed
// TODO: Is the same is true for 0xff, 0xffff?
+ // Likewise, if we have |0 inside a block that will be >>'d, then the |0 is unnecessary because some
+ // 'useful' mathops already |0 anyhow.
function simplifyBitops(ast) {
- var SAFE_BINARY_OPS = set('+', '-', '*', '/', '%', '|');
+ var USEFUL_BINARY_OPS = set('<<', '>>', '|', '&', '^');
+ var SAFE_BINARY_OPS = set('+', '-', '*', '/', '%');
var ZERO = ['num', 0];
var rerun = true;
while (rerun) {
@@ -287,6 +290,8 @@ function simplifyExpressionsPre(ast) {
break; // Too bad, we can't
}
}
+ } else if (type == 'binary' && node[1] in USEFUL_BINARY_OPS) {
+ stack.push(1);
} else if ((type == 'binary' && node[1] in SAFE_BINARY_OPS) || type == 'num' || type == 'name') {
stack.push(0); // This node is safe in that it does not interfere with this optimization
} else {
diff --git a/tools/test-js-optimizer-output.js b/tools/test-js-optimizer-output.js
index 49ea879d..82b0254d 100644
--- a/tools/test-js-optimizer-output.js
+++ b/tools/test-js-optimizer-output.js
@@ -79,7 +79,18 @@ function ignoreLoopy() {
}
}
function bits() {
- print((($s & 65535) + (($f & 65535) << 16 >> 16) * (($f & 65535) << 16 >> 16) % 256 | 0) & 65535);
+ print(($s & 65535) + (($f & 65535) << 16 >> 16) * (($f & 65535) << 16 >> 16) % 256 & 65535);
+ z(HEAP32[$id + 40 >> 2]);
+ z($f << 2);
+ z($f | 255);
+ z($f & 255);
+ z($f ^ 1);
+ z($f << 2);
+ z($f * 100 << 2);
+ z($f % 2 | 255);
+ z($f / 55 & 255);
+ z($f - 22 ^ 1);
+ z($f + 15 << 2);
}
function maths() {
check(17);
diff --git a/tools/test-js-optimizer.js b/tools/test-js-optimizer.js
index 31f314e3..e2bcb903 100644
--- a/tools/test-js-optimizer.js
+++ b/tools/test-js-optimizer.js
@@ -80,8 +80,19 @@ function ignoreLoopy() {
var $inc=$ok+1;
}
}
-function bits() { // TODO: optimize this!
+function bits() {
print((($s & 65535) + ((($f & 65535) << 16 >> 16) * (($f & 65535) << 16 >> 16) | 0 | 0) % 256 | 0) & 65535);
+ z(HEAP32[($id + 40 | 0) >> 2]);
+ z(($f | 0) << 2);
+ z(($f | 0) | 255);
+ z(($f | 0) & 255);
+ z(($f | 0) ^ 1);
+ z(($f | 0) << 2);
+ z((($f | 0) * 100) << 2);
+ z((($f | 0) % 2) | 255);
+ z((($f | 0) / 55) & 255);
+ z((($f | 0) - 22) ^ 1);
+ z((($f | 0) + 15) << 2);
}
function maths() {
check(5+12);