1 /*
   2  * Copyright (c) 2011, 2012, 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.virtual.phases.ea;
  24 
  25 import java.util.Iterator;
  26 import java.util.Map;
  27 
  28 public abstract class EffectsBlockState<T extends EffectsBlockState<T>> {
  29 
  30     /*
  31      * This flag specifies whether this path that leads to this block is unreachable.
  32      */
  33     private boolean dead;
  34 
  35     public EffectsBlockState() {
  36         // emtpy
  37     }
  38 
  39     public EffectsBlockState(EffectsBlockState<T> other) {
  40         this.dead = other.dead;
  41     }
  42 
  43     @Override
  44     public String toString() {
  45         return "";
  46     }
  47 
  48     protected abstract boolean equivalentTo(T other);
  49 
  50     public boolean isDead() {
  51         return dead;
  52     }
  53 
  54     public void markAsDead() {
  55         this.dead = true;
  56     }
  57 
  58     protected static <K, V> boolean compareMaps(Map<K, V> left, Map<K, V> right) {
  59         if (left.size() != right.size()) {
  60             return false;
  61         }
  62         return compareMapsNoSize(left, right);
  63     }
  64 
  65     protected static <K, V> boolean compareMapsNoSize(Map<K, V> left, Map<K, V> right) {
  66         if (left == right) {
  67             return true;
  68         }
  69         for (Map.Entry<K, V> entry : right.entrySet()) {
  70             K key = entry.getKey();
  71             V value = entry.getValue();
  72             assert value != null;
  73             V otherValue = left.get(key);
  74             if (otherValue != value && !value.equals(otherValue)) {
  75                 return false;
  76             }
  77         }
  78         return true;
  79     }
  80 
  81     protected static <U, V> void meetMaps(Map<U, V> target, Map<U, V> source) {
  82         Iterator<Map.Entry<U, V>> iter = target.entrySet().iterator();
  83         while (iter.hasNext()) {
  84             Map.Entry<U, V> entry = iter.next();
  85             if (source.containsKey(entry.getKey())) {
  86                 assert source.get(entry.getKey()) == entry.getValue();
  87             } else {
  88                 iter.remove();
  89             }
  90         }
  91     }
  92 
  93 }