Skip to content

Commit 58132ad

Browse files
committed
improve side effect detection in js optimizer
1 parent 22dc3bb commit 58132ad

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

Diff for: tools/js-optimizer.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -783,14 +783,13 @@ function simplifyExpressionsPost(ast) {
783783
simplifyNotComps(ast);
784784
}
785785

786-
function hasSideEffects(node) { // this is 99% incomplete and wrong! It just works on __label__ == X and number literals
787-
if (node[0] == 'num') return false;
788-
if (node[0] == 'binary' && (node[1] == '==' || node[1] == '!=') && node[2][0] == 'name' &&
789-
node[3][0] == 'num') {
790-
return false;
791-
} else {
792-
return true;
793-
}
786+
var NO_SIDE_EFFECTS = set('num', 'name');
787+
788+
function hasSideEffects(node) { // this is 99% incomplete!
789+
if (node[0] in NO_SIDE_EFFECTS) return false;
790+
if (node[0] == 'unary-prefix' && node[1] == '!') return hasSideEffects(node[2]);
791+
if (node[0] == 'binary') return hasSideEffects(node[2]) || hasSideEffects(node[3]);
792+
return true;
794793
}
795794

796795
// Clear out empty ifs and blocks, and redundant blocks/stats and so forth

Diff for: tools/test-js-optimizer-output.js

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ function hoisting() {
173173
if (__label__ == 38) {
174174
var $79 = $_pr6;
175175
}
176+
pause(9);
177+
var $cmp70 = ($call69 | 0) != 0;
176178
}
177179
function innerShouldAlsoBeHoisted() {
178180
function hoisting() {

Diff for: tools/test-js-optimizer.js

+10
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ function hoisting() {
216216
var $79 = $_pr6;
217217
}
218218
} while (0);
219+
pause(9);
220+
var $cmp70 = ($call69 | 0) != 0;
221+
if ($cmp70) {
222+
__label__ = 40;
223+
} else {
224+
__label__ = 41;
225+
}
226+
$if_then72$$if_end73$126 : do {
227+
if (__label__ == 40) {} else if (__label__ == 41) {}
228+
} while (0);
219229
}
220230
function innerShouldAlsoBeHoisted() {
221231
function hoisting() {

0 commit comments

Comments
 (0)