1 /* 2 * Copyright (c) 2011, 2016, 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.HashMap; 26 import java.util.Map; 27 28 import org.graalvm.compiler.core.common.CollectionsFactory; 29 import org.graalvm.compiler.core.common.LocationIdentity; 30 import org.graalvm.compiler.nodes.ValueNode; 31 32 public class ReadEliminationBlockState extends EffectsBlockState<ReadEliminationBlockState> { 33 34 final HashMap<CacheEntry<?>, ValueNode> readCache; 35 36 abstract static class CacheEntry<T> { 37 38 public final ValueNode object; 39 public final T identity; 40 41 CacheEntry(ValueNode object, T identity) { 42 this.object = object; 43 this.identity = identity; 44 } 45 46 public abstract CacheEntry<T> duplicateWithObject(ValueNode newObject); 47 48 @Override 49 public int hashCode() { 50 int result = 31 + ((identity == null) ? 0 : identity.hashCode()); 51 return 31 * result + ((object == null) ? 0 : object.hashCode()); 52 } 53 54 @Override 55 public boolean equals(Object obj) { 56 if (!(obj instanceof CacheEntry<?>)) { 57 return false; 58 } 59 CacheEntry<?> other = (CacheEntry<?>) obj; 60 return identity.equals(other.identity) && object == other.object; 61 } 62 63 @Override 64 public String toString() { 65 return object + ":" + identity; 66 } 67 68 public abstract boolean conflicts(LocationIdentity other); 69 70 public abstract LocationIdentity getIdentity(); 71 } 72 73 static class LoadCacheEntry extends CacheEntry<LocationIdentity> { 74 75 LoadCacheEntry(ValueNode object, LocationIdentity identity) { 76 super(object, identity); 77 } 78 79 @Override 80 public CacheEntry<LocationIdentity> duplicateWithObject(ValueNode newObject) { 81 return new LoadCacheEntry(newObject, identity); 82 } 83 84 @Override 85 public boolean conflicts(LocationIdentity other) { 86 return identity.equals(other); 87 } 88 89 @Override 90 public LocationIdentity getIdentity() { 91 return identity; 92 } 93 } 94 95 /** 96 * CacheEntry describing an Unsafe memory reference. The memory location and the location 97 * identity are separate so both must be considered when looking for optimizable memory 98 * accesses. 99 */ 100 static class UnsafeLoadCacheEntry extends CacheEntry<ValueNode> { 101 102 private final LocationIdentity locationIdentity; 103 104 UnsafeLoadCacheEntry(ValueNode object, ValueNode location, LocationIdentity locationIdentity) { 105 super(object, location); 106 assert locationIdentity != null; 107 this.locationIdentity = locationIdentity; 108 } 109 110 @Override 111 public CacheEntry<ValueNode> duplicateWithObject(ValueNode newObject) { 112 return new UnsafeLoadCacheEntry(newObject, identity, locationIdentity); 113 } 114 115 @Override 116 public boolean conflicts(LocationIdentity other) { 117 return locationIdentity.equals(other); 118 } 119 120 @Override 121 public int hashCode() { 122 return 31 * super.hashCode() + locationIdentity.hashCode(); 123 } 124 125 @Override 126 public boolean equals(Object obj) { 127 if (obj instanceof UnsafeLoadCacheEntry) { 128 UnsafeLoadCacheEntry other = (UnsafeLoadCacheEntry) obj; 129 return super.equals(other) && locationIdentity.equals(other.locationIdentity); 130 } 131 return false; 132 } 133 134 @Override 135 public LocationIdentity getIdentity() { 136 return locationIdentity; 137 } 138 139 @Override 140 public String toString() { 141 return "UNSAFE:" + super.toString() + " location:" + locationIdentity; 142 } 143 } 144 145 public ReadEliminationBlockState() { 146 readCache = CollectionsFactory.newMap(); 147 } 148 149 public ReadEliminationBlockState(ReadEliminationBlockState other) { 150 readCache = CollectionsFactory.newMap(other.readCache); 151 } 152 153 @Override 154 public String toString() { 155 return super.toString() + " " + readCache; 156 } 157 158 @Override 159 public boolean equivalentTo(ReadEliminationBlockState other) { 160 return compareMapsNoSize(readCache, other.readCache); 161 } 162 163 public void addCacheEntry(CacheEntry<?> identifier, ValueNode value) { 164 readCache.put(identifier, value); 165 } 166 167 public ValueNode getCacheEntry(CacheEntry<?> identifier) { 168 return readCache.get(identifier); 169 } 170 171 public void killReadCache() { 172 readCache.clear(); 173 } 174 175 public void killReadCache(LocationIdentity identity) { 176 readCache.entrySet().removeIf(entry -> entry.getKey().conflicts(identity)); 177 } 178 179 public Map<CacheEntry<?>, ValueNode> getReadCache() { 180 return readCache; 181 } 182 }