Skip to content

Commit

Permalink
Convert DeadAssignmentsElimination pass to use the ES6 Syntactic Scop…
Browse files Browse the repository at this point in the history
…e Creator.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160222117
  • Loading branch information
lillymliu authored and brad4d committed Jun 27, 2017
1 parent 2048279 commit 72d8adb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
19 changes: 8 additions & 11 deletions src/com/google/javascript/jscomp/DeadAssignmentsElimination.java
Expand Up @@ -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;
Expand All @@ -32,15 +32,15 @@

/**
* 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.
*
*/
class DeadAssignmentsElimination extends AbstractScopedCallback implements CompilerPass {

private final AbstractCompiler compiler;
private LiveVariablesAnalysis liveness;
private LiveVariablesAnalysisEs6 liveness;
private final Deque<BailoutInformation> functionStack;

private static final class BailoutInformation {
Expand Down Expand Up @@ -126,12 +126,9 @@ private void eliminateDeadAssignments(NodeTraversal t) {

// Computes liveness information first.
ControlFlowGraph<Node> 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);
}
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 3 additions & 9 deletions src/com/google/javascript/jscomp/LiveVariablesAnalysisEs6.java
Expand Up @@ -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();
Expand Down

0 comments on commit 72d8adb

Please sign in to comment.