From 72d8adb709b120793a000fd1229d787e4111e4e3 Mon Sep 17 00:00:00 2001 From: lillymliu Date: Mon, 26 Jun 2017 17:57:36 -0700 Subject: [PATCH] Convert DeadAssignmentsElimination pass to use the ES6 Syntactic Scope Creator. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=160222117 --- .../jscomp/DeadAssignmentsElimination.java | 19 ++++++++----------- .../jscomp/LiveVariablesAnalysisEs6.java | 12 +++--------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/com/google/javascript/jscomp/DeadAssignmentsElimination.java b/src/com/google/javascript/jscomp/DeadAssignmentsElimination.java index d257c912d4b..0d96c532e03 100644 --- a/src/com/google/javascript/jscomp/DeadAssignmentsElimination.java +++ b/src/com/google/javascript/jscomp/DeadAssignmentsElimination.java @@ -22,7 +22,7 @@ import com.google.javascript.jscomp.ControlFlowGraph.Branch; import com.google.javascript.jscomp.DataFlowAnalysis.FlowState; -import com.google.javascript.jscomp.LiveVariablesAnalysis.LiveVariableLattice; +import com.google.javascript.jscomp.LiveVariablesAnalysisEs6.LiveVariableLattice; import com.google.javascript.jscomp.NodeTraversal.AbstractScopedCallback; import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode; import com.google.javascript.rhino.IR; @@ -32,7 +32,7 @@ /** * Removes local variable assignments that are useless based on information from {@link - * LiveVariablesAnalysis}. If there is an assignment to variable {@code x} and {@code x} is dead + * LiveVariablesAnalysisEs6}. If there is an assignment to variable {@code x} and {@code x} is dead * after this assignment, we know that the current content of {@code x} will not be read and this * assignment is useless. * @@ -40,7 +40,7 @@ class DeadAssignmentsElimination extends AbstractScopedCallback implements CompilerPass { private final AbstractCompiler compiler; - private LiveVariablesAnalysis liveness; + private LiveVariablesAnalysisEs6 liveness; private final Deque functionStack; private static final class BailoutInformation { @@ -126,12 +126,9 @@ private void eliminateDeadAssignments(NodeTraversal t) { // Computes liveness information first. ControlFlowGraph cfg = t.getControlFlowGraph(); - /*TODO (simranarora) We are currently traversing in Es6 for this pass, but the conversion - *to an Es6 scope creator is breaking existing test cases - */ liveness = - new LiveVariablesAnalysis( - cfg, functionScope, compiler, SyntacticScopeCreator.makeUntyped(compiler)); + new LiveVariablesAnalysisEs6( + cfg, functionScope, blockScope, compiler, new Es6SyntacticScopeCreator(compiler)); liveness.analyze(); tryRemoveDeadAssignments(t, cfg); } @@ -266,12 +263,12 @@ private void tryRemoveAssignment(NodeTraversal t, Node n, Node exprRoot, return; } - if (state.getOut().isLive(var)) { + if (state.getOut().isLive(liveness.getVarIndex(var.name))) { return; // Variable not dead. } - if (state.getIn().isLive(var) && - isVariableStillLiveWithinExpression(n, exprRoot, var.name)) { + if (state.getIn().isLive(liveness.getVarIndex(var.name)) + && isVariableStillLiveWithinExpression(n, exprRoot, var.name)) { // The variable is killed here but it is also live before it. // This is possible if we have say: // if (X = a && a = C) {..} ; .......; a = S; diff --git a/src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java b/src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java index 0205894a3bc..e31ba48dbca 100644 --- a/src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java +++ b/src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java @@ -88,19 +88,13 @@ public boolean equals(Object other) { && this.liveSet.equals(((LiveVariableLattice) other).liveSet); } - public boolean isLive(Var v) { - checkNotNull(v); - return liveSet.get(v.index); - } - + // There is only a version of this function with index since var.index will + // return the wrong one. Use an instantiation of + // LiveVariablesAnalysisEs6 and getVarIndex(var) to get the right index. public boolean isLive(int index) { return liveSet.get(index); } - public boolean isLive(Var v, int index) { - return liveSet.get(index); - } - @Override public String toString() { return liveSet.toString();