enhance inline (#3832)

This commit is contained in:
Alex Lam S.L
2020-04-30 21:33:46 +01:00
committed by GitHub
parent d900006973
commit f80d5b8c9e
3 changed files with 582 additions and 21 deletions

View File

@@ -225,7 +225,7 @@ merge(Compressor.prototype, {
// output and performance.
descend(node, this);
var opt = node.optimize(this);
if (is_scope) {
if (is_scope && opt === node) {
opt.drop_unused(this);
descend(opt, this);
}
@@ -3981,6 +3981,38 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_Function, function(self, compressor) {
self.body = tighten_body(self.body, compressor);
if (compressor.option("inline")) for (var i = 0; i < self.body.length; i++) {
var stat = self.body[i];
if (stat instanceof AST_Directive) continue;
if (stat instanceof AST_Return) {
var call = stat.value;
if (!call || call.TYPE != "Call") break;
var fn = call.expression;
if (fn instanceof AST_SymbolRef) {
fn = fn.fixed_value();
}
if (!(fn instanceof AST_Lambda)) break;
if (fn.uses_arguments) break;
if (fn.contains_this()) break;
var j = fn.argnames.length;
if (j > 0 && compressor.option("inline") < 2) break;
if (j > self.argnames.length) break;
if (j < self.argnames.length && !compressor.drop_fargs(fn, call)) break;
while (--j >= 0) {
var arg = call.args[j];
if (!(arg instanceof AST_SymbolRef)) break;
if (arg.definition() !== self.argnames[j].definition()) break;
}
if (j >= 0) break;
return call.expression;
}
break;
}
return self;
});
AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
if (!compressor.option("unused")) return;
if (compressor.has_directive("use asm")) return;
@@ -6133,7 +6165,7 @@ merge(Compressor.prototype, {
function can_substitute_directly() {
if (var_assigned) return;
if (compressor.option("inline") <= 1 && fn.argnames.length) return;
if (compressor.option("inline") < 2 && fn.argnames.length) return;
if (!fn.variables.all(function(def) {
return def.references.length < 2 && def.orig[0] instanceof AST_SymbolFunarg;
})) return;