src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.phases.common/src/org/graalvm/compiler/phases/common/util/EconomicSetNodeEventListener.java
author dlong
Thu, 15 Nov 2018 09:04:07 -0800
changeset 52578 7dd81e82d083
child 52910 583fd71c47d6
permissions -rw-r--r--
8210777: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52578
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     1
/*
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
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;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    36
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    37
/**
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    38
 * A simple {@link NodeEventListener} implementation that accumulates event nodes in a
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    39
 * {@link EconomicSet}.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    40
 */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    41
public class EconomicSetNodeEventListener extends NodeEventListener {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    42
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    43
    private final EconomicSet<Node> nodes;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    44
    private final Set<NodeEvent> filter;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    45
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    46
    /**
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    47
     * Creates a {@link NodeEventListener} that collects nodes from all events.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    48
     */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    49
    public EconomicSetNodeEventListener() {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    50
        this.nodes = EconomicSet.create(Equivalence.IDENTITY);
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    51
        this.filter = EnumSet.allOf(NodeEvent.class);
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    52
    }
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
     * Creates a {@link NodeEventListener} that collects nodes from all events that match a given
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    56
     * filter.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    57
     */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    58
    public EconomicSetNodeEventListener(Set<NodeEvent> filter) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    59
        this.nodes = EconomicSet.create(Equivalence.IDENTITY);
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    60
        this.filter = filter;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    61
    }
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
     * Excludes a given event from those for which nodes are collected.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    65
     */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    66
    public EconomicSetNodeEventListener exclude(NodeEvent e) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    67
        filter.remove(e);
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    68
        return this;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    69
    }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    70
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    71
    @Override
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    72
    public void changed(NodeEvent e, Node node) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    73
        if (filter.contains(e)) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    74
            nodes.add(node);
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    75
            if (node instanceof IndirectCanonicalization) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    76
                for (Node usage : node.usages()) {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    77
                    nodes.add(usage);
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    78
                }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    79
            }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    80
        }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    81
    }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    82
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    83
    /**
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    84
     * Gets the set being used to accumulate the nodes communicated to this listener.
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    85
     */
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    86
    public EconomicSet<Node> getNodes() {
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    87
        return nodes;
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    88
    }
7dd81e82d083 8210777: Update Graal
dlong
parents:
diff changeset
    89
}