aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-03-27 10:34:52 -0700
committerAlon Zakai <alonzakai@gmail.com>2014-03-27 10:34:52 -0700
commit422d65dc0603ca7c856e82425adfeee01463d2b0 (patch)
tree9f18a36de2cdbb210af08b00ff281716f9a59e14 /tools
parente01dac87dae70222db7b8c0a8f3301b368f8b67b (diff)
optimize away individual loop vars, even if we can't remove them all
Diffstat (limited to 'tools')
-rw-r--r--tools/eliminator/asm-eliminator-test-output.js38
-rw-r--r--tools/eliminator/asm-eliminator-test.js51
-rw-r--r--tools/js-optimizer.js19
3 files changed, 105 insertions, 3 deletions
diff --git a/tools/eliminator/asm-eliminator-test-output.js b/tools/eliminator/asm-eliminator-test-output.js
index e1bc83bf..a3873067 100644
--- a/tools/eliminator/asm-eliminator-test-output.js
+++ b/tools/eliminator/asm-eliminator-test-output.js
@@ -819,4 +819,42 @@ function selfAssign() {
}
return i1 & 16384 | 0;
}
+function elimOneLoopVar($argc, $argv) {
+ $argc = $argc | 0;
+ $argv = $argv | 0;
+ var $arg$0 = 0, $call10 = Math_fround(0), $curri$012 = 0, $inc = 0, $j$010 = 0, $ok$0 = 0, $primes$011 = 0, $retval$0 = 0, $vararg_buffer1 = 0;
+ $curri$012 = 2;
+ $primes$011 = 0;
+ while (1) {
+ $call10 = Math_fround(Math_sqrt(Math_fround(Math_fround($curri$012 | 0))));
+ L15 : do {
+ if ($call10 > Math_fround(+2)) {
+ $j$010 = 2;
+ while (1) {
+ $inc = $j$010 + 1 | 0;
+ if ((($curri$012 | 0) % ($j$010 | 0) & -1 | 0) == 0) {
+ $ok$0 = 0;
+ break L15;
+ }
+ if (Math_fround($inc | 0) < $call10) {
+ $j$010 = $inc;
+ } else {
+ $ok$0 = 1;
+ break;
+ }
+ }
+ } else {
+ $ok$0 = 1;
+ }
+ } while (0);
+ $primes$011 = $ok$0 + $primes$011 | 0;
+ if (($primes$011 | 0) >= ($arg$0 | 0)) {
+ break;
+ } else {
+ $curri$012 = $curri$012 + 1 | 0;
+ }
+ }
+ HEAP32[$vararg_buffer1 >> 2] = $curri$012;
+ return $retval$0 | 0;
+}
diff --git a/tools/eliminator/asm-eliminator-test.js b/tools/eliminator/asm-eliminator-test.js
index 8886d54a..312e9e2c 100644
--- a/tools/eliminator/asm-eliminator-test.js
+++ b/tools/eliminator/asm-eliminator-test.js
@@ -1037,5 +1037,54 @@ function selfAssign() {
STACKTOP = STACKTOP;
return i1 & 16384 | 0;
}
-// EMSCRIPTEN_GENERATED_FUNCTIONS: ["asm", "__Z11printResultPiS_j", "_segment_holding", "__ZN5identC2EiPKcPci", "_vec2Length", "exc", "label", "confuusion", "tempDouble", "_org_apache_harmony_luni_util_NumberConverter_freeFormat__", "__ZN23b2EdgeAndPolygonContact8EvaluateEP10b2ManifoldRK11b2TransformS4_", "_java_nio_charset_Charset_forNameInternal___java_lang_String", "looop2", "looop3", "looop4", "looop5", "looop6", "looop7", "looop8", "multiloop", "multiloop2", "tempDouble2", "watIf", "select2", "binary", "cute", "selfAssign"]
+function elimOneLoopVar($argc, $argv) {
+ $argc = $argc | 0;
+ $argv = $argv | 0;
+ var $0 = 0, $1 = 0, $arg$0 = 0, $arrayidx = 0, $call10 = Math_fround(0), $cmp = 0, $cmp11 = 0, $cmp119 = 0, $cmp12 = 0, $cmp7 = 0, $conv = 0, $conv8 = Math_fround(0), $conv9 = Math_fround(0), $curri$012 = 0, $inc = 0, $inc14$primes$0 = 0, $inc16 = 0, $j$010 = 0, $j$010$phi = 0, $ok$0 = 0;
+ var $primes$011 = 0, $rem = 0, $retval$0 = 0, $sub = 0, $vararg_buffer1 = 0, label = 0, sp = 0;
+ $curri$012 = 2;
+ $primes$011 = 0;
+ while (1) {
+ $conv9 = Math_fround($curri$012 | 0);
+ $call10 = Math_fround(Math_sqrt(Math_fround($conv9)));
+ $cmp119 = $call10 > Math_fround(+2);
+ L15 : do {
+ if ($cmp119) {
+ $j$010 = 2;
+ while (1) {
+ $rem = ($curri$012 | 0) % ($j$010 | 0) & -1;
+ $cmp12 = ($rem | 0) == 0;
+ $inc = $j$010 + 1 | 0;
+ if ($cmp12) {
+ $ok$0 = 0;
+ break L15;
+ }
+ $conv8 = Math_fround($inc | 0);
+ $cmp11 = $conv8 < $call10;
+ if ($cmp11) {
+ $j$010$phi = $inc;
+ $j$010 = $j$010$phi;
+ } else {
+ $ok$0 = 1;
+ break;
+ }
+ }
+ } else {
+ $ok$0 = 1;
+ }
+ } while (0);
+ $inc14$primes$0 = $ok$0 + $primes$011 | 0;
+ $inc16 = $curri$012 + 1 | 0;
+ $cmp7 = ($inc14$primes$0 | 0) < ($arg$0 | 0);
+ if ($cmp7) {
+ $curri$012 = $inc16;
+ $primes$011 = $inc14$primes$0;
+ } else {
+ break;
+ }
+ }
+ HEAP32[$vararg_buffer1 >> 2] = $curri$012;
+ return $retval$0 | 0;
+}
+// EMSCRIPTEN_GENERATED_FUNCTIONS: ["asm", "__Z11printResultPiS_j", "_segment_holding", "__ZN5identC2EiPKcPci", "_vec2Length", "exc", "label", "confuusion", "tempDouble", "_org_apache_harmony_luni_util_NumberConverter_freeFormat__", "__ZN23b2EdgeAndPolygonContact8EvaluateEP10b2ManifoldRK11b2TransformS4_", "_java_nio_charset_Charset_forNameInternal___java_lang_String", "looop2", "looop3", "looop4", "looop5", "looop6", "looop7", "looop8", "multiloop", "multiloop2", "tempDouble2", "watIf", "select2", "binary", "cute", "selfAssign", "elimOneLoopVar"]
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index 49bc9d11..a4c05b70 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -3964,7 +3964,7 @@ function eliminate(ast, memSafe) {
}
}
}
- if (loopers.length < assigns.length) return; // TODO: handle the case where can can just eliminate one. (we can't optimize the break, but we can remove the var at least)
+ if (loopers.length === 0) return;
for (var l = 0; l < loopers.length; l++) {
var looper = loopers[l];
var helper = helpers[l];
@@ -4016,9 +4016,24 @@ function eliminate(ast, memSafe) {
// simplify the if. we remove the if branch, leaving only the else
if (flip) {
last[1] = simplifyNotCompsDirect(['unary-prefix', '!', last[1]]);
+ var temp = last[2];
last[2] = last[3];
+ last[3] = temp;
+ }
+ if (loopers.length === assigns.length) {
+ last.pop();
+ } else {
+ var elseStats = getStatements(last[3]);
+ for (var i = 0; i < elseStats.length; i++) {
+ var stat = elseStats[i];
+ if (stat[0] === 'stat') stat = stat[1];
+ if (stat[0] === 'assign' && stat[2][0] === 'name') {
+ if (loopers.indexOf(stat[2][1]) >= 0) {
+ elseStats[i] = emptyNode();
+ }
+ }
+ }
}
- last.pop();
}
}
}