enhance reduce_vars (#5616)

closes #5614
This commit is contained in:
Alex Lam S.L
2022-08-17 02:25:59 +01:00
committed by GitHub
parent 887e086890
commit d6d2f5ced2
3 changed files with 237 additions and 28 deletions

View File

@@ -1053,9 +1053,6 @@ Compressor.prototype.compress = function(node) {
if (!fixed || sym.in_arg || !safe_to_assign(tw, d)) {
walk();
d.fixed = false;
} else if (modified) {
walk();
d.fixed = 0;
} else {
push_ref(d, sym);
mark(tw, d);
@@ -1064,7 +1061,8 @@ Compressor.prototype.compress = function(node) {
d.single_use = false;
}
tw.loop_ids[d.id] = tw.in_loop;
sym.fixed = d.fixed = fixed;
d.fixed = modified ? 0 : fixed;
sym.fixed = fixed;
sym.fixed.assigns = [ node ];
mark_escaped(tw, d, sym.scope, node, right, 0, 1);
}
@@ -1355,8 +1353,10 @@ Compressor.prototype.compress = function(node) {
this.definition().fixed = false;
});
def(AST_SymbolRef, function(tw, descend, compressor) {
var d = this.definition();
push_ref(d, this);
var ref = this;
var d = ref.definition();
var fixed = d.fixed || d.last_ref && d.last_ref.fixed;
push_ref(d, ref);
if (d.references.length == 1 && !d.fixed && d.orig[0] instanceof AST_SymbolDefun) {
tw.loop_ids[d.id] = tw.in_loop;
}
@@ -1371,14 +1371,27 @@ Compressor.prototype.compress = function(node) {
if (!safe) return;
safe.assign = true;
});
if (d.fixed === false || d.fixed === 0) {
if (d.single_use == "m" && d.fixed) {
d.fixed = 0;
d.single_use = false;
}
switch (d.fixed) {
case 0:
if (!safe_to_read(tw, d)) d.fixed = false;
case false:
var redef = d.redefined();
if (redef && cross_scope(d.scope, this.scope)) redef.single_use = false;
} else if (d.fixed === undefined || !safe_to_read(tw, d)) {
if (redef && cross_scope(d.scope, ref.scope)) redef.single_use = false;
break;
case undefined:
d.fixed = false;
} else if (d.fixed) {
if (this.in_arg && d.orig[0] instanceof AST_SymbolLambda) this.fixed = d.scope;
var value = this.fixed_value();
break;
default:
if (!safe_to_read(tw, d)) {
d.fixed = false;
break;
}
if (ref.in_arg && d.orig[0] instanceof AST_SymbolLambda) ref.fixed = d.scope;
var value = ref.fixed_value();
if (recursive) {
d.recursive_refs++;
} else if (value && ref_once(compressor, d)) {
@@ -1387,12 +1400,12 @@ Compressor.prototype.compress = function(node) {
&& !value.pinned()
&& (!d.in_loop || tw.parent() instanceof AST_Call)
|| !d.in_loop
&& d.scope === this.scope.resolve()
&& d.scope === ref.scope.resolve()
&& value.is_constant_expression();
} else {
d.single_use = false;
}
if (is_modified(compressor, tw, this, value, 0, is_immutable(value), recursive)) {
if (is_modified(compressor, tw, ref, value, 0, is_immutable(value), recursive)) {
if (d.single_use) {
d.single_use = "m";
} else {
@@ -1400,12 +1413,13 @@ Compressor.prototype.compress = function(node) {
}
}
if (d.fixed && tw.loop_ids[d.id] !== tw.in_loop) d.cross_loop = true;
mark_escaped(tw, d, this.scope, this, value, 0, 1);
mark_escaped(tw, d, ref.scope, ref, value, 0, 1);
break;
}
if (!this.fixed) this.fixed = d.fixed;
if (!ref.fixed) ref.fixed = d.fixed === 0 ? fixed : d.fixed;
var parent;
if (value instanceof AST_Lambda
&& !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) {
&& !((parent = tw.parent()) instanceof AST_Call && parent.expression === ref)) {
mark_fn_def(tw, d, value);
}
});
@@ -1578,8 +1592,9 @@ Compressor.prototype.compress = function(node) {
this.walk(tw);
});
AST_Symbol.DEFMETHOD("fixed_value", function() {
var fixed = this.definition().fixed;
AST_Symbol.DEFMETHOD("fixed_value", function(ref_only) {
var def = this.definition();
var fixed = def.fixed;
if (fixed) {
if (this.fixed) fixed = this.fixed;
return (fixed instanceof AST_Node ? fixed : fixed()).tail_node();
@@ -1587,7 +1602,8 @@ Compressor.prototype.compress = function(node) {
fixed = fixed === 0 && this.fixed;
if (!fixed) return fixed;
var value = (fixed instanceof AST_Node ? fixed : fixed()).tail_node();
return value.is_constant() && value;
if (ref_only && def.escaped.depth != 1 && is_object(value, true)) return value;
if (value.is_constant()) return value;
});
AST_SymbolRef.DEFMETHOD("is_immutable", function() {
@@ -4510,7 +4526,7 @@ Compressor.prototype.compress = function(node) {
if (is_arguments(def) && !def.scope.rest && all(def.scope.argnames, function(argname) {
return argname instanceof AST_SymbolFunarg;
})) return def.scope.uses_arguments > 2;
var fixed = this.fixed_value();
var fixed = this.fixed_value(true);
if (!fixed) return true;
this._dot_throw = return_true;
if (fixed._dot_throw(compressor)) {
@@ -11454,14 +11470,14 @@ Compressor.prototype.compress = function(node) {
var indexFns = makePredicate("indexOf lastIndexOf");
var commutativeOperators = makePredicate("== === != !== * & | ^");
function is_object(node) {
if (node instanceof AST_Assign) return node.operator == "=" && is_object(node.right);
if (node instanceof AST_Sequence) return is_object(node.tail_node());
if (node instanceof AST_SymbolRef) return is_object(node.fixed_value());
function is_object(node, plain) {
if (node instanceof AST_Assign) return !plain && node.operator == "=" && is_object(node.right);
if (node instanceof AST_New) return !plain;
if (node instanceof AST_Sequence) return is_object(node.tail_node(), plain);
if (node instanceof AST_SymbolRef) return !plain && is_object(node.fixed_value());
return node instanceof AST_Array
|| node instanceof AST_Class
|| node instanceof AST_Lambda
|| node instanceof AST_New
|| node instanceof AST_Object;
}