uncommaify between ifs that can potentially be simplified

This commit is contained in:
Alon Zakai 2014-03-15 15:44:14 -07:00
Родитель f6c2bcd2a3
Коммит 6c31546ced
4 изменённых файлов: 60 добавлений и 9 удалений

4
emcc
Просмотреть файл

@ -1689,7 +1689,9 @@ try:
js_optimizer_queue += ['simplifyExpressions']
if debug_level == 0 or profiling: js_optimizer_queue += ['simplifyIfs']
# simplify ifs if it is ok to make the code somewhat unreadable, and unless outlining (simplified ifs
# with commaified code breaks late aggressive variable elimination)
if (debug_level == 0 or profiling) and shared.Settings.OUTLINING_LIMIT == 0: js_optimizer_queue += ['simplifyIfs']
if opt_level >= 3 and shared.Settings.PRECISE_F32: js_optimizer_queue += ['optimizeFrounds']

Просмотреть файл

@ -140,6 +140,8 @@ var ALTER_FLOW = set('break', 'continue', 'return');
var BREAK_CAPTURERS = set('do', 'while', 'for', 'switch');
var CONTINUE_CAPTURERS = LOOP;
var COMMABLE = set('assign', 'binary', 'unary-prefix', 'unary-postfix', 'name', 'num', 'call', 'seq', 'conditional', 'sub');
var FUNCTIONS_THAT_ALWAYS_THROW = set('abort', '___resumeException', '___cxa_throw', '___cxa_rethrow');
var NULL_NODE = ['name', 'null'];
@ -832,8 +834,29 @@ function simplifyIfs(ast) {
var body = node[2];
// recurse to handle chains
while (body[0] === 'block') {
if (body[1].length !== 1) break;
var singleton = body[1][0];
var stats = body[1];
if (stats.length > 1) {
var last = stats[stats.length-1];
if (last[0] === 'if' && !last[3]) {
// try to commaify - turn everything between the ifs into a comma operator inside the second if
var ok = true;
for (var i = 0; i < stats.length-1; i++) {
var curr = stats[i];
if (curr[0] === 'stat') curr = curr[1];
if (!(curr[0] in COMMABLE)) ok = false;
}
if (ok) {
for (var i = stats.length-2; i >= 0; i--) {
var curr = stats[i];
if (curr[0] === 'stat') curr = curr[1];
last[1] = ['seq', curr, last[1]];
}
stats = body[1] = [last];
}
}
}
if (stats.length !== 1) break;
var singleton = stats[0];
if (singleton[0] === 'if' && !singleton[3]) {
node[1] = ['conditional', node[1], singleton[1], ['num', 0]];
body = node[2] = singleton[2];

Просмотреть файл

@ -23,7 +23,7 @@ function a() {
h();
}
if (x) {
f();
return;
if (y) {
g();
}
@ -32,13 +32,13 @@ function a() {
g();
}
if (x) {
f();
return;
if (y ? z : 0) {
g();
}
}
if (x ? y : 0) {
f();
return;
if (z) {
g();
}
@ -55,5 +55,16 @@ function a() {
}
f();
}
if (x ? (f(), x = x + 2 | 0, y) : 0) {
g();
}
if (x) {
f();
x = x + 2 | 0;
return;
if (y) {
g();
}
}
}

Просмотреть файл

@ -25,7 +25,7 @@ function a() {
h();
}
if (x) {
f();
return;
if (y) {
g();
}
@ -38,7 +38,7 @@ function a() {
}
}
if (x) {
f();
return;
if (y) {
if (z) {
g();
@ -47,7 +47,7 @@ function a() {
}
if (x) {
if (y) {
f();
return;
if (z) {
g();
}
@ -69,5 +69,20 @@ function a() {
}
f();
}
if (x) {
f();
x = x + 2 | 0;
if (y) {
g();
}
}
if (x) {
f();
x = x + 2 | 0;
return;
if (y) {
g();
}
}
}