Skip to content

Commit d6d2f5c

Browse files
authored
enhance reduce_vars (#5616)
closes #5614
1 parent 887e086 commit d6d2f5c

File tree

3 files changed

+237
-28
lines changed

3 files changed

+237
-28
lines changed

lib/compress.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,9 +1053,6 @@ Compressor.prototype.compress = function(node) {
10531053
if (!fixed || sym.in_arg || !safe_to_assign(tw, d)) {
10541054
walk();
10551055
d.fixed = false;
1056-
} else if (modified) {
1057-
walk();
1058-
d.fixed = 0;
10591056
} else {
10601057
push_ref(d, sym);
10611058
mark(tw, d);
@@ -1064,7 +1061,8 @@ Compressor.prototype.compress = function(node) {
10641061
d.single_use = false;
10651062
}
10661063
tw.loop_ids[d.id] = tw.in_loop;
1067-
sym.fixed = d.fixed = fixed;
1064+
d.fixed = modified ? 0 : fixed;
1065+
sym.fixed = fixed;
10681066
sym.fixed.assigns = [ node ];
10691067
mark_escaped(tw, d, sym.scope, node, right, 0, 1);
10701068
}
@@ -1355,8 +1353,10 @@ Compressor.prototype.compress = function(node) {
13551353
this.definition().fixed = false;
13561354
});
13571355
def(AST_SymbolRef, function(tw, descend, compressor) {
1358-
var d = this.definition();
1359-
push_ref(d, this);
1356+
var ref = this;
1357+
var d = ref.definition();
1358+
var fixed = d.fixed || d.last_ref && d.last_ref.fixed;
1359+
push_ref(d, ref);
13601360
if (d.references.length == 1 && !d.fixed && d.orig[0] instanceof AST_SymbolDefun) {
13611361
tw.loop_ids[d.id] = tw.in_loop;
13621362
}
@@ -1371,14 +1371,27 @@ Compressor.prototype.compress = function(node) {
13711371
if (!safe) return;
13721372
safe.assign = true;
13731373
});
1374-
if (d.fixed === false || d.fixed === 0) {
1374+
if (d.single_use == "m" && d.fixed) {
1375+
d.fixed = 0;
1376+
d.single_use = false;
1377+
}
1378+
switch (d.fixed) {
1379+
case 0:
1380+
if (!safe_to_read(tw, d)) d.fixed = false;
1381+
case false:
13751382
var redef = d.redefined();
1376-
if (redef && cross_scope(d.scope, this.scope)) redef.single_use = false;
1377-
} else if (d.fixed === undefined || !safe_to_read(tw, d)) {
1383+
if (redef && cross_scope(d.scope, ref.scope)) redef.single_use = false;
1384+
break;
1385+
case undefined:
13781386
d.fixed = false;
1379-
} else if (d.fixed) {
1380-
if (this.in_arg && d.orig[0] instanceof AST_SymbolLambda) this.fixed = d.scope;
1381-
var value = this.fixed_value();
1387+
break;
1388+
default:
1389+
if (!safe_to_read(tw, d)) {
1390+
d.fixed = false;
1391+
break;
1392+
}
1393+
if (ref.in_arg && d.orig[0] instanceof AST_SymbolLambda) ref.fixed = d.scope;
1394+
var value = ref.fixed_value();
13821395
if (recursive) {
13831396
d.recursive_refs++;
13841397
} else if (value && ref_once(compressor, d)) {
@@ -1387,25 +1400,26 @@ Compressor.prototype.compress = function(node) {
13871400
&& !value.pinned()
13881401
&& (!d.in_loop || tw.parent() instanceof AST_Call)
13891402
|| !d.in_loop
1390-
&& d.scope === this.scope.resolve()
1403+
&& d.scope === ref.scope.resolve()
13911404
&& value.is_constant_expression();
13921405
} else {
13931406
d.single_use = false;
13941407
}
1395-
if (is_modified(compressor, tw, this, value, 0, is_immutable(value), recursive)) {
1408+
if (is_modified(compressor, tw, ref, value, 0, is_immutable(value), recursive)) {
13961409
if (d.single_use) {
13971410
d.single_use = "m";
13981411
} else {
13991412
d.fixed = 0;
14001413
}
14011414
}
14021415
if (d.fixed && tw.loop_ids[d.id] !== tw.in_loop) d.cross_loop = true;
1403-
mark_escaped(tw, d, this.scope, this, value, 0, 1);
1416+
mark_escaped(tw, d, ref.scope, ref, value, 0, 1);
1417+
break;
14041418
}
1405-
if (!this.fixed) this.fixed = d.fixed;
1419+
if (!ref.fixed) ref.fixed = d.fixed === 0 ? fixed : d.fixed;
14061420
var parent;
14071421
if (value instanceof AST_Lambda
1408-
&& !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) {
1422+
&& !((parent = tw.parent()) instanceof AST_Call && parent.expression === ref)) {
14091423
mark_fn_def(tw, d, value);
14101424
}
14111425
});
@@ -1578,16 +1592,18 @@ Compressor.prototype.compress = function(node) {
15781592
this.walk(tw);
15791593
});
15801594

1581-
AST_Symbol.DEFMETHOD("fixed_value", function() {
1582-
var fixed = this.definition().fixed;
1595+
AST_Symbol.DEFMETHOD("fixed_value", function(ref_only) {
1596+
var def = this.definition();
1597+
var fixed = def.fixed;
15831598
if (fixed) {
15841599
if (this.fixed) fixed = this.fixed;
15851600
return (fixed instanceof AST_Node ? fixed : fixed()).tail_node();
15861601
}
15871602
fixed = fixed === 0 && this.fixed;
15881603
if (!fixed) return fixed;
15891604
var value = (fixed instanceof AST_Node ? fixed : fixed()).tail_node();
1590-
return value.is_constant() && value;
1605+
if (ref_only && def.escaped.depth != 1 && is_object(value, true)) return value;
1606+
if (value.is_constant()) return value;
15911607
});
15921608

15931609
AST_SymbolRef.DEFMETHOD("is_immutable", function() {
@@ -4510,7 +4526,7 @@ Compressor.prototype.compress = function(node) {
45104526
if (is_arguments(def) && !def.scope.rest && all(def.scope.argnames, function(argname) {
45114527
return argname instanceof AST_SymbolFunarg;
45124528
})) return def.scope.uses_arguments > 2;
4513-
var fixed = this.fixed_value();
4529+
var fixed = this.fixed_value(true);
45144530
if (!fixed) return true;
45154531
this._dot_throw = return_true;
45164532
if (fixed._dot_throw(compressor)) {
@@ -11454,14 +11470,14 @@ Compressor.prototype.compress = function(node) {
1145411470

1145511471
var indexFns = makePredicate("indexOf lastIndexOf");
1145611472
var commutativeOperators = makePredicate("== === != !== * & | ^");
11457-
function is_object(node) {
11458-
if (node instanceof AST_Assign) return node.operator == "=" && is_object(node.right);
11459-
if (node instanceof AST_Sequence) return is_object(node.tail_node());
11460-
if (node instanceof AST_SymbolRef) return is_object(node.fixed_value());
11473+
function is_object(node, plain) {
11474+
if (node instanceof AST_Assign) return !plain && node.operator == "=" && is_object(node.right);
11475+
if (node instanceof AST_New) return !plain;
11476+
if (node instanceof AST_Sequence) return is_object(node.tail_node(), plain);
11477+
if (node instanceof AST_SymbolRef) return !plain && is_object(node.fixed_value());
1146111478
return node instanceof AST_Array
1146211479
|| node instanceof AST_Class
1146311480
|| node instanceof AST_Lambda
11464-
|| node instanceof AST_New
1146511481
|| node instanceof AST_Object;
1146611482
}
1146711483

test/compress/dead-code.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,8 +1485,6 @@ self_assignments_5: {
14851485
}
14861486
expect: {
14871487
var i = 0, l = [ "FAIL", "PASS" ];
1488-
l[0];
1489-
l[0];
14901488
l[0] = l[1];
14911489
console.log(l[0], 2);
14921490
}

test/compress/issue-5614.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
record_update: {
2+
options = {
3+
loops: true,
4+
passes: 3,
5+
pure_getters: "strict",
6+
reduce_vars: true,
7+
side_effects: true,
8+
toplevel: true,
9+
unused: true,
10+
}
11+
input: {
12+
var value = { a: 42, b: "PASS" };
13+
var unused = _Utils_update(value, { b: "FAIL" });
14+
function _Utils_update(oldRecord, updatedFields) {
15+
var newRecord = {};
16+
for (var key in oldRecord)
17+
newRecord[key] = oldRecord[key];
18+
for (var key in updatedFields)
19+
newRecord[key] = updatedFields[key];
20+
return newRecord;
21+
}
22+
}
23+
expect: {}
24+
}
25+
26+
currying: {
27+
options = {
28+
inline: true,
29+
passes: 2,
30+
pure_getters: "strict",
31+
reduce_funcs: true,
32+
reduce_vars: true,
33+
sequences: true,
34+
side_effects: true,
35+
toplevel: true,
36+
unused: true,
37+
}
38+
input: {
39+
function F(arity, fun, wrapper) {
40+
wrapper.a = arity;
41+
wrapper.f = fun;
42+
return wrapper;
43+
}
44+
function F2(fun) {
45+
return F(2, fun, function(a) {
46+
return function(b) {
47+
return fun(a, b);
48+
};
49+
});
50+
}
51+
function _Utils_eq(x, y) {
52+
var pair, stack = [], isEqual = _Utils_eqHelp(x, y, 0, stack);
53+
while (isEqual && (pair = stack.pop()))
54+
isEqual = _Utils_eqHelp(pair.a, pair.b, 0, stack);
55+
return isEqual;
56+
}
57+
var _Utils_equal = F2(_Utils_eq);
58+
}
59+
expect: {}
60+
}
61+
62+
conditional_property_write: {
63+
options = {
64+
pure_getters: "strict",
65+
reduce_vars: true,
66+
unused: true,
67+
}
68+
input: {
69+
function f(a) {
70+
var o = {};
71+
if (a)
72+
o.p = console.log("foo");
73+
else
74+
o.q = console.log("bar");
75+
o.r = console.log("baz");
76+
}
77+
f(42);
78+
f(null);
79+
}
80+
expect: {
81+
function f(a) {
82+
if (a)
83+
console.log("foo");
84+
else
85+
console.log("bar");
86+
console.log("baz");
87+
}
88+
f(42);
89+
f(null);
90+
}
91+
expect_stdout: [
92+
"foo",
93+
"baz",
94+
"bar",
95+
"baz",
96+
]
97+
}
98+
99+
reassign_1: {
100+
options = {
101+
reduce_vars: true,
102+
toplevel: true,
103+
unused: true,
104+
}
105+
input: {
106+
var a = "PASS", b = "FAIL";
107+
(b = a).toString();
108+
console.log(b);
109+
}
110+
expect: {
111+
var b = "FAIL";
112+
(b = "PASS").toString();
113+
console.log(b);
114+
}
115+
expect_stdout: "PASS"
116+
}
117+
118+
reassign_2: {
119+
options = {
120+
evaluate: true,
121+
reduce_vars: true,
122+
toplevel: true,
123+
}
124+
input: {
125+
var a = "PASS";
126+
if (false) {
127+
a = null + 0;
128+
a();
129+
}
130+
console.log(a);
131+
}
132+
expect: {
133+
var a = "PASS";
134+
if (false) {
135+
a = 0;
136+
a();
137+
}
138+
console.log(a);
139+
}
140+
expect_stdout: "PASS"
141+
}
142+
143+
reassign_3: {
144+
options = {
145+
evaluate: true,
146+
reduce_vars: true,
147+
toplevel: true,
148+
}
149+
input: {
150+
var a = 0;
151+
(a = a || "PASS").toString();
152+
console.log(a);
153+
}
154+
expect: {
155+
var a = 0;
156+
(a = (0, "PASS")).toString();
157+
console.log(a);
158+
}
159+
expect_stdout: "PASS"
160+
}
161+
162+
retain_instance_write: {
163+
options = {
164+
pure_getters: true,
165+
reduce_vars: true,
166+
unused: true,
167+
}
168+
input: {
169+
function f(a) {
170+
return a;
171+
}
172+
function g() {
173+
var o = {};
174+
var b = new f(o);
175+
if (console)
176+
b.p = "PASS";
177+
return o;
178+
}
179+
console.log(g().p);
180+
}
181+
expect: {
182+
function f(a) {
183+
return a;
184+
}
185+
function g() {
186+
var o = {};
187+
var b = new f(o);
188+
if (console)
189+
b.p = "PASS";
190+
return o;
191+
}
192+
console.log(g().p);
193+
}
194+
expect_stdout: "PASS"
195+
}

0 commit comments

Comments
 (0)