enhance unused (#3794)

This commit is contained in:
Alex Lam S.L
2020-04-18 11:52:44 +01:00
committed by GitHub
parent b38838c6bf
commit 60d4e7b09f
6 changed files with 54 additions and 30 deletions

View File

@@ -4134,6 +4134,7 @@ merge(Compressor.prototype, {
// side effects, we can cascade the init. code // side effects, we can cascade the init. code
// into the next one, or next statement. // into the next one, or next statement.
var side_effects = []; var side_effects = [];
var duplicated = 0;
node.definitions.forEach(function(def) { node.definitions.forEach(function(def) {
if (def.value) def.value = def.value.transform(tt); if (def.value) def.value = def.value.transform(tt);
var sym = def.name.definition(); var sym = def.name.definition();
@@ -4142,25 +4143,18 @@ merge(Compressor.prototype, {
def.value = def.value.drop_side_effect_free(compressor); def.value = def.value.drop_side_effect_free(compressor);
} }
var var_defs = var_defs_by_id.get(sym.id); var var_defs = var_defs_by_id.get(sym.id);
if (var_defs.length > 1 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) { if (var_defs.length > 1) {
AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); if (!def.value) {
if (def.value) { AST_Node.warn("Dropping duplicated declaration of variable {name} [{file}:{line},{col}]", template(def.name));
var ref = make_node(AST_SymbolRef, def.name, def.name);
sym.references.push(ref);
var assign = make_node(AST_Assign, def, {
operator: "=",
left: ref,
right: def.value
});
if (fixed_ids[sym.id] === def) {
fixed_ids[sym.id] = assign;
}
side_effects.push(assign.transform(tt));
}
remove(var_defs, def); remove(var_defs, def);
sym.eliminated++; sym.eliminated++;
return; return;
} }
if (sym.orig.indexOf(def.name) > sym.eliminated) {
remove(var_defs, def);
duplicated++;
}
}
if (!def.value) { if (!def.value) {
head.push(def); head.push(def);
} else if (compressor.option("functions") } else if (compressor.option("functions")
@@ -4220,7 +4214,22 @@ merge(Compressor.prototype, {
return !def || fn.name && def === fn.name.definition(); return !def || fn.name && def === fn.name.definition();
} }
}); });
if (head.length > 0 || tail.length > 0) { if (head.length == 0 && tail.length == duplicated) {
[].unshift.apply(side_effects, tail.map(function(def) {
AST_Node.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
var sym = def.name.definition();
var ref = make_node(AST_SymbolRef, def.name, def.name);
sym.references.push(ref);
var assign = make_node(AST_Assign, def, {
operator: "=",
left: ref,
right: def.value
});
if (fixed_ids[sym.id] === def) fixed_ids[sym.id] = assign;
sym.eliminated++;
return assign.transform(tt);
}));
} else if (head.length > 0 || tail.length > 0) {
node.definitions = head.concat(tail); node.definitions = head.concat(tail);
body.push(node); body.push(node);
} }

View File

@@ -91,8 +91,7 @@ asm_mixed: {
function no_asm_GeometricMean(stdlib, foreign, buffer) { function no_asm_GeometricMean(stdlib, foreign, buffer) {
function logSum(start, end) { function logSum(start, end) {
start |= 0, end |= 0; start |= 0, end |= 0;
var sum = 0, p = 0, q = 0; for (var sum = 0, p = 0, q = 0, p = start << 3, q = end << 3; (0 | p) < (0 | q); p = p + 8 | 0) sum += +log(values[p >> 3]);
for (p = start << 3, q = end << 3; (0 | p) < (0 | q); p = p + 8 | 0) sum += +log(values[p >> 3]);
return +sum; return +sum;
} }
function geometricMean(start, end) { function geometricMean(start, end) {

View File

@@ -2570,8 +2570,7 @@ chained_3: {
} }
expect: { expect: {
console.log(function(a, b) { console.log(function(a, b) {
var c = 1; var c = 1, c = b;
c = b;
b++; b++;
return c; return c;
}(0, 2)); }(0, 2));

View File

@@ -2444,3 +2444,23 @@ issue_3746: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
drop_duplicated_side_effects: {
options = {
toplevel: true,
unused: true,
}
input: {
var a = 0;
for (var i = 1; i--;)
var a = 0, b = ++a;
console.log(a);
}
expect: {
var a = 0;
for (var i = 1; i--;)
a = 0, ++a;
console.log(a);
}
expect_stdout: "1"
}

View File

@@ -1226,8 +1226,7 @@ chained_3: {
} }
expect: { expect: {
console.log(function(b) { console.log(function(b) {
var c = 1; var c = 1, c = b;
c = b;
b++; b++;
return c; return c;
}(2)); }(2));

View File

@@ -5434,8 +5434,7 @@ lvalues_def_1: {
} }
expect: { expect: {
var b = 1; var b = 1;
var a = b++; var a = b++, b = NaN;
b = NaN;
console.log(a, b); console.log(a, b);
} }
expect_stdout: "1 NaN" expect_stdout: "1 NaN"
@@ -5454,8 +5453,7 @@ lvalues_def_2: {
} }
expect: { expect: {
var b = 1; var b = 1;
var a = b += 1; var a = b += 1, b = NaN;
b = NaN;
console.log(a, b); console.log(a, b);
} }
expect_stdout: "2 NaN" expect_stdout: "2 NaN"
@@ -6867,9 +6865,9 @@ issue_3666: {
} finally { } finally {
for (;!a;) for (;!a;)
a++; a++;
var b = a = "PASS"; a = "PASS";
} }
console.log(a, b); console.log(a, "PASS");
} }
expect_stdout: "PASS PASS" expect_stdout: "PASS PASS"
} }