diff options
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r-- | tools/js-optimizer.js | 74 |
1 files changed, 71 insertions, 3 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index d22de39c..bf971951 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -78,6 +78,7 @@ function traverse(node, pre, post, stack) { if (stack) len = stack.length; var result = pre(node, type, stack); if (result == true) return true; + if (typeof result == 'object') node = result; // Continue processing on this node if (stack && len == stack.length) stack.push(0); } for (var i = 0; i < node.length; i++) { @@ -259,8 +260,74 @@ function removeUnneededLabelSettings(ast) { }); } -// Various expression simplifications -function simplifyExpressions(ast) { +// Various expression simplifications. Pre run before closure (where we still have metadata), Post run after. + +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, []); + } + } + + // The most common mathop is addition, e.g. in getelementptr done repeatedly. We can join all of those, + // by doing (num+num) ==> newnum, and (name+num)+num = name+newnum + function joinAdditions(ast) { + var rerun = true; + while (rerun) { + rerun = false; + traverseGenerated(ast, function(node, type) { + if (type == 'binary' && node[1] == '+') { + if (node[2][0] == 'num' && node[3][0] == 'num') { + rerun = true; + return ['num', node[2][1] + node[3][1]]; + } + for (var i = 2; i <= 3; i++) { + var ii = 5-i; + for (var j = 2; j <= 3; j++) { + if (node[i][0] == 'num' && node[ii][0] == 'binary' && node[ii][1] == '+' && node[ii][j][0] == 'num') { + rerun = true; + node[ii][j][1] += node[i][1]; + return node[ii]; + } + } + } + } + }); + } + } + + simplifyBitops(ast); + joinAdditions(ast); +} + +function simplifyExpressionsPost(ast) { // We often have branchings that are simplified so one end vanishes, and // we then get // if (!(x < 5)) @@ -373,7 +440,8 @@ var passes = { unGlobalize: unGlobalize, removeAssignsToUndefined: removeAssignsToUndefined, //removeUnneededLabelSettings: removeUnneededLabelSettings, - simplifyExpressions: simplifyExpressions, + simplifyExpressionsPre: simplifyExpressionsPre, + simplifyExpressionsPost: simplifyExpressionsPost, loopOptimizer: loopOptimizer }; |