Skip to content

Commit

Permalink
simplify x != 0 to x when the output is a boolean ; 1.35.20
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Jan 11, 2016
1 parent 904f5d7 commit 6ad2079
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion emscripten-version.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"1.35.19"
"1.35.20"

16 changes: 16 additions & 0 deletions tests/optimizer/test-js-optimizer-asm-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,20 @@ function hoisting() {
}
}
}
function toZero(x, y) {
x = x | 0;
y = +y;
if (x) a();
if (y) a();
while (x) a();
while (y) a();
do {
a();
} while (x);
do {
a();
} while (y);
x ? a() : b();
y ? a() : b();
}

6 changes: 3 additions & 3 deletions tests/optimizer/test-js-optimizer-asm-pre-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function _main($argc, $argv) {
$118 = $world + 102952 | 0;
HEAP32[$116 + 96 >> 2] = HEAP32[$118 >> 2];
$121 = HEAP32[$118 >> 2] | 0;
if (($121 | 0) != 0) {
if ($121 | 0) {
HEAP32[$121 + 92 >> 2] = $116;
}
HEAP32[$118 >> 2] = $116;
Expand Down Expand Up @@ -442,7 +442,7 @@ function _main($argc, $argv) {
$y_sroa_0_0_insert_insert$1 = +$y_sroa_1_4_load293550;
HEAPF32[$185 >> 2] = $y_sroa_0_0_load283451;
HEAPF32[$185 + 4 >> 2] = $y_sroa_0_0_insert_insert$1;
if ((HEAP32[$98 >> 2] & 2 | 0) != 0) {
if (HEAP32[$98 >> 2] & 2 | 0) {
label = 65;
break L82;
}
Expand All @@ -457,7 +457,7 @@ function _main($argc, $argv) {
HEAP32[$209 + 92 >> 2] = 0;
HEAP32[$209 + 96 >> 2] = HEAP32[$118 >> 2];
$213 = HEAP32[$118 >> 2] | 0;
if (($213 | 0) != 0) {
if ($213 | 0) {
HEAP32[$213 + 92 >> 2] = $209;
}
HEAP32[$118 >> 2] = $209;
Expand Down
6 changes: 3 additions & 3 deletions tests/optimizer/test-js-optimizer-asm-pre-output2.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function _main($argc, $argv) {
$118 = $world + 102952 | 0;
HEAP32[$116 + 96 >> 2] = HEAP32[$118 >> 2];
$121 = HEAP32[$118 >> 2] | 0;
if (($121 | 0) != 0) {
if ($121 | 0) {
HEAP32[$121 + 92 >> 2] = $116;
}
HEAP32[$118 >> 2] = $116;
Expand Down Expand Up @@ -442,7 +442,7 @@ function _main($argc, $argv) {
$y_sroa_0_0_insert_insert$1 = +$y_sroa_1_4_load293550;
HEAPF32[$185 >> 2] = $y_sroa_0_0_load283451;
HEAPF32[$185 + 4 >> 2] = $y_sroa_0_0_insert_insert$1;
if ((HEAP32[$98 >> 2] & 2 | 0) != 0) {
if (HEAP32[$98 >> 2] & 2 | 0) {
label = 65;
break L82;
}
Expand All @@ -457,7 +457,7 @@ function _main($argc, $argv) {
HEAP32[$209 + 92 >> 2] = 0;
HEAP32[$209 + 96 >> 2] = HEAP32[$118 >> 2];
$213 = HEAP32[$118 >> 2] | 0;
if (($213 | 0) != 0) {
if ($213 | 0) {
HEAP32[$213 + 92 >> 2] = $209;
}
HEAP32[$118 >> 2] = $209;
Expand Down
18 changes: 17 additions & 1 deletion tests/optimizer/test-js-optimizer-asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,20 @@ function hoisting() {
}
}
}
// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps", "tricky", "asmy"]
function toZero(x, y) {
x = x | 0;
y = +y;
if (x != 0) a();
if (y != 0) a();
while (x != 0) a();
while (y != 0) a();
do {
a();
} while (x != 0);
do {
a();
} while (y != 0);
x != 0 ? a() : b();
y != 0 ? a() : b();
}
// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps", "tricky", "asmy", "toZero"]
2 changes: 1 addition & 1 deletion tests/optimizer/test-js-optimizer-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function moreLabels() {
cheez();
}
function notComps() {
if (HEAP32[$incdec_ptr71_i + 8 >> 2] != 0) {
if (HEAP32[$incdec_ptr71_i + 8 >> 2]) {
shoo();
}
}
Expand Down
13 changes: 13 additions & 0 deletions tools/js-optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ var CONTINUE_CAPTURERS = LOOP;
var COMMABLE = set('assign', 'binary', 'unary-prefix', 'unary-postfix', 'name', 'num', 'call', 'seq', 'conditional', 'sub');

var CONDITION_CHECKERS = set('if', 'do', 'while', 'switch');
var BOOLEAN_RECEIVERS = set('if', 'do', 'while', 'conditional');

var FUNCTIONS_THAT_ALWAYS_THROW = set('abort', '___resumeException', '___cxa_throw', '___cxa_rethrow');

Expand Down Expand Up @@ -945,11 +946,23 @@ function simplifyExpressions(ast) {
});
}

function simplifyNotZero(ast) {
traverse(ast, function(node, type) {
if (node[0] in BOOLEAN_RECEIVERS) {
var boolean = node[1];
if (boolean[0] === 'binary' && boolean[1] === '!=' && boolean[3][0] === 'num' && boolean[3][1] === 0) {
node[1] = boolean[2];
}
}
});
}

traverseGeneratedFunctions(ast, function(func) {
simplifyIntegerConversions(func);
simplifyOps(func);
simplifyNotComps(func);
conditionalize(func);
simplifyNotZero(func);
});
}

Expand Down
13 changes: 13 additions & 0 deletions tools/optimizer/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ StringSet ASSOCIATIVE_BINARIES("+ * | & ^"),
LOOP("do while for"),
NAME_OR_NUM("name num"),
CONDITION_CHECKERS("if do while switch"),
BOOLEAN_RECEIVERS("if do while conditional"),
SAFE_TO_DROP_COERCION("unary-prefix name num");

StringSet BREAK_CAPTURERS("do while for switch"),
Expand Down Expand Up @@ -1898,6 +1899,17 @@ void simplifyExpressions(Ref ast) {
});
};

auto simplifyNotZero = [](Ref ast) {
traversePre(ast, [](Ref node) {
if (BOOLEAN_RECEIVERS.has(node[0])) {
auto boolean = node[1];
if (boolean[0] == BINARY && boolean[1] == NE && boolean[3][0] == NUM && boolean[3][1]->getNumber() == 0) {
node[1] = boolean[2];
}
}
});
};

traverseFunctions(ast, [&](Ref func) {
simplifyIntegerConversions(func);
simplifyOps(func);
Expand All @@ -1908,6 +1920,7 @@ void simplifyExpressions(Ref ast) {
}
});
conditionalize(func);
simplifyNotZero(func);
});
}

Expand Down

0 comments on commit 6ad2079

Please sign in to comment.