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