enhance evaluate & reduce_vars (#3873)

This commit is contained in:
Alex Lam S.L
2020-05-10 20:08:05 +01:00
committed by GitHub
parent 7e0ad232b0
commit e23bf48052
4 changed files with 188 additions and 67 deletions

View File

@@ -492,8 +492,7 @@ merge(Compressor.prototype, {
function ref_once(tw, compressor, def) {
return compressor.option("unused")
&& !def.scope.pinned()
&& def.references.length - def.recursive_refs == 1
&& tw.loop_ids[def.id] === tw.in_loop;
&& def.references.length - def.recursive_refs == 1;
}
function is_immutable(value) {
@@ -797,8 +796,9 @@ merge(Compressor.prototype, {
if (recursive) {
d.recursive_refs++;
} else if (value && ref_once(tw, compressor, d)) {
d.in_loop = tw.loop_ids[d.id] !== tw.in_loop;
d.single_use = value instanceof AST_Lambda && !value.pinned()
|| d.scope === this.scope && value.is_constant_expression();
|| !d.in_loop && d.scope === this.scope && value.is_constant_expression();
} else {
d.single_use = false;
}
@@ -3501,7 +3501,21 @@ merge(Compressor.prototype, {
if (fn.name && fn.name.definition().recursive_refs > 0) return this;
if (this.is_expr_pure(compressor)) return this;
var stat = fn.first_statement();
if (!(stat instanceof AST_Return)) return this;
if (!(stat instanceof AST_Return)) {
if (ignore_side_effects) {
var found = false;
fn.walk(new TreeWalker(function(node) {
if (found) return true;
if (node instanceof AST_Return) {
if (node.value && node.value.evaluate(compressor, true) !== undefined) found = true;
return true;
}
if (node instanceof AST_Scope && node !== fn) return true;
}));
if (!found) return void 0;
}
return this;
}
var args = eval_args(this.args);
if (!args) return this;
if (!stat.value) return undefined;
@@ -7427,7 +7441,7 @@ merge(Compressor.prototype, {
var single_use = def.single_use && !(parent instanceof AST_Call && parent.is_expr_pure(compressor));
if (single_use) {
if (fixed instanceof AST_Lambda) {
if (def.scope !== self.scope
if ((def.scope !== self.scope || def.in_loop)
&& (!compressor.option("reduce_funcs") || def.escaped.depth == 1 || fixed.inlined)) {
single_use = false;
} else if (recursive_ref(compressor, def)) {
@@ -7771,7 +7785,7 @@ merge(Compressor.prototype, {
expressions.push(self);
return make_sequence(self, expressions);
}
var condition = self.condition.is_truthy() || self.condition.evaluate(compressor);
var condition = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
if (!condition) {
AST_Node.warn("Condition always false [{file}:{line},{col}]", self.start);
return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor);