1/* 2 * Copyright (c) 2011, 2017, 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 */ 23package org.graalvm.compiler.virtual.phases.ea; 24 25import java.util.Iterator; 26import java.util.Map; 27 28import org.graalvm.util.EconomicMap; 29import org.graalvm.util.UnmodifiableMapCursor; 30 31public abstract class EffectsBlockState<T extends EffectsBlockState<T>> { 32 33 /** 34 * This flag specifies whether this block is unreachable, which can happen during analysis if 35 * conditions turn constant or nodes canonicalize to cfg sinks. 36 */ 37 private boolean dead; 38 39 public EffectsBlockState() { 40 // emtpy 41 } 42 43 public EffectsBlockState(EffectsBlockState<T> other) { 44 this.dead = other.dead; 45 } 46 47 @Override 48 public String toString() { 49 return ""; 50 } 51 52 protected abstract boolean equivalentTo(T other); 53 54 public boolean isDead() { 55 return dead; 56 } 57 58 public void markAsDead() { 59 this.dead = true; 60 } 61 62 /** 63 * Returns true if every value in subMap is also present in the superMap (according to "equals" 64 * semantics). 65 */ 66 protected static <K, V> boolean isSubMapOf(EconomicMap<K, V> superMap, EconomicMap<K, V> subMap) { 67 if (superMap == subMap) { 68 return true; 69 } 70 UnmodifiableMapCursor<K, V> cursor = subMap.getEntries(); 71 while (cursor.advance()) { 72 K key = cursor.getKey(); 73 V value = cursor.getValue(); 74 assert value != null; 75 V otherValue = superMap.get(key); 76 if (otherValue != value && !value.equals(otherValue)) { 77 return false; 78 } 79 } 80 return true; 81 } 82 83 /** 84 * Modifies target so that only entries that have corresponding entries in source remain. 85 */ 86 protected static <U, V> void meetMaps(Map<U, V> target, Map<U, V> source) { 87 Iterator<Map.Entry<U, V>> iter = target.entrySet().iterator(); 88 while (iter.hasNext()) { 89 Map.Entry<U, V> entry = iter.next(); 90 if (source.containsKey(entry.getKey())) { 91 assert source.get(entry.getKey()) == entry.getValue(); 92 } else { 93 iter.remove(); 94 } 95 } 96 } 97} 98