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.HashSet;
  29 import java.util.Set;
  30 
  31 import jdk.internal.vm.compiler.collections.EconomicSet;
  32 import jdk.internal.vm.compiler.collections.Equivalence;
  33 import org.graalvm.compiler.graph.Graph.NodeEvent;
  34 import org.graalvm.compiler.graph.Graph.NodeEventListener;
  35 import org.graalvm.compiler.graph.Node;
  36 import org.graalvm.compiler.graph.Node.IndirectCanonicalization;
  37 
  38 /**
  39  * A simple {@link NodeEventListener} implementation that accumulates event nodes in a
  40  * {@link HashSet}.
  41  */
  42 public class HashSetNodeEventListener 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 HashSetNodeEventListener() {
  51         this.nodes = EconomicSet.create(Equivalence.IDENTITY);
  52         this.filter = EnumSet.allOf(NodeEvent.class);
  53     }
  54 
  55     /**
  56      * Creates a {@link NodeEventListener} that collects nodes from all events that match a given
  57      * filter.
  58      */
  59     public HashSetNodeEventListener(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 HashSetNodeEventListener 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             nodes.add(node);
  76             if (node instanceof IndirectCanonicalization) {
  77                 for (Node usage : node.usages()) {
  78                     nodes.add(usage);
  79                 }
  80             }
  81         }
  82     }
  83 
  84     /**
  85      * Gets the set being used to accumulate the nodes communicated to this listener.
  86      */
  87     public EconomicSet<Node> getNodes() {
  88         return nodes;
  89     }
  90 }