diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-06-08 09:47:48 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-06-08 09:55:49 -0700 |
commit | 9ea131b762d124ff8e40898de211025f42589164 (patch) | |
tree | 17e61cf998a80d41e7be7509b03665d9d5488145 | |
parent | 430cfefc1612c00c481e5343b3be4ddc514fd415 (diff) |
remove unneeded outside |0 when a sequence ends in a safe bitop
-rw-r--r-- | tools/js-optimizer.js | 14 | ||||
-rw-r--r-- | tools/test-js-optimizer-asm-pre-output.js | 30 | ||||
-rw-r--r-- | tools/test-js-optimizer-asm-pre.js | 32 |
3 files changed, 74 insertions, 2 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index c598fb81..57a3b1b3 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -402,6 +402,8 @@ function removeUnneededLabelSettings(ast) { // Various expression simplifications. Pre run before closure (where we still have metadata), Post run after. +var USEFUL_BINARY_OPS = set('<<', '>>', '|', '&', '^'); + function simplifyExpressionsPre(ast) { // Look for (x&A)<<B>>B and replace it with X&A if possible. function simplifySignExtends(ast) { @@ -427,7 +429,6 @@ function simplifyExpressionsPre(ast) { // 'useful' mathops already |0 anyhow. function simplifyBitops(ast) { - var USEFUL_BINARY_OPS = set('<<', '>>', '|', '&', '^'); var SAFE_BINARY_OPS = set('+', '-', '*'); // division is unsafe as it creates non-ints in JS; mod is unsafe as signs matter so we can't remove |0's var ZERO = ['num', 0]; var rerun = true; @@ -538,6 +539,17 @@ function simplifyExpressionsPre(ast) { } } } + } else if (type == 'binary' && node[1] == '|') { + // canonicalize order of |0 to end + if (node[2][0] == 'num' && node[2][1] == 0) { + var temp = node[2]; + node[2] = node[3]; + node[3] = temp; + } + // if a seq ends in an |0, remove an external |0 + if (node[2][0] == 'seq' && node[2][2][0] == 'binary' && node[2][2][1] in USEFUL_BINARY_OPS) { + return node[2]; + } } }); diff --git a/tools/test-js-optimizer-asm-pre-output.js b/tools/test-js-optimizer-asm-pre-output.js index 3257576e..a41ba2f8 100644 --- a/tools/test-js-optimizer-asm-pre-output.js +++ b/tools/test-js-optimizer-asm-pre-output.js @@ -8,6 +8,7 @@ function a() { HEAP[1024] = 5; whee(12, 13); whee(12, 13); + f((g = t(), g + g | 0)); } function b($this, $__n) { $this = $this | 0; @@ -131,4 +132,33 @@ function tempDoublePtr($45, $14, $28, $42) { func(); HEAP32[4] = unelim2; } +function boxx($this, $aabb, $xf, $childIndex) { + $this = $this | 0; + $aabb = $aabb | 0; + $xf = $xf | 0; + $childIndex = $childIndex | 0; + var $2 = +0, $4 = +0, $7 = +0, $9 = +0, $13 = +0, $14 = +0, $19 = +0, $20 = +0, $22 = +0, $25 = +0, $28 = +0, $32 = +0, $42 = +0, $45 = 0, $_sroa_06_0_insert_insert$1 = +0, $51 = 0, $_sroa_0_0_insert_insert$1 = +0; + $2 = +HEAPF32[$xf + 12 >> 2]; + $4 = +HEAPF32[$this + 12 >> 2]; + $7 = +HEAPF32[$xf + 8 >> 2]; + $9 = +HEAPF32[$this + 16 >> 2]; + $13 = +HEAPF32[$xf >> 2]; + $14 = $13 + ($2 * $4 - $7 * $9); + $19 = +HEAPF32[$xf + 4 >> 2]; + $20 = $4 * $7 + $2 * $9 + $19; + $22 = +HEAPF32[$this + 20 >> 2]; + $25 = +HEAPF32[$this + 24 >> 2]; + $28 = $13 + ($2 * $22 - $7 * $25); + $32 = $19 + ($7 * $22 + $2 * $25); + $42 = +HEAPF32[$this + 8 >> 2]; + $45 = $aabb; + $_sroa_06_0_insert_insert$1 = +(($20 < $32 ? $20 : $32) - $42); + HEAPF32[$45 >> 2] = ($14 < $28 ? $14 : $28) - $42; + HEAPF32[$45 + 4 >> 2] = $_sroa_06_0_insert_insert$1; + $51 = $aabb + 8 | 0; + $_sroa_0_0_insert_insert$1 = +($42 + ($20 > $32 ? $20 : $32)); + HEAPF32[$51 >> 2] = $42 + ($14 > $28 ? $14 : $28); + HEAPF32[$51 + 4 >> 2] = $_sroa_0_0_insert_insert$1; + return; +} diff --git a/tools/test-js-optimizer-asm-pre.js b/tools/test-js-optimizer-asm-pre.js index efa73218..38487d2f 100644 --- a/tools/test-js-optimizer-asm-pre.js +++ b/tools/test-js-optimizer-asm-pre.js @@ -8,6 +8,7 @@ function a() { HEAP[(4096 & 8191) >> 2] = 5; whee(12, 13) | 0; +whee(12, 13); + f((g = t(), (g+g)|0)|0); } function b($this, $__n) { $this = $this | 0; @@ -135,4 +136,33 @@ function tempDoublePtr($45, $14, $28, $42) { func(); HEAPF32[4] = unelim2; } -// EMSCRIPTEN_GENERATED_FUNCTIONS: ["a", "b", "rett", "ret2t", "retf", "i32_8", "tempDoublePtr"] +function boxx($this, $aabb, $xf, $childIndex) { + $this = $this | 0; + $aabb = $aabb | 0; + $xf = $xf | 0; + $childIndex = $childIndex | 0; + var $2 = +0, $4 = +0, $7 = +0, $9 = +0, $13 = +0, $14 = +0, $19 = +0, $20 = +0, $22 = +0, $25 = +0, $28 = +0, $32 = +0, $42 = +0, $45 = 0, $_sroa_06_0_insert_insert$1 = 0, $51 = 0, $_sroa_0_0_insert_insert$1 = 0; + $2 = +HEAPF32[$xf + 12 >> 2]; + $4 = +HEAPF32[$this + 12 >> 2]; + $7 = +HEAPF32[$xf + 8 >> 2]; + $9 = +HEAPF32[$this + 16 >> 2]; + $13 = +HEAPF32[$xf >> 2]; + $14 = $13 + ($2 * $4 - $7 * $9); + $19 = +HEAPF32[$xf + 4 >> 2]; + $20 = $4 * $7 + $2 * $9 + $19; + $22 = +HEAPF32[$this + 20 >> 2]; + $25 = +HEAPF32[$this + 24 >> 2]; + $28 = $13 + ($2 * $22 - $7 * $25); + $32 = $19 + ($7 * $22 + $2 * $25); + $42 = +HEAPF32[$this + 8 >> 2]; + $45 = $aabb; + $_sroa_06_0_insert_insert$1 = (HEAPF32[tempDoublePtr >> 2] = ($20 < $32 ? $20 : $32) - $42, HEAP32[tempDoublePtr >> 2] | 0) | 0; + HEAPF32[$45 >> 2] = ($14 < $28 ? $14 : $28) - $42; + HEAP32[$45 + 4 >> 2] = $_sroa_06_0_insert_insert$1; + $51 = $aabb + 8 | 0; + $_sroa_0_0_insert_insert$1 = (HEAPF32[tempDoublePtr >> 2] = $42 + ($20 > $32 ? $20 : $32), HEAP32[tempDoublePtr >> 2] | 0) | 0; + HEAPF32[$51 >> 2] = $42 + ($14 > $28 ? $14 : $28); + HEAP32[$51 + 4 >> 2] = $_sroa_0_0_insert_insert$1; + return; +} +// EMSCRIPTEN_GENERATED_FUNCTIONS: ["a", "b", "rett", "ret2t", "retf", "i32_8", "tempDoublePtr", "boxx"] |