aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-12-29 15:27:37 -0800
committerAlon Zakai <alonzakai@gmail.com>2011-12-29 15:27:37 -0800
commit4f876565ff4ead7f0527bbcc6b9dce71654d3ed1 (patch)
tree9d030f59ff2d95765bcf54993f10bebf34a6cf15 /tools
parent85436a9cc37153035a069bfe8c2dedc18b262912 (diff)
clean optimizeShifts
Diffstat (limited to 'tools')
-rw-r--r--tools/js-optimizer.js34
-rw-r--r--tools/test-js-optimizer-t2-output.js6
-rw-r--r--tools/test-js-optimizer-t2.js6
3 files changed, 22 insertions, 24 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]) {
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)