src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.phases.common/src/org/graalvm/compiler/phases/common/util/EconomicSetNodeEventListener.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52578
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
     2
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
52578
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     4
 *
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     8
 *
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    13
 * accompanied this code).
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    14
 *
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    18
 *
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    21
 * questions.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    22
 */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    23
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    24
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    25
package org.graalvm.compiler.phases.common.util;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    26
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    27
import java.util.EnumSet;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    28
import java.util.Set;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    29
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    30
import jdk.internal.vm.compiler.collections.EconomicSet;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    31
import jdk.internal.vm.compiler.collections.Equivalence;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    32
import org.graalvm.compiler.graph.Graph.NodeEvent;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    33
import org.graalvm.compiler.graph.Graph.NodeEventListener;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    34
import org.graalvm.compiler.graph.Node;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.graph.Node.IndirectCanonicalization;
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    36
import org.graalvm.compiler.nodes.AbstractBeginNode;
52578
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    37
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    38
/**
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    39
 * A simple {@link NodeEventListener} implementation that accumulates event nodes in a
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    40
 * {@link EconomicSet}.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    41
 */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    42
public class EconomicSetNodeEventListener extends NodeEventListener {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    43
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    44
    private final EconomicSet<Node> nodes;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    45
    private final Set<NodeEvent> filter;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    46
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    47
    /**
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    48
     * Creates a {@link NodeEventListener} that collects nodes from all events.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    49
     */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    50
    public EconomicSetNodeEventListener() {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    51
        this.nodes = EconomicSet.create(Equivalence.IDENTITY);
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    52
        this.filter = EnumSet.of(NodeEvent.INPUT_CHANGED, NodeEvent.NODE_ADDED, NodeEvent.ZERO_USAGES);
52578
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    53
    }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    54
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    55
    /**
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    56
     * Creates a {@link NodeEventListener} that collects nodes from all events that match a given
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    57
     * filter.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    58
     */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    59
    public EconomicSetNodeEventListener(Set<NodeEvent> filter) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    60
        this.nodes = EconomicSet.create(Equivalence.IDENTITY);
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    61
        this.filter = filter;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    62
    }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    63
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    64
    /**
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    65
     * Excludes a given event from those for which nodes are collected.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    66
     */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    67
    public EconomicSetNodeEventListener exclude(NodeEvent e) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    68
        filter.remove(e);
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    69
        return this;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    70
    }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    71
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    72
    @Override
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    73
    public void changed(NodeEvent e, Node node) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    74
        if (filter.contains(e)) {
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    75
            add(node);
52578
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    76
            if (node instanceof IndirectCanonicalization) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    77
                for (Node usage : node.usages()) {
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    78
                    add(usage);
52578
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    79
                }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    80
            }
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    81
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    82
            if (node instanceof AbstractBeginNode) {
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    83
                AbstractBeginNode abstractBeginNode = (AbstractBeginNode) node;
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    84
                add(abstractBeginNode.predecessor());
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    85
            }
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    86
        }
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    87
    }
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    88
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    89
    private void add(Node n) {
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    90
        if (n != null) {
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 52910
diff changeset
    91
            nodes.add(n);
52578
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    92
        }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    93
    }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    94
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    95
    /**
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    96
     * Gets the set being used to accumulate the nodes communicated to this listener.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    97
     */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    98
    public EconomicSet<Node> getNodes() {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    99
        return nodes;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
   100
    }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
   101
}