diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-06-06 21:12:20 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-06-07 10:09:12 -0700 |
commit | 5f2ccad0a399276de4e8426fcd1449e256499815 (patch) | |
tree | ae98d4a32912a48f91b4f19885a98b5b8a980ebe | |
parent | 57ea5f4e7110310733e81194082bda168a463ae4 (diff) |
track number of uses properly for loop variable removal
-rw-r--r-- | tools/eliminator/asm-eliminator-test-output.js | 17 | ||||
-rw-r--r-- | tools/eliminator/asm-eliminator-test.js | 19 | ||||
-rw-r--r-- | tools/js-optimizer.js | 9 |
3 files changed, 43 insertions, 2 deletions
diff --git a/tools/eliminator/asm-eliminator-test-output.js b/tools/eliminator/asm-eliminator-test-output.js index 8cd11541..9b874ba6 100644 --- a/tools/eliminator/asm-eliminator-test-output.js +++ b/tools/eliminator/asm-eliminator-test-output.js @@ -5031,4 +5031,21 @@ function looop6() { } moar(i); } +function looop7() { + var $old_0_i107_i = 0, $current_0_i108_i = 0, $696 = 0; + $old_0_i107_i = $draw_left_i; + while (1) { + $current_0_i108_i = HEAP32[$old_0_i107_i >> 2] | 0; + if (($current_0_i108_i | 0) == 0) { + break; + } + $696 = $current_0_i108_i + 4 | 0; + if (($current_0_i108_i | 0) == ($P_3207_i | 0)) { + break; + } else { + $old_0_i107_i = $696; + } + } + HEAP32[$old_0_i107_i >> 2] = HEAP32[$696 >> 2] | 0; +} diff --git a/tools/eliminator/asm-eliminator-test.js b/tools/eliminator/asm-eliminator-test.js index 6f881654..9524bde2 100644 --- a/tools/eliminator/asm-eliminator-test.js +++ b/tools/eliminator/asm-eliminator-test.js @@ -6755,5 +6755,22 @@ function looop6() { } moar(helper); // this is cool } -// 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"] +function looop7() { + var $old_0_i107_i = 0, $current_0_i108_i = 0, $696 = 0; + $old_0_i107_i = $draw_left_i; + while (1) { + $current_0_i108_i = HEAP32[$old_0_i107_i >> 2] | 0; + if (($current_0_i108_i | 0) == 0) { + break; + } + $696 = $current_0_i108_i + 4 | 0; + if (($current_0_i108_i | 0) == ($P_3207_i | 0)) { + break; + } else { + $old_0_i107_i = $696; + } + } + HEAP32[$old_0_i107_i >> 2] = HEAP32[$696 >> 2] | 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"] diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index a3a58f2b..67ce2b3d 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -1854,6 +1854,7 @@ function eliminate(ast, memSafe) { // First, find the potentially eliminatable functions: that have one definition and one use var definitions = {}; var uses = {}; + var namings = {}; var values = {}; var locals = {}; var varsToRemove = {}; // variables being removed, that we can eliminate all 'var x;' of (this refers to 'var' nodes we should remove) @@ -1896,6 +1897,8 @@ function eliminate(ast, memSafe) { if (!values[name]) values[name] = node[3]; if (node[1] === true) { // not +=, -= etc., just = uses[name]--; // because the name node will show up by itself in the previous case + if (!namings[name]) namings[name] = 0; + namings[name]++; // offset it here, this tracks the total times we are named } } } else if (type == 'switch') { @@ -1903,6 +1906,10 @@ function eliminate(ast, memSafe) { } }); + for (var used in uses) { + namings[used] = (namings[used] || 0) + uses[used]; + } + // we cannot eliminate variables if there is a switch if (hasSwitch && !asm) return; @@ -2372,7 +2379,7 @@ function eliminate(ast, memSafe) { if (assign[1] === true && assign[2][0] == 'name' && assign[3][0] == 'name') { var looper = assign[2][1]; var helper = assign[3][1]; - if (definitions[helper] == 1 && seenUses[looper] == uses[looper] + 1 && // +1, because uses does not count the definition + if (definitions[helper] == 1 && seenUses[looper] == namings[looper] && !helperReplacements[helper] && !helperReplacements[looper]) { // the remaining issue is whether looper is used after the assignment to helper and before the last line (where we assign to it) var found = -1; |