aboutsummaryrefslogtreecommitdiff
path: root/tools/js-optimizer.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-05-23 15:28:08 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-05-23 15:28:08 -0700
commitb90b3ea970f3cc65020730f33ff97c4f2bc2a860 (patch)
tree87ef7d60b16d78d9796450c2607e32e141f60e4d /tools/js-optimizer.js
parentf6ab05e76cbb946bd35b35eaccfb7ae4567f6501 (diff)
parenta2b9c72b677a23aa665fca5ab9239a725040ca44 (diff)
Merge pull request #1204 from sunfishcode/incoming
Optimize (x&A)<<B>>B.
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r--tools/js-optimizer.js18
1 files changed, 18 insertions, 0 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index 32ed1cce..5e7a2448 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -403,6 +403,23 @@ function removeUnneededLabelSettings(ast) {
// Various expression simplifications. Pre run before closure (where we still have metadata), Post run after.
function simplifyExpressionsPre(ast) {
+ // Look for (x&A)<<B>>B and replace it with X&A if possible.
+ function simplifySignExtends(ast) {
+ traverseGenerated(ast, function(node, type) {
+ if (type == 'binary' && node[1] == '>>' && node[3][0] == 'num' &&
+ node[2][0] == 'binary' && node[2][1] == '<<' && node[2][3][0] == 'num' && node[3][1] == node[2][3][1]) {
+ var innerNode = node[2][2];
+ var shifts = node[3][1];
+ if (innerNode[0] == 'binary' && innerNode[1] == '&' && innerNode[3][0] == 'num') {
+ var mask = innerNode[3][1];
+ if (mask << shifts >> shifts == mask) {
+ return innerNode;
+ }
+ }
+ }
+ });
+ }
+
// When there is a bunch of math like (((8+5)|0)+12)|0, only the external |0 is needed, one correction is enough.
// At each node, ((X|0)+Y)|0 can be transformed into (X+Y): The inner corrections are not needed
// TODO: Is the same is true for 0xff, 0xffff?
@@ -592,6 +609,7 @@ function simplifyExpressionsPre(ast) {
});
}
+ simplifySignExtends(ast);
simplifyBitops(ast);
joinAdditions(ast);
// simplifyZeroComp(ast); TODO: investigate performance