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 package org.graalvm.compiler.phases.common.util; 24 25 import java.util.EnumSet; 26 import java.util.HashSet; 27 import java.util.Set; 28 29 import org.graalvm.compiler.graph.Graph.NodeEvent; 30 import org.graalvm.compiler.graph.Graph.NodeEventListener; 31 import org.graalvm.compiler.graph.Node; 32 import org.graalvm.compiler.graph.Node.IndirectCanonicalization; 33 34 /** 35 * A simple {@link NodeEventListener} implementation that accumulates event nodes in a 36 * {@link HashSet}. 37 */ 38 public class HashSetNodeEventListener implements NodeEventListener { 39 40 private final Set<Node> nodes; 41 private final Set<NodeEvent> filter; 42 43 /** 44 * Creates a {@link NodeEventListener} that collects nodes from all events. 45 */ 46 public HashSetNodeEventListener() { 47 this.nodes = Node.newSet(); 48 this.filter = EnumSet.allOf(NodeEvent.class); 49 } 50 51 /** 52 * Creates a {@link NodeEventListener} that collects nodes from all events that match a given 53 * filter. 54 */ 55 public HashSetNodeEventListener(Set<NodeEvent> filter) { 56 this.nodes = Node.newSet(); 57 this.filter = filter; 58 } 59 60 /** 61 * Excludes a given event from those for which nodes are collected. 62 */ 63 public HashSetNodeEventListener exclude(NodeEvent e) { 64 filter.remove(e); 65 return this; 66 } 67 68 @Override 69 public void event(NodeEvent e, Node node) { 70 if (filter.contains(e)) { 71 nodes.add(node); 72 if (node instanceof IndirectCanonicalization) { 73 for (Node usage : node.usages()) { 74 nodes.add(usage); 75 } 76 } 77 } 78 } 79 80 /** 81 * Gets the set being used to accumulate the nodes communicated to this listener. 82 */ 83 public Set<Node> getNodes() { 84 return nodes; 85 } 86 }