diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-12-28 15:16:59 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-12-28 15:16:59 -0800 |
commit | f705c72430d292432c81737ed424729e56385ea9 (patch) | |
tree | 4ce71f6f6fd6fa3e13cd6e1394481fc3f336b8cb | |
parent | ab12dc1380d89298f289215087ee94a65e37acca (diff) |
do not turn shifts into slow additions in optimizeShifts
-rw-r--r-- | tools/js-optimizer.js | 25 | ||||
-rw-r--r-- | tools/test-js-optimizer-t2-output.js | 5 | ||||
-rw-r--r-- | tools/test-js-optimizer-t2.js | 1 |
3 files changed, 14 insertions, 17 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 6cd1fbcc..248ec9cd 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -492,7 +492,6 @@ function optimizeShifts(ast) { function needsShift(name) { return vars[name] && vars[name].primaryShift >= 0; } - traverse(fun, function(node, type) { // add shift to assignments if (node[0] == 'assign' && node[1] === true && node[2][0] == 'name' && needsShift(node[2][1])) { node[3] = ['binary', '>>', node[3], ['num', vars[node[2][1]].primaryShift]]; @@ -519,25 +518,21 @@ function optimizeShifts(ast) { while (more) { // collapse shifts and unshifts, completing the optimization more = false; traverse(fun, function(node, type) { - if (node[0] == 'binary' && node[1] in SIMPLE_SHIFTS && node[2][0] == 'binary' && node[2][1] in SIMPLE_SHIFTS) { + if (node[0] == 'binary' && node[1] in SIMPLE_SHIFTS && node[2][0] == 'binary' && node[2][1] in SIMPLE_SHIFTS && + node[3][0] == 'num' && node[2][3][0] == 'num') { // do not turn a << b << c into a << b + c; while logically identical, it is slower more = true; var combinedShift = '>>'; var sign1 = node[1] == '>>' ? 1 : -1; var sign2 = node[2][1] == '>>' ? 1 : -1; var total = 0; - if (node[3][0] == 'num' && node[2][3][0] == 'num') { - total = ['num', sign1*node[3][1] + sign2*node[2][3][1]]; - if (total[1] < 0) { - combinedShift = '<<'; - total[1] *= -1; - } - if (total[1] == 0) { - // XXX this may be wrong, if we removed a |0 that assumed we had this >> which |0's anyhow! - return node[2][2]; - } - } else { - combinedShift = node[1]; - total = ['binary', sign1 == sign2 ? '+' : '-', node[3], node[2][3]]; + total = ['num', sign1*node[3][1] + sign2*node[2][3][1]]; + if (total[1] < 0) { + combinedShift = '<<'; + total[1] *= -1; + } + if (total[1] == 0) { + // XXX this may be wrong, if we removed a |0 that assumed we had this >> which |0's anyhow! + return node[2][2]; } return ['binary', combinedShift, node[2][2], total]; } diff --git a/tools/test-js-optimizer-t2-output.js b/tools/test-js-optimizer-t2-output.js index d16dd6c9..11f1c3c9 100644 --- a/tools/test-js-optimizer-t2-output.js +++ b/tools/test-js-optimizer-t2-output.js @@ -7,8 +7,8 @@ function shifty($id) { q(HEAP32[(unknown1 + unknown2 >> 2) + $id]); var localUnchanged1 = get(1), localUnchanged2 = get(1); q(HEAP32[(localUnchanged1 + localUnchanged2 >> 2) + $id]); - q($id >> _something_ - 2); - q($id << _somethingElse_ + 2); + q($id << 2 >> _something_); + q($id << 2 << _somethingElse_); pause(-1); var $id2; $id2 = get(54) >> 1; @@ -58,5 +58,6 @@ function shifty($id) { } pause(6); q($idx << 3); + q(1 << $idx << 1); } // EMSCRIPTEN_GENERATED_FUNCTIONS: ["shifty"] diff --git a/tools/test-js-optimizer-t2.js b/tools/test-js-optimizer-t2.js index 5005c91e..907907ef 100644 --- a/tools/test-js-optimizer-t2.js +++ b/tools/test-js-optimizer-t2.js @@ -66,5 +66,6 @@ function shifty($id) { } pause(6); q($idx << 1 << 2); + q(1 << $idx << 1); // Do not turn this into the slower 1 << $idx + 1 (which is identical though) } // EMSCRIPTEN_GENERATED_FUNCTIONS: ["shifty"] |