diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-12-29 15:27:37 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-12-29 15:27:37 -0800 |
commit | 4f876565ff4ead7f0527bbcc6b9dce71654d3ed1 (patch) | |
tree | 9d030f59ff2d95765bcf54993f10bebf34a6cf15 /tools/js-optimizer.js | |
parent | 85436a9cc37153035a069bfe8c2dedc18b262912 (diff) |
clean optimizeShifts
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r-- | tools/js-optimizer.js | 34 |
1 files changed, 18 insertions, 16 deletions
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]) { |