Search in sources :

Example 1 with LiveVariableLattice

use of com.google.javascript.jscomp.LiveVariablesAnalysis.LiveVariableLattice in project closure-compiler by google.

the class CoalesceVariableNames method computeVariableNamesInterferenceGraph.

/**
 * In order to determine when it is appropriate to coalesce two variables, we use a live variables
 * analysis to make sure they are not alive at the same time. We take every pairing of variables
 * and for every CFG node, determine whether the two variables are alive at the same time. If two
 * variables are alive at the same time, we create an edge between them in the interference graph.
 * The interference graph is the input to a graph coloring algorithm that ensures any interfering
 * variables are marked in different color groups, while variables that can safely be coalesced
 * are assigned the same color group.
 *
 * @param cfg
 * @param escaped we don't want to coalesce any escaped variables
 * @return graph with variable nodes and edges representing variable interference
 */
private UndiGraph<Var, Void> computeVariableNamesInterferenceGraph(ControlFlowGraph<Node> cfg, Set<? extends Var> escaped) {
    UndiGraph<Var, Void> interferenceGraph = LinkedUndirectedGraph.create();
    // First create a node for each non-escaped variable. We add these nodes in the order in which
    // they appear in the code because we want the names that appear earlier in the code to be used
    // when coalescing to variables that appear later in the code.
    List<Var> orderedVariables = liveness.getAllVariablesInOrder();
    for (Var v : orderedVariables) {
        if (escaped.contains(v)) {
            continue;
        }
        // around with it.
        if (v.getParentNode().isFunction()) {
            continue;
        }
        // incorrect semantics. See test case "testCapture".
        if (v.isLet() || v.isConst()) {
            Node nameDecl = NodeUtil.getEnclosingNode(v.getNode(), NodeUtil::isNameDeclaration);
            if (NodeUtil.findLhsNodesInNode(nameDecl).size() > 1) {
                continue;
            }
        }
        interferenceGraph.createNode(v);
    }
    // Go through each variable and try to connect them.
    int v1Index = -1;
    for (Var v1 : orderedVariables) {
        v1Index++;
        int v2Index = -1;
        NEXT_VAR_PAIR: for (Var v2 : orderedVariables) {
            v2Index++;
            // Skip duplicate pairs.
            if (v1Index > v2Index) {
                continue;
            }
            if (!interferenceGraph.hasNode(v1) || !interferenceGraph.hasNode(v2)) {
                // locals. Also avoid merging a variable with itself.
                continue NEXT_VAR_PAIR;
            }
            if (v1.isParam() && v2.isParam()) {
                interferenceGraph.connectIfNotFound(v1, null, v2);
                continue NEXT_VAR_PAIR;
            }
            // time, add an edge between them and continue to the next pair.
            NEXT_CROSS_CFG_NODE: for (DiGraphNode<Node, Branch> cfgNode : cfg.getDirectedGraphNodes()) {
                if (cfg.isImplicitReturn(cfgNode)) {
                    continue NEXT_CROSS_CFG_NODE;
                }
                FlowState<LiveVariableLattice> state = cfgNode.getAnnotation();
                if ((state.getIn().isLive(v1Index) && state.getIn().isLive(v2Index)) || (state.getOut().isLive(v1Index) && state.getOut().isLive(v2Index))) {
                    interferenceGraph.connectIfNotFound(v1, null, v2);
                    continue NEXT_VAR_PAIR;
                }
            }
            // if there's a collision *within* the cfg node.
            NEXT_INTRA_CFG_NODE: for (DiGraphNode<Node, Branch> cfgNode : cfg.getDirectedGraphNodes()) {
                if (cfg.isImplicitReturn(cfgNode)) {
                    continue NEXT_INTRA_CFG_NODE;
                }
                FlowState<LiveVariableLattice> state = cfgNode.getAnnotation();
                boolean v1OutLive = state.getOut().isLive(v1Index);
                boolean v2OutLive = state.getOut().isLive(v2Index);
                CombinedLiveRangeChecker checker = new CombinedLiveRangeChecker(cfgNode.getValue(), new LiveRangeChecker(v1, v2OutLive ? null : v2), new LiveRangeChecker(v2, v1OutLive ? null : v1));
                checker.check(cfgNode.getValue());
                if (checker.connectIfCrossed(interferenceGraph)) {
                    continue NEXT_VAR_PAIR;
                }
            }
        }
    }
    return interferenceGraph;
}
Also used : LiveVariableLattice(com.google.javascript.jscomp.LiveVariablesAnalysis.LiveVariableLattice) GraphNode(com.google.javascript.jscomp.graph.GraphNode) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) Branch(com.google.javascript.jscomp.ControlFlowGraph.Branch)

Example 2 with LiveVariableLattice

use of com.google.javascript.jscomp.LiveVariablesAnalysis.LiveVariableLattice in project closure-compiler by google.

the class DeadAssignmentsElimination method tryRemoveDeadAssignments.

/**
 * Try to remove useless assignments from a control flow graph that has been
 * annotated with liveness information.
 *
 * @param t The node traversal.
 * @param cfg The control flow graph of the program annotated with liveness
 *        information.
 */
private void tryRemoveDeadAssignments(NodeTraversal t, ControlFlowGraph<Node> cfg, Map<String, Var> allVarsInFn) {
    Iterable<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes();
    for (DiGraphNode<Node, Branch> cfgNode : nodes) {
        FlowState<LiveVariableLattice> state = cfgNode.getAnnotation();
        Node n = cfgNode.getValue();
        if (n == null) {
            continue;
        }
        switch(n.getToken()) {
            case IF:
            case WHILE:
            case DO:
                tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state, allVarsInFn);
                continue;
            case FOR:
            case FOR_IN:
            case FOR_OF:
                if (n.isVanillaFor()) {
                    tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state, allVarsInFn);
                }
                continue;
            case SWITCH:
            case CASE:
            case RETURN:
                if (n.hasChildren()) {
                    tryRemoveAssignment(t, n.getFirstChild(), state, allVarsInFn);
                }
                continue;
            // TODO(user): case VAR: Remove var a=1;a=2;.....
            default:
                break;
        }
        tryRemoveAssignment(t, n, state, allVarsInFn);
    }
}
Also used : DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) LiveVariableLattice(com.google.javascript.jscomp.LiveVariablesAnalysis.LiveVariableLattice) Branch(com.google.javascript.jscomp.ControlFlowGraph.Branch) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) Node(com.google.javascript.rhino.Node)

Aggregations

Branch (com.google.javascript.jscomp.ControlFlowGraph.Branch)2 LiveVariableLattice (com.google.javascript.jscomp.LiveVariablesAnalysis.LiveVariableLattice)2 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)2 Node (com.google.javascript.rhino.Node)2 GraphNode (com.google.javascript.jscomp.graph.GraphNode)1