aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-06-30 12:49:32 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-06-30 12:49:32 -0700
commit547b13cf5bbf81ca8946d19e8e45b9c2a870a235 (patch)
treefdd1eb61940e562c46d0d5453c49995f5f32bce2
parent6e3a916b14d0e101efc2e7880949c81fbfe144c0 (diff)
do not optimize while into do-while if there are continues; fixes #1337
-rw-r--r--tools/js-optimizer.js17
-rw-r--r--tools/test-js-optimizer-asm-last-output.js28
-rw-r--r--tools/test-js-optimizer-asm-last.js34
3 files changed, 79 insertions, 0 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index 3cd1b229..d04807a7 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -2807,6 +2807,23 @@ function asmLoopOptimizer(ast) {
var stats = node[2][1];
var last = stats[stats.length-1];
if (last && last[0] === 'if' && !last[3] && last[2][0] === 'block' && last[2][1][0] && last[2][1][0][0] === 'break' && !last[2][1][0][1]) {
+ var abort = false;
+ var stack = 0;
+ traverse(stats, function(node, type) {
+ if (type == 'continue') {
+ if (stack == 0 || node[1]) { // abort if labeled (we do not analyze labels here yet), or a continue directly on us
+ abort = true;
+ return true;
+ }
+ } else if (type in LOOP) {
+ stack++;
+ }
+ }, function(node, type) {
+ if (type in LOOP) {
+ stack--;
+ }
+ });
+ if (abort) return;
var conditionToBreak = last[1];
stats.pop();
node[0] = 'do';
diff --git a/tools/test-js-optimizer-asm-last-output.js b/tools/test-js-optimizer-asm-last-output.js
index cbc0a4d3..0f95d544 100644
--- a/tools/test-js-optimizer-asm-last-output.js
+++ b/tools/test-js-optimizer-asm-last-output.js
@@ -42,5 +42,33 @@ function looop() {
do {
do_it();
} while (x());
+ while (1) {
+ do_it();
+ if (a()) continue;
+ if (!x()) {
+ break;
+ }
+ }
+ do {
+ do_it();
+ do {
+ if (a()) continue;
+ } while (b());
+ } while (x());
+ do {
+ do_it();
+ while (b()) {
+ if (a()) continue;
+ }
+ } while (x());
+ X : while (1) {
+ do_it();
+ while (b()) {
+ if (a()) continue X;
+ }
+ if (!x()) {
+ break;
+ }
+ }
}
diff --git a/tools/test-js-optimizer-asm-last.js b/tools/test-js-optimizer-asm-last.js
index 6331879e..05e1049e 100644
--- a/tools/test-js-optimizer-asm-last.js
+++ b/tools/test-js-optimizer-asm-last.js
@@ -51,6 +51,40 @@ function looop() {
break;
}
}
+ while (1) {
+ do_it();
+ if (a()) continue; // we cannot move to do-while, continue will hit the while check
+ if (!x()) {
+ break;
+ }
+ }
+ while (1) {
+ do_it();
+ do {
+ if (a()) continue; // ok to optimize, continue is not for us
+ } while (b());
+ if (!x()) {
+ break;
+ }
+ }
+ while (1) {
+ do_it();
+ while (b()) {
+ if (a()) continue; // also ok to optimize, continue is not for us
+ }
+ if (!x()) {
+ break;
+ }
+ }
+ X: while (1) {
+ do_it();
+ while (b()) {
+ if (a()) continue X; // not ok to optimize
+ }
+ if (!x()) {
+ break;
+ }
+ }
}
// EMSCRIPTEN_GENERATED_FUNCTIONS: ["finall", "looop"]