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