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