Skip to content

Commit

Permalink
Made DeadAssignmentsElimination use ES6ScopeCreator.
Browse files Browse the repository at this point in the history
Currently the hack is to move all the liveness analysis to exitScope. Also, all declarations are redeclared in the closest enclosing function scope during exitScope so that the function scope has the full picture to do the analysis.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=102382602
  • Loading branch information
blickly committed Sep 4, 2015
1 parent f1ce899 commit b27aa3b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
71 changes: 40 additions & 31 deletions src/com/google/javascript/jscomp/DeadAssignmentsElimination.java
Expand Up @@ -63,48 +63,57 @@ public DeadAssignmentsElimination(AbstractCompiler compiler) {
public void process(Node externs, Node root) { public void process(Node externs, Node root) {
Preconditions.checkNotNull(externs); Preconditions.checkNotNull(externs);
Preconditions.checkNotNull(root); Preconditions.checkNotNull(root);
NodeTraversal.traverse(compiler, root, this); NodeTraversal.traverseEs6(compiler, root, this);
} }


@Override @Override
public void enterScope(NodeTraversal t) { public void enterScope(NodeTraversal t) {
Scope scope = t.getScope(); }
// Global scope _SHOULD_ work, however, liveness won't finish without
// -Xmx1024 in closure. We might have to look at coding conventions for
// exported variables as well.
if (scope.isGlobal()) {
return;
}


if (LiveVariablesAnalysis.MAX_VARIABLES_TO_ANALYZE < @Override
t.getScope().getVarCount()) { public void exitScope(NodeTraversal t) {
// Do nothing on global scope / global blocks
if (t.inGlobalHoistScope()) {
return; return;
} }


// We are not going to do any dead assignment elimination in when there is Scope scope = t.getScope();
// at least one inner function because in most browsers, when there is a // Elevate all variable declarations up till the enclosing function scope
// closure, ALL the variables are saved (escaped). // so the liveness analysis has all variables for the process.
Node fnBlock = t.getScopeRoot().getLastChild(); if (!scope.isFunctionScope()) {
if (NodeUtil.containsFunction(fnBlock)) { for (Var var : scope.getVarIterable()) {
return; Preconditions.checkArgument(
} !var.isClass() && !var.isLet() && !var.isConst());
scope.getParent().declare(
var.getName(), var.getNameNode(), var.getInput());
}
} else {
if (LiveVariablesAnalysis.MAX_VARIABLES_TO_ANALYZE <
scope.getVarCount()) {
return;
}


// We don't do any dead assignment elimination if there are no assigns // We are not going to do any dead assignment elimination in when there is
// to eliminate. :) // at least one inner function because in most browsers, when there is a
if (!NodeUtil.has(fnBlock, matchRemovableAssigns, // closure, ALL the variables are saved (escaped).
Predicates.<Node>alwaysTrue())) { Node fnBlock = t.getScopeRoot().getLastChild();
return; if (NodeUtil.containsFunction(fnBlock)) {
} return;
}


// Computes liveness information first. // We don't do any dead assignment elimination if there are no assigns
ControlFlowGraph<Node> cfg = t.getControlFlowGraph(); // to eliminate. :)
liveness = new LiveVariablesAnalysis(cfg, scope, compiler); if (!NodeUtil.has(fnBlock, matchRemovableAssigns,
liveness.analyze(); Predicates.<Node>alwaysTrue())) {
tryRemoveDeadAssignments(t, cfg); return;
} }


@Override // Computes liveness information first.
public void exitScope(NodeTraversal t) { ControlFlowGraph<Node> cfg = t.getControlFlowGraph();
liveness = new LiveVariablesAnalysis(cfg, scope, compiler);
liveness.analyze();
tryRemoveDeadAssignments(t, cfg);
}
} }


@Override @Override
Expand Down
Expand Up @@ -33,7 +33,7 @@ public CompilerPass getProcessor(final Compiler compiler) {
return new CompilerPass() { return new CompilerPass() {
@Override @Override
public void process(Node externs, Node js) { public void process(Node externs, Node js) {
NodeTraversal.traverse( NodeTraversal.traverseEs6(
compiler, js, new DeadAssignmentsElimination(compiler)); compiler, js, new DeadAssignmentsElimination(compiler));
} }
}; };
Expand Down

0 comments on commit b27aa3b

Please sign in to comment.