From 4f876565ff4ead7f0527bbcc6b9dce71654d3ed1 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 29 Dec 2011 15:27:37 -0800 Subject: clean optimizeShifts --- tools/js-optimizer.js | 34 ++++++++++++++++++---------------- tools/test-js-optimizer-t2-output.js | 6 ++---- tools/test-js-optimizer-t2.js | 6 ++---- 3 files changed, 22 insertions(+), 24 deletions(-) (limited to 'tools') diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 738ee7ee..9eaf1916 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -354,7 +354,7 @@ function simplifyExpressionsPre(ast) { // HEAP[x >> 2] // very often. We can in some cases do the shift on the variable itself when it is set, // to greatly reduce the number of shift operations. -// XXX x << 24 >> 24 - are we breaking that? only optimize small shifts! +// TODO: when shifting a variable, if there are other uses, keep an unshifted version too, to prevent slowdowns? function optimizeShifts(ast) { traverseGeneratedFunctions(ast, function(fun) { var funMore = true; @@ -403,23 +403,25 @@ function optimizeShifts(ast) { traverse(fun, function(node, type) { if (type == 'binary' && node[1] == '>>' && node[3][0] == 'num') { var shifts = node[3][1]; - // Push the >> inside the value elements, doing some simplification while we are there - function addShift(subNode) { - if (subNode[0] == 'binary' && subNode[1] == '+') { - subNode[2] = addShift(subNode[2]); - subNode[3] = addShift(subNode[3]); - return subNode; - } - if (subNode[0] == 'name' && !subNode[2]) { // names are returned with a shift, but we also note their being shifted - var name = subNode[1]; - if (vars[name]) { - vars[name].timesShifted[shifts]++; - subNode[2] = true; + if (shifts >= 0 && shifts <= 3) { + // Push the >> inside the value elements + function addShift(subNode) { + if (subNode[0] == 'binary' && subNode[1] == '+') { + subNode[2] = addShift(subNode[2]); + subNode[3] = addShift(subNode[3]); + return subNode; + } + if (subNode[0] == 'name' && !subNode[2]) { // names are returned with a shift, but we also note their being shifted + var name = subNode[1]; + if (vars[name]) { + vars[name].timesShifted[shifts]++; + subNode[2] = true; + } } + return ['binary', '>>', subNode, ['num', shifts]]; } - return ['binary', '>>', subNode, ['num', shifts]]; + return addShift(node[2]); } - return addShift(node[2]); } }); traverse(fun, function(node, type) { @@ -454,7 +456,7 @@ function optimizeShifts(ast) { } } } - //printErr(dump(vars)); + //printErr(JSON.stringify(vars)); function cleanNotes() { // We need to mark 'name' nodes as 'processed' in some passes here; this cleans the notes up traverse(fun, function(node, type) { if (node[0] == 'name' && node[2]) { diff --git a/tools/test-js-optimizer-t2-output.js b/tools/test-js-optimizer-t2-output.js index dd2069fd..04f1d0f4 100644 --- a/tools/test-js-optimizer-t2-output.js +++ b/tools/test-js-optimizer-t2-output.js @@ -52,10 +52,8 @@ function shifty($id) { q(HEAP32[$parameters_addr + 1416 + $p]); pause(5); var $res_spec242 = get($real), $cp = get("b"), $tileno = arguments[2]; - while (get(1)) { - q(HEAP32[$parameters_addr + 1406 + ($res_spec242 - 1)]); - q(HEAP32[(HEAP32[($cp >> 2) + 27] >> 2) + $tileno * 1397 + 105]); - } + q(HEAP32[$parameters_addr + 1406 + ($res_spec242 - 1)]); + q(HEAP32[(HEAP32[($cp >> 2) + 27] >> 2) + $tileno * 1397 + 105]); pause(6); q($idx << 3); q(1 << $idx << 1); diff --git a/tools/test-js-optimizer-t2.js b/tools/test-js-optimizer-t2.js index 55417ec0..ffda2703 100644 --- a/tools/test-js-optimizer-t2.js +++ b/tools/test-js-optimizer-t2.js @@ -60,10 +60,8 @@ function shifty($id) { pause(5); // loops count as more uses! var $res_spec242 = get($real), $cp = get('b'), $tileno = arguments[2]; - while (get(1)) { - q(HEAP32[($parameters_addr + 5624 + (($res_spec242 - 1 | 0) << 2) | 0) >> 2]); - q(HEAP32[(HEAP32[($cp + 108 | 0) >> 2] + $tileno * 5588 + 420 | 0) >> 2]); - } + q(HEAP32[($parameters_addr + 5624 + (($res_spec242 - 1 | 0) << 2) | 0) >> 2]); + q(HEAP32[(HEAP32[($cp + 108 | 0) >> 2] + $tileno * 5588 + 420 | 0) >> 2]); pause(6); q($idx << 1 << 2); q(1 << $idx << 1); // Do not turn this into the slower 1 << $idx + 1 (which is identical though) -- cgit v1.2.3-18-g5258