diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-03-14 20:05:33 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-03-17 17:56:17 -0700 |
commit | 3215f80b20f5cf98390c43a2c5c8b3f4ebcdd984 (patch) | |
tree | 28a3c5a52f7a3c07c1b45587f233ffe3b52a6fa1 /tools/js-optimizer.js | |
parent | 4f0e8c3aed0ce2e4606c5c8facacfbd3c015d375 (diff) |
simplifyIfs js optimizer pass
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r-- | tools/js-optimizer.js | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 81bf8824..7900044a 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -436,7 +436,7 @@ function removeUnneededLabelSettings(ast) { }); } -// Various expression simplifications. Pre run before closure (where we still have metadata), Post run after. +// Various expression simplifications. Happens after elimination, which opens up many of these simplification opportunities. var USEFUL_BINARY_OPS = set('<<', '>>', '|', '&', '^'); var COMPARE_OPS = set('<', '<=', '>', '>=', '==', '===', '!=', '!=='); @@ -819,11 +819,32 @@ function simplifyExpressions(ast) { simplifyIntegerConversions(func); simplifyBitops(func); joinAdditions(func); - // simplifyZeroComp(func); TODO: investigate performance simplifyNotComps(func); + // simplifyZeroComp(func); TODO: investigate performance }); } + +function simplifyIfs(ast) { + traverse(ast, function(node, type) { + // simplify if (x) { if (y) { .. } } to if (x ? y : 0) { .. } + if (type === 'if' && !node[3]) { + var body = node[2]; + // recurse to handle chains + while (body[0] === 'block' && body[1].length === 1) { + var singleton = body[1][0]; + if (singleton[0] === 'if' && !singleton[3]) { + node[1] = ['conditional', node[1], singleton[1], ['num', 0]]; + body = node[2] = singleton[2]; + } else { + break; + } + } + } + }); +} + + // In typed arrays mode 2, we can have // HEAP[x >> 2] // very often. We can in some cases do the shift on the variable itself when it is set, @@ -5262,6 +5283,7 @@ var passes = { removeAssignsToUndefined: removeAssignsToUndefined, //removeUnneededLabelSettings: removeUnneededLabelSettings, simplifyExpressions: simplifyExpressions, + simplifyIfs: simplifyIfs, optimizeShiftsConservative: optimizeShiftsConservative, optimizeShiftsAggressive: optimizeShiftsAggressive, hoistMultiples: hoistMultiples, |