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