aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/js-optimizer.js42
-rw-r--r--tools/test-js-optimizer-output.js7
-rw-r--r--tools/test-js-optimizer.js5
3 files changed, 49 insertions, 5 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index d22de39c..010739d3 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -260,7 +260,44 @@ function removeUnneededLabelSettings(ast) {
}
// Various expression simplifications
-function simplifyExpressions(ast) {
+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?
+
+ function simplifyBitops(ast) {
+ var SAFE_BINARY_OPS = set('+', '-', '*', '/', '%', '|');
+ var ZERO = ['num', 0];
+ var rerun = true;
+ while (rerun) {
+ rerun = false;
+ traverseGenerated(ast, function(node, type, stack) {
+ if (type == 'binary' && node[1] == '|' && (jsonCompare(node[2], ZERO) || jsonCompare(node[3], ZERO))) {
+ stack.push(1); // From here on up, no need for this kind of correction, it's done at the top
+
+ // We might be able to remove this correction
+ for (var i = stack.length-2; i >= 0; i--) {
+ if (stack[i] == 1) {
+ // Great, we can eliminate
+ rerun = true;
+ return jsonCompare(node[2], ZERO) ? node[3] : node[2];
+ } else if (stack[i] == -1) {
+ break; // Too bad, we can't
+ }
+ }
+ } 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 {
+ stack.push(-1); // This node is dangerous! Give up if you see this before you see '1'
+ }
+ }, null, []);
+ }
+ }
+
+ simplifyBitops(ast);
+}
+
+function simplifyExpressionsPost(ast) {
// We often have branchings that are simplified so one end vanishes, and
// we then get
// if (!(x < 5))
@@ -373,7 +410,8 @@ var passes = {
unGlobalize: unGlobalize,
removeAssignsToUndefined: removeAssignsToUndefined,
//removeUnneededLabelSettings: removeUnneededLabelSettings,
- simplifyExpressions: simplifyExpressions,
+ simplifyExpressionsPre: simplifyExpressionsPre,
+ simplifyExpressionsPost: simplifyExpressionsPost,
loopOptimizer: loopOptimizer
};
diff --git a/tools/test-js-optimizer-output.js b/tools/test-js-optimizer-output.js
index fcd2380d..95426478 100644
--- a/tools/test-js-optimizer-output.js
+++ b/tools/test-js-optimizer-output.js
@@ -79,6 +79,9 @@ function ignoreLoopy() {
}
}
function bits() {
- print((($s & 65535) + ((($f & 65535) << 16 >> 16) * (($f & 65535) << 16 >> 16) | 0 | 0) % 256 | 0) & 65535);
+ print((($s & 65535) + (($f & 65535) << 16 >> 16) * (($f & 65535) << 16 >> 16) % 256 | 0) & 65535);
}
-// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits"]
+function maths() {
+ __ZN6b2Vec2C1Ev($this1 + 20 + 8 + 8 + 8 + 8 + 8 + 8 + 8 | 0);
+}
+// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths"]
diff --git a/tools/test-js-optimizer.js b/tools/test-js-optimizer.js
index e06ebd29..c59454e6 100644
--- a/tools/test-js-optimizer.js
+++ b/tools/test-js-optimizer.js
@@ -83,5 +83,8 @@ function ignoreLoopy() {
function bits() { // TODO: optimize this!
print((($s & 65535) + ((($f & 65535) << 16 >> 16) * (($f & 65535) << 16 >> 16) | 0 | 0) % 256 | 0) & 65535);
}
-// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits"]
+function maths() {
+ __ZN6b2Vec2C1Ev(((((((($this1 + 20 | 0 | 0) + 8 | 0) + 8 | 0) + 8 | 0) + 8 | 0) + 8 | 0) + 8 | 0) + 8 | 0);
+}
+// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths"]