diff options
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, |