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