enhance loops (#3633)

This commit is contained in:
Alex Lam S.L
2019-12-10 12:57:47 +00:00
committed by GitHub
parent 18c2b1841b
commit 036bca980c
2 changed files with 162 additions and 13 deletions

View File

@@ -4718,8 +4718,8 @@ merge(Compressor.prototype, {
});
function if_break_in_loop(self, compressor) {
var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body;
if (compressor.option("dead_code") && is_break(first)) {
var first = first_statement(self.body);
if (compressor.option("dead_code") && (breaks(first) || first instanceof AST_Exit)) {
var body = [];
if (self.init instanceof AST_Statement) {
body.push(self.init);
@@ -4728,10 +4728,19 @@ merge(Compressor.prototype, {
body: self.init
}));
}
if (self.condition) {
var retain = external_target(first) || first instanceof AST_Exit;
if (self.condition && retain) {
body.push(make_node(AST_If, self, {
condition: self.condition,
body: first,
alternative: null
}));
} else if (self.condition) {
body.push(make_node(AST_SimpleStatement, self.condition, {
body: self.condition
}));
} else if (retain) {
body.push(first);
}
extract_declarations_from_unreachable_code(self.body, body);
return make_node(AST_BlockStatement, self, {
@@ -4739,7 +4748,8 @@ merge(Compressor.prototype, {
});
}
if (first instanceof AST_If) {
if (is_break(first.body)) {
var ab = first_statement(first.body);
if (breaks(ab)) {
if (self.condition) {
self.condition = make_node(AST_Binary, self.condition, {
left: self.condition,
@@ -4749,8 +4759,12 @@ merge(Compressor.prototype, {
} else {
self.condition = first.condition.negate(compressor);
}
drop_it(first.alternative);
} else if (is_break(first.alternative)) {
var body = as_statement_array(first.alternative);
extract_declarations_from_unreachable_code(first.body, body);
return drop_it(body);
}
ab = first_statement(first.alternative);
if (breaks(ab)) {
if (self.condition) {
self.condition = make_node(AST_Binary, self.condition, {
left: self.condition,
@@ -4760,18 +4774,27 @@ merge(Compressor.prototype, {
} else {
self.condition = first.condition;
}
drop_it(first.body);
var body = as_statement_array(first.body);
extract_declarations_from_unreachable_code(first.alternative, body);
return drop_it(body);
}
}
return self;
function is_break(node) {
function first_statement(body) {
return body instanceof AST_BlockStatement ? body.body[0] : body;
}
function external_target(node) {
return compressor.loopcontrol_target(node) !== compressor.self();
}
function breaks(node) {
return node instanceof AST_Break
&& compressor.loopcontrol_target(node) === compressor.self();
|| node instanceof AST_Continue && external_target(node);
}
function drop_it(rest) {
rest = as_statement_array(rest);
if (self.body instanceof AST_BlockStatement) {
self.body = self.body.clone();
self.body.body = rest.concat(self.body.body.slice(1));
@@ -4781,7 +4804,7 @@ merge(Compressor.prototype, {
body: rest
}).transform(compressor);
}
self = if_break_in_loop(self, compressor);
return if_break_in_loop(self, compressor);
}
}