Skip to content

Commit d7c3e10

Browse files
committed
optimize |,& on constants
1 parent 4c4cade commit d7c3e10

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

tools/js-optimizer.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -419,22 +419,26 @@ function simplifyExpressionsPre(ast, asm) {
419419
while (rerun) {
420420
rerun = false;
421421
traverseGenerated(ast, function process(node, type, stack) {
422-
if (type == 'binary' && node[1] == '|' && (jsonCompare(node[2], ZERO) || jsonCompare(node[3], ZERO))) {
423-
// We might be able to remove this correction
424-
for (var i = stack.length-1; i >= 0; i--) {
425-
if (stack[i] == 1) {
426-
// we will replace ourselves with the non-zero side. Recursively process that node.
427-
var result = jsonCompare(node[2], ZERO) ? node[3] : node[2], other;
428-
// Great, we can eliminate
429-
rerun = true;
430-
while (other = process(result, result[0], stack)) {
431-
result = other;
422+
if (type == 'binary' && node[1] == '|') {
423+
if (node[2][0] == 'num' && node[3][0] == 'num') {
424+
return ['num', node[2][1] | node[3][1]];
425+
} else if (jsonCompare(node[2], ZERO) || jsonCompare(node[3], ZERO)) {
426+
// We might be able to remove this correction
427+
for (var i = stack.length-1; i >= 0; i--) {
428+
if (stack[i] == 1) {
429+
// we will replace ourselves with the non-zero side. Recursively process that node.
430+
var result = jsonCompare(node[2], ZERO) ? node[3] : node[2], other;
431+
// Great, we can eliminate
432+
rerun = true;
433+
while (other = process(result, result[0], stack)) {
434+
result = other;
435+
}
436+
return result;
437+
} else if (stack[i] == -1) {
438+
break; // Too bad, we can't
439+
} else if (asm) {
440+
break; // we must keep a coercion right on top of a heap access in asm mode
432441
}
433-
return result;
434-
} else if (stack[i] == -1) {
435-
break; // Too bad, we can't
436-
} else if (asm) {
437-
break; // we must keep a coercion right on top of a heap access in asm mode
438442
}
439443
}
440444
stack.push(1); // From here on up, no need for this kind of correction, it's done at the top
@@ -452,6 +456,7 @@ function simplifyExpressionsPre(ast, asm) {
452456
// &-related optimizations
453457
traverseGenerated(ast, function(node, type) {
454458
if (type == 'binary' && node[1] == '&' && node[3][0] == 'num') {
459+
if (node[2][0] == 'num') return ['num', node[2][1] & node[3][1]];
455460
var input = node[2];
456461
var amount = node[3][1];
457462
if (input[0] == 'binary' && input[1] == '&' && input[3][0] == 'num') {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function a() {
22
f((HEAPU8[10202] | 0) + 5 | 0);
33
f(HEAPU8[10202] | 0);
4+
f(347);
5+
f(351);
6+
f(8);
47
}
58

tools/test-js-optimizer-asm-pre.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function a() {
22
f((HEAPU8[10202] | 0) + 5 | 0);
33
f((HEAPU8[10202] | 0) | 0);
4+
f(347 | 0);
5+
f(347 | 12);
6+
f(347 & 12);
47
}
58
// EMSCRIPTEN_GENERATED_FUNCTIONS: ["a"]

0 commit comments

Comments
 (0)