diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-06-05 18:37:01 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-06-05 19:01:10 -0700 |
commit | 24807a6d8f657b27e0ae6b73e73fa987a0672480 (patch) | |
tree | 778bdee6fa5d25d1390bd291a777fa7ac79ea93e /tools/js-optimizer.js | |
parent | 0e4a1c01e3f09751c72c57bf9e8bddfc5581bd53 (diff) |
simplify infinite while loops with a break at the end into a do-while with a condition
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r-- | tools/js-optimizer.js | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 09791150..57c9cf79 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -968,6 +968,7 @@ function simplifyNotComps(ast) { } } }); + return ast; } function simplifyExpressionsPost(ast) { @@ -1804,6 +1805,21 @@ function registerize(ast) { } denormalizeAsm(fun, finalAsmData); } + + // Post-registerize optimizations. This is near the end of the pipeline, we can assume all other optimizations except for minification are done + traverse(fun, function(node, type) { + if (type == 'while' && node[1][0] == 'num' && node[1][1] == 1 && node[2][0] == 'block') { + // while (1) { .. if (..) { break } } ==> do { .. } while(..) + var stats = node[2][1]; + var last = stats[stats.length-1]; + if (last[0] == 'if' && !last[3] && last[2][0] == 'block' && last[2][1][0][0] == 'break' && !last[2][1][0][1]) { + var conditionToBreak = last[1]; + stats.pop(); + node[0] = 'do'; + node[1] = simplifyNotComps(['unary-prefix', '!', conditionToBreak]); + } + } + }); }); } |