diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-03-17 12:54:55 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-03-17 17:56:17 -0700 |
commit | db3ab955c6167dec0e3d694b13b381f6bf7fb330 (patch) | |
tree | 770462d2525f58dfaad796e485cfa43b6f3782c8 /tools | |
parent | dc797a4570fcb776020c886581f6de7feaf734ea (diff) |
fuse if-elses where the relooper did not emit a label clearing
Diffstat (limited to 'tools')
-rw-r--r-- | tools/js-optimizer.js | 14 | ||||
-rw-r--r-- | tools/test-js-optimizer-si-output.js | 13 | ||||
-rw-r--r-- | tools/test-js-optimizer-si.js | 14 |
3 files changed, 34 insertions, 7 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 1c3bc8c0..d5b30476 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -914,7 +914,9 @@ function simplifyIfs(ast) { }); if (abort) return; + var inLoop = 0; // when in a loop, we do not emit label = 0; in the relooper as there is no need traverse(func, function(node, type) { + if (type === 'while') inLoop++; var stats = getStatements(node); if (stats) { for (var i = 0; i < stats.length-1; i++) { @@ -937,12 +939,14 @@ function simplifyIfs(ast) { // Conditions match, just need to make sure the post clears label if (post[2][0] === 'block' && post[2][1] && post[2][1].length > 0) { var postStat = post[2][1][0]; - if (postStat[0] === 'stat' && postStat[1][0] === 'assign' && - postStat[1][1] === true && postStat[1][2][0] === 'name' && postStat[1][2][1] === 'label' && - postStat[1][3][0] === 'num' && postStat[1][3][1] === 0) { + var haveClear = + postStat[0] === 'stat' && postStat[1][0] === 'assign' && + postStat[1][1] === true && postStat[1][2][0] === 'name' && postStat[1][2][1] === 'label' && + postStat[1][3][0] === 'num' && postStat[1][3][1] === 0; + if (!inLoop || haveClear) { // Everything lines up, do it pre[3] = post[2]; - pre[3][1].splice(0, 1); // remove the label clearing + if (haveClear) pre[3][1].splice(0, 1); // remove the label clearing stats.splice(i+1, 1); // remove the post entirely } } @@ -952,6 +956,8 @@ function simplifyIfs(ast) { } } } + }, function(node, type) { + if (type === 'while') inLoop--; }); } }); diff --git a/tools/test-js-optimizer-si-output.js b/tools/test-js-optimizer-si-output.js index 0a2468b5..9ef5171c 100644 --- a/tools/test-js-optimizer-si-output.js +++ b/tools/test-js-optimizer-si-output.js @@ -121,11 +121,18 @@ function a() { if (x ? y : 0) { f(); } else { - label = 53; - } - if ((label | 0) == 53) { a(); } + while (1) { + if (x ? y : 0) { + f(); + } else { + label = 953; + } + if ((label | 0) == 953) { + a(); + } + } if (x ? y : 0) { label = 54; } else { diff --git a/tools/test-js-optimizer-si.js b/tools/test-js-optimizer-si.js index 9bb82da4..04ceec4a 100644 --- a/tools/test-js-optimizer-si.js +++ b/tools/test-js-optimizer-si.js @@ -164,6 +164,20 @@ function a() { if ((label|0) == 53) { a(); } + while (1) { + if (x) { + if (y) { + f(); + } else { + label = 953; + } + } else { + label = 953; + } + if ((label|0) == 953) { + a(); + } + } if (x) { if (y) { label = 54; // extra label setting, cannot fuse here |