1 /* 2 * Copyright (c) 2013, 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 37 /** 38 * A simple {@link NodeEventListener} implementation that accumulates event nodes in a 39 * {@link EconomicSet}. 40 */ 41 public class EconomicSetNodeEventListener extends NodeEventListener { 42 43 private final EconomicSet<Node> nodes; 44 private final Set<NodeEvent> filter; 45 46 /** 47 * Creates a {@link NodeEventListener} that collects nodes from all events. 48 */ 49 public EconomicSetNodeEventListener() { 50 this.nodes = EconomicSet.create(Equivalence.IDENTITY); 51 this.filter = EnumSet.allOf(NodeEvent.class); 52 } 53 54 /** 55 * Creates a {@link NodeEventListener} that collects nodes from all events that match a given 56 * filter. 57 */ 58 public EconomicSetNodeEventListener(Set<NodeEvent> filter) { 59 this.nodes = EconomicSet.create(Equivalence.IDENTITY); 60 this.filter = filter; 61 } 62 63 /** 64 * Excludes a given event from those for which nodes are collected. 65 */ 66 public EconomicSetNodeEventListener exclude(NodeEvent e) { 67 filter.remove(e); 68 return this; 69 } 70 71 @Override 72 public void changed(NodeEvent e, Node node) { 73 if (filter.contains(e)) { 74 nodes.add(node); 75 if (node instanceof IndirectCanonicalization) { 76 for (Node usage : node.usages()) { 77 nodes.add(usage); 78 } 79 } 80 } 81 } 82 83 /** 84 * Gets the set being used to accumulate the nodes communicated to this listener. 85 */ 86 public EconomicSet<Node> getNodes() { 87 return nodes; 88 } 89 }