Skip to content

Commit

Permalink
[NTI] Use a static method to create the NTIWorkset.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160535478
  • Loading branch information
dimvar authored and brad4d committed Jun 30, 2017
1 parent f1fc500 commit b3d0fb8
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 102 deletions.
208 changes: 107 additions & 101 deletions src/com/google/javascript/jscomp/NTIWorkset.java
Expand Up @@ -23,6 +23,7 @@
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
Expand All @@ -31,43 +32,24 @@


/** /**
* Represents the workset used by the flow-sensitive analysis in NTI. * Represents the workset used by the flow-sensitive analysis in NTI.
* The workset is computed happens iteratively, otherwise large programs can cause stack overflow. * We compute the workset iteratively, otherwise large programs can cause stack overflow.
*/ */
public class NTIWorkset { public class NTIWorkset {
private final ControlFlowGraph<Node> cfg;
// What this class computes. Represents the workset used by the flow-sensitive analysis in NTI. // What this class computes. Represents the workset used by the flow-sensitive analysis in NTI.
private final List<DiGraphNode<Node, ControlFlowGraph.Branch>> ntiWorkset; private List<DiGraphNode<Node, ControlFlowGraph.Branch>> ntiWorkset;
// The algorithm that computes the NTI workset itself uses a workset.
private Deque<DiGraphNode<Node, ControlFlowGraph.Branch>> workset;
// If a node is in this set, don't revisit it.
private Set<DiGraphNode<Node, ControlFlowGraph.Branch>> seen;


NTIWorkset(ControlFlowGraph<Node> cfg) { static NTIWorkset create(ControlFlowGraph<Node> cfg) {
this.cfg = cfg; NTIWorkset result = new NTIWorkset();
this.ntiWorkset = new ArrayList<>(); result.ntiWorkset = Collections.unmodifiableList((new WorksetBuilder(cfg)).build());
this.workset = new ArrayDeque<>(); return result;
this.seen = new LinkedHashSet<>();
buildWorkset();
}

private void buildWorkset() {
Preconditions.checkState(ntiWorkset.isEmpty());
workset.push(cfg.getEntry());
while (!workset.isEmpty()) {
processGraphNode();
}
workset = null;
seen = null;
} }


Iterable<DiGraphNode<Node, ControlFlowGraph.Branch>> forward() { Iterable<DiGraphNode<Node, ControlFlowGraph.Branch>> forward() {
Preconditions.checkState(!ntiWorkset.isEmpty()); Preconditions.checkState(!ntiWorkset.isEmpty());
return ntiWorkset; return ntiWorkset;
} }


/** /** The backwards analysis in NTI traverses the workset in the reverse direction. */
* The backwards analysis in NTI traverses the workset in the reverse direction.
*/
private class BackwardIterator implements Iterator<DiGraphNode<Node, ControlFlowGraph.Branch>> { private class BackwardIterator implements Iterator<DiGraphNode<Node, ControlFlowGraph.Branch>> {
int i = ntiWorkset.size() - 1; int i = ntiWorkset.size() - 1;


Expand Down Expand Up @@ -97,94 +79,118 @@ public Iterator<DiGraphNode<Node, Branch>> iterator() {
}; };
} }


private void processGraphNode() { private static class WorksetBuilder {
DiGraphNode<Node, ControlFlowGraph.Branch> dn = workset.pop(); private final ControlFlowGraph<Node> cfg;
if (seen.contains(dn) || dn == cfg.getImplicitReturn()) { private List<DiGraphNode<Node, ControlFlowGraph.Branch>> ntiWorkset;
return; // The algorithm that computes the NTI workset itself uses a workset.
private Deque<DiGraphNode<Node, ControlFlowGraph.Branch>> workset;
// If a node is in this set, don't revisit it.
private Set<DiGraphNode<Node, ControlFlowGraph.Branch>> seen;

WorksetBuilder(ControlFlowGraph<Node> cfg) {
this.cfg = cfg;
} }
switch (dn.getValue().getToken()) {
case DO: List<DiGraphNode<Node, ControlFlowGraph.Branch>> build() {
case WHILE: ntiWorkset = new ArrayList<>();
case FOR: workset = new ArrayDeque<>();
case FOR_IN: seen = new LinkedHashSet<>();
case FOR_OF: { workset.push(cfg.getEntry());
List<DiGraphEdge<Node, ControlFlowGraph.Branch>> outEdges = dn.getOutEdges(); while (!workset.isEmpty()) {
// The workset is a stack. If we want to analyze nodeA after nodeB, we need to push nodeA processGraphNode();
// before nodeB. For this reason, we push the code after a loop before the loop body. }
for (DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : outEdges) { return ntiWorkset;
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) { }
workset.push(outEdge.getDestination());
} private void processGraphNode() {
} DiGraphNode<Node, ControlFlowGraph.Branch> dn = workset.pop();
for (DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : outEdges) { if (seen.contains(dn) || dn == cfg.getImplicitReturn()) {
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_TRUE) {
workset.push(outEdge.getDestination());
}
}
// The loop condition must be analyzed first, so it's pushed last.
seen.add(dn);
ntiWorkset.add(dn);
return; return;
} }
default: { switch (dn.getValue().getToken()) {
for (DiGraphEdge<Node, ControlFlowGraph.Branch> inEdge : dn.getInEdges()) { case DO:
DiGraphNode<Node, ControlFlowGraph.Branch> source = inEdge.getSource(); case WHILE:
Node sourceNode = source.getValue(); case FOR:
// Wait for all other incoming edges at join nodes. case FOR_IN:
if (!seen.contains(inEdge.getSource()) && !sourceNode.isDo()) { case FOR_OF: {
return; List<DiGraphEdge<Node, ControlFlowGraph.Branch>> outEdges = dn.getOutEdges();
// The workset is a stack. If we want to analyze nodeA after nodeB, we need to push nodeA
// before nodeB. For this reason, we push the code after a loop before the loop body.
for (DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : outEdges) {
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) {
workset.push(outEdge.getDestination());
}
} }
// The loop header has already been added, and will be analyzed before the loop body. for (DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : outEdges) {
// Here, we want to add it again, so that we analyze the header after the loop body, if (outEdge.getValue() == ControlFlowGraph.Branch.ON_TRUE) {
// and before the code following the loop. workset.push(outEdge.getDestination());
if (NodeUtil.isLoopStructure(sourceNode) && !sourceNode.isDo() }
&& inEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) {
ntiWorkset.add(source);
} }
} // The loop condition must be analyzed first, so it's pushed last.
seen.add(dn); seen.add(dn);
if (cfg.getEntry() != dn) {
ntiWorkset.add(dn); ntiWorkset.add(dn);
return;
} }
Node n = dn.getValue(); default: {
List<DiGraphNode<Node, ControlFlowGraph.Branch>> succs = cfg.getDirectedSuccNodes(dn); for (DiGraphEdge<Node, ControlFlowGraph.Branch> inEdge : dn.getInEdges()) {
// Currently, the ELSE branch of an IF is analyzed before the THEN branch. DiGraphNode<Node, ControlFlowGraph.Branch> source = inEdge.getSource();
// To do it the other way around, the ELSE branch has to be pushed to the workset Node sourceNode = source.getValue();
// *before* the THEN branch, so we need to reverse succs. But the order doesn't impact // Wait for all other incoming edges at join nodes.
// correctness, so we don't do the reversal. if (!seen.contains(inEdge.getSource()) && !sourceNode.isDo()) {
for (DiGraphNode<Node, ControlFlowGraph.Branch> succ : succs) { return;
workset.push(succ); }
if (succ == cfg.getImplicitReturn()) { // The loop header has already been added, and will be analyzed before the loop body.
if (n.getNext() != null) { // Here, we want to add it again, so that we analyze the header after the loop body,
processDeadNode(n.getNext()); // and before the code following the loop.
if (NodeUtil.isLoopStructure(sourceNode) && !sourceNode.isDo()
&& inEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) {
ntiWorkset.add(source);
} }
} }
} seen.add(dn);
if (n.isTry()) { if (cfg.getEntry() != dn) {
processDeadNode(n.getSecondChild()); ntiWorkset.add(dn);
} else if (n.isBreak() || n.isContinue() || n.isThrow()) { }
processDeadNode(n.getNext()); Node n = dn.getValue();
List<DiGraphNode<Node, ControlFlowGraph.Branch>> succs = cfg.getDirectedSuccNodes(dn);
// Currently, the ELSE branch of an IF is analyzed before the THEN branch.
// To do it the other way around, the ELSE branch has to be pushed to the workset
// *before* the THEN branch, so we need to reverse succs. But the order doesn't impact
// correctness, so we don't do the reversal.
for (DiGraphNode<Node, ControlFlowGraph.Branch> succ : succs) {
workset.push(succ);
if (succ == cfg.getImplicitReturn()) {
if (n.getNext() != null) {
processDeadNode(n.getNext());
}
}
}
if (n.isTry()) {
processDeadNode(n.getSecondChild());
} else if (n.isBreak() || n.isContinue() || n.isThrow()) {
processDeadNode(n.getNext());
}
} }
} }
} }
}


