diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-10-27 17:44:52 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-10-27 17:44:52 -0700 |
commit | 9d245102247d798ffef7e003ae4c76c62a875902 (patch) | |
tree | 93198b3b50fdf6c2078a3eca172f03b0721f5c8a | |
parent | bdeaabb263dee309d5b64f4765183f056a064d19 (diff) |
flip associative binaries when safe to do so for elimination purposes
-rw-r--r-- | tools/eliminator/eliminator-test-output.js | 3 | ||||
-rw-r--r-- | tools/eliminator/eliminator-test.js | 5 | ||||
-rw-r--r-- | tools/js-optimizer.js | 15 |
3 files changed, 23 insertions, 0 deletions
diff --git a/tools/eliminator/eliminator-test-output.js b/tools/eliminator/eliminator-test-output.js index c32266df..bff9071b 100644 --- a/tools/eliminator/eliminator-test-output.js +++ b/tools/eliminator/eliminator-test-output.js @@ -89,6 +89,9 @@ function b() { } var $156; HEAP32[$139 + ($136 << 4) + 4 >> 2] = _sqlite3FindFunction($145, $147, $148, $156, $135, 0); + farr(); + f3(f1() + f2()); + farr(); return cheez(); } function c() { diff --git a/tools/eliminator/eliminator-test.js b/tools/eliminator/eliminator-test.js index 1c6af7f3..d901bc78 100644 --- a/tools/eliminator/eliminator-test.js +++ b/tools/eliminator/eliminator-test.js @@ -109,6 +109,11 @@ function b() { } var $156; HEAP32[$139 + ($136 << 4) + 4 >> 2] = _sqlite3FindFunction($145, $147, $148, $156, $135, 0); + farr(); + var $a = f1(); + var $b = f2() + $a; // this could be reordered to facilitate optimization + f3($b); + farr(); var finality = cheez(); return finality; } diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 855cf6a5..222bd0f4 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -132,6 +132,8 @@ var LOOP = set('do', 'while', 'for'); var LOOP_FLOW = set('break', 'continue'); var ASSIGN_OR_ALTER = set('assign', 'unary-postfix', 'unary-prefix'); var CONTROL_FLOW = set('do', 'while', 'for', 'if', 'switch'); +var NAME_OR_NUM = set('name', 'num'); +var ASSOCIATIVE_BINARIES = set('+', '*', '|', '&', '^'); var NULL_NODE = ['name', 'null']; var UNDEFINED_NODE = ['unary-prefix', 'void', ['num', 0]]; @@ -1635,8 +1637,21 @@ function eliminate(ast) { } } } else if (type == 'binary') { + var flipped = false; + if (node[1] in ASSOCIATIVE_BINARIES && !(node[2][0] in NAME_OR_NUM) && node[3][0] in NAME_OR_NUM) { // TODO recurse here? + // associatives like + and * can be reordered in the simple case of one of the sides being a name, since we assume they are all just numbers + var temp = node[2]; + node[2] = node[3]; + node[3] = temp; + flipped = true; + } traverseInOrder(node[2]); traverseInOrder(node[3]); + if (flipped && node[2][0] in NAME_OR_NUM) { // dunno if we optimized, but safe to flip back - and keeps the code closer to the original and more readable + var temp = node[2]; + node[2] = node[3]; + node[3] = temp; + } } else if (type == 'name') { if (!ignoreName) { // ignoreName means we are the name of something like a call or a sub - irrelevant for us var name = node[1]; |