/** /**
* Analyze dead code, such as a catch that is never executed or a statement following a * Analyze dead code, such as a catch that is never executed or a statement following a
* return/break/continue. This code can be a predecessor of live code in the cfg. We wait * return/break/continue. This code can be a predecessor of live code in the cfg. We wait
* on incoming edges before adding nodes to the workset, and don't want dead code to block * on incoming edges before adding nodes to the workset, and don't want dead code to block
* live code from being analyzed. * live code from being analyzed.
*/ */
private void processDeadNode(Node maybeDeadNode) { private void processDeadNode(Node maybeDeadNode) {
if (maybeDeadNode == null) { if (maybeDeadNode == null) {
return; return;
} }
DiGraphNode<Node, ControlFlowGraph.Branch> cfgNode = cfg.getDirectedGraphNode(maybeDeadNode); DiGraphNode<Node, ControlFlowGraph.Branch> cfgNode = cfg.getDirectedGraphNode(maybeDeadNode);
if (cfgNode == null) { if (cfgNode == null) {
return; return;
} }
if (cfg.getDirectedPredNodes(cfgNode).isEmpty()) { if (cfg.getDirectedPredNodes(cfgNode).isEmpty()) {
workset.push(cfgNode); workset.push(cfgNode);
}
} }
} }
} }
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/NewTypeInference.java
Expand Up @@ -755,7 +755,7 @@ private void analyzeFunction(NTIScope scope) {
println(this.cfg); println(this.cfg);
// The size is > 1 when multiple files are compiled // The size is > 1 when multiple files are compiled
// Preconditions.checkState(cfg.getEntry().getOutEdges().size() == 1); // Preconditions.checkState(cfg.getEntry().getOutEdges().size() == 1);
NTIWorkset workset = new NTIWorkset(this.cfg); NTIWorkset workset = NTIWorkset.create(this.cfg);
/* println("Workset: ", workset); */ /* println("Workset: ", workset); */
this.typeEnvFromDeclaredTypes = getTypeEnvFromDeclaredTypes(); this.typeEnvFromDeclaredTypes = getTypeEnvFromDeclaredTypes();
if (scope.isFunction() && scope.hasUndeclaredFormalsOrOuters()) { if (scope.isFunction() && scope.hasUndeclaredFormalsOrOuters()) {
Expand Down

0 comments on commit b3d0fb8

Please sign in to comment.