43972
|
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.Iterator;
|
|
26 |
import java.util.List;
|
|
27 |
|
48861
|
28 |
import org.graalvm.collections.EconomicMap;
|
|
29 |
import org.graalvm.collections.Equivalence;
|
47667
|
30 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
31 |
import org.graalvm.compiler.core.common.type.Stamp;
|
46640
|
32 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
33 |
import org.graalvm.compiler.nodes.FieldLocationIdentity;
|
48190
|
34 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
35 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
36 |
import org.graalvm.compiler.nodes.virtual.AllocatedObjectNode;
|
|
37 |
import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode;
|
|
38 |
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
|
46344
|
39 |
import org.graalvm.compiler.options.OptionValues;
|
46551
|
40 |
import org.graalvm.word.LocationIdentity;
|
43972
|
41 |
|
46762
|
42 |
import jdk.vm.ci.meta.JavaKind;
|
|
43 |
|
46344
|
44 |
public final class PEReadEliminationBlockState extends PartialEscapeBlockState<PEReadEliminationBlockState> {
|
43972
|
45 |
|
46344
|
46 |
final EconomicMap<ReadCacheEntry, ValueNode> readCache;
|
43972
|
47 |
|
|
48 |
static final class ReadCacheEntry {
|
|
49 |
|
|
50 |
public final LocationIdentity identity;
|
|
51 |
public final ValueNode object;
|
|
52 |
public final int index;
|
46762
|
53 |
public final JavaKind kind;
|
43972
|
54 |
|
47667
|
55 |
/* This flag does not affect hashCode or equals implementations. */
|
|
56 |
public final boolean overflowAccess;
|
|
57 |
|
|
58 |
ReadCacheEntry(LocationIdentity identity, ValueNode object, int index, JavaKind kind, boolean overflowAccess) {
|
43972
|
59 |
this.identity = identity;
|
|
60 |
this.object = object;
|
|
61 |
this.index = index;
|
46762
|
62 |
this.kind = kind;
|
47667
|
63 |
this.overflowAccess = overflowAccess;
|
43972
|
64 |
}
|
|
65 |
|
|
66 |
@Override
|
|
67 |
public int hashCode() {
|
|
68 |
int result = 31 + ((identity == null) ? 0 : identity.hashCode());
|
46344
|
69 |
result = 31 * result + ((object == null) ? 0 : System.identityHashCode(object));
|
46762
|
70 |
result = 31 * result + kind.ordinal();
|
43972
|
71 |
return result * 31 + index;
|
|
72 |
}
|
|
73 |
|
|
74 |
@Override
|
|
75 |
public boolean equals(Object obj) {
|
|
76 |
if (!(obj instanceof ReadCacheEntry)) {
|
|
77 |
return false;
|
|
78 |
}
|
|
79 |
ReadCacheEntry other = (ReadCacheEntry) obj;
|
46762
|
80 |
return identity.equals(other.identity) && object == other.object && index == other.index && kind == other.kind;
|
43972
|
81 |
}
|
|
82 |
|
|
83 |
@Override
|
|
84 |
public String toString() {
|
46762
|
85 |
return index == -1 ? (object + ":" + kind + "<" + identity + ">") : (object + "[" + index + "]:" + kind + "<" + identity + ">");
|
43972
|
86 |
}
|
|
87 |
}
|
|
88 |
|
46640
|
89 |
public PEReadEliminationBlockState(OptionValues options, DebugContext debug) {
|
|
90 |
super(options, debug);
|
46344
|
91 |
readCache = EconomicMap.create(Equivalence.DEFAULT);
|
43972
|
92 |
}
|
|
93 |
|
|
94 |
public PEReadEliminationBlockState(PEReadEliminationBlockState other) {
|
|
95 |
super(other);
|
46344
|
96 |
readCache = EconomicMap.create(Equivalence.DEFAULT, other.readCache);
|
43972
|
97 |
}
|
|
98 |
|
|
99 |
@Override
|
|
100 |
public String toString() {
|
|
101 |
return super.toString() + " " + readCache;
|
|
102 |
}
|
|
103 |
|
47667
|
104 |
private static JavaKind stampToJavaKind(Stamp stamp) {
|
|
105 |
if (stamp instanceof IntegerStamp) {
|
|
106 |
switch (((IntegerStamp) stamp).getBits()) {
|
|
107 |
case 1:
|
|
108 |
return JavaKind.Boolean;
|
|
109 |
case 8:
|
|
110 |
return JavaKind.Byte;
|
|
111 |
case 16:
|
|
112 |
return ((IntegerStamp) stamp).isPositive() ? JavaKind.Char : JavaKind.Short;
|
|
113 |
case 32:
|
|
114 |
return JavaKind.Int;
|
|
115 |
case 64:
|
|
116 |
return JavaKind.Long;
|
|
117 |
default:
|
|
118 |
throw new IllegalArgumentException("unexpected IntegerStamp " + stamp);
|
|
119 |
}
|
|
120 |
} else {
|
|
121 |
return stamp.getStackKind();
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
43972
|
125 |
@Override
|
|
126 |
protected void objectMaterialized(VirtualObjectNode virtual, AllocatedObjectNode representation, List<ValueNode> values) {
|
|
127 |
if (virtual instanceof VirtualInstanceNode) {
|
|
128 |
VirtualInstanceNode instance = (VirtualInstanceNode) virtual;
|
|
129 |
for (int i = 0; i < instance.entryCount(); i++) {
|
47667
|
130 |
JavaKind declaredKind = instance.field(i).getJavaKind();
|
48190
|
131 |
if (declaredKind == stampToJavaKind(values.get(i).stamp(NodeView.DEFAULT))) {
|
47667
|
132 |
// We won't cache unaligned field writes upon instantiation unless we add
|
|
133 |
// support for non-array objects in PEReadEliminationClosure.processUnsafeLoad.
|
|
134 |
readCache.put(new ReadCacheEntry(new FieldLocationIdentity(instance.field(i)), representation, -1, declaredKind, false), values.get(i));
|
|
135 |
}
|
43972
|
136 |
}
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
@Override
|
|
141 |
public boolean equivalentTo(PEReadEliminationBlockState other) {
|
46344
|
142 |
if (!isSubMapOf(readCache, other.readCache)) {
|
43972
|
143 |
return false;
|
|
144 |
}
|
|
145 |
return super.equivalentTo(other);
|
|
146 |
}
|
|
147 |
|
47667
|
148 |
public void addReadCache(ValueNode object, LocationIdentity identity, int index, JavaKind kind, boolean overflowAccess, ValueNode value, PartialEscapeClosure<?> closure) {
|
43972
|
149 |
ValueNode cacheObject;
|
|
150 |
ObjectState obj = closure.getObjectState(this, object);
|
|
151 |
if (obj != null) {
|
|
152 |
assert !obj.isVirtual();
|
|
153 |
cacheObject = obj.getMaterializedValue();
|
|
154 |
} else {
|
|
155 |
cacheObject = object;
|
|
156 |
}
|
47667
|
157 |
readCache.put(new ReadCacheEntry(identity, cacheObject, index, kind, overflowAccess), value);
|
43972
|
158 |
}
|
|
159 |
|
46762
|
160 |
public ValueNode getReadCache(ValueNode object, LocationIdentity identity, int index, JavaKind kind, PartialEscapeClosure<?> closure) {
|
43972
|
161 |
ValueNode cacheObject;
|
|
162 |
ObjectState obj = closure.getObjectState(this, object);
|
|
163 |
if (obj != null) {
|
|
164 |
assert !obj.isVirtual() : object;
|
|
165 |
cacheObject = obj.getMaterializedValue();
|
|
166 |
} else {
|
|
167 |
cacheObject = object;
|
|
168 |
}
|
47667
|
169 |
ValueNode cacheValue = readCache.get(new ReadCacheEntry(identity, cacheObject, index, kind, false));
|
43972
|
170 |
obj = closure.getObjectState(this, cacheValue);
|
|
171 |
if (obj != null) {
|
|
172 |
assert !obj.isVirtual();
|
|
173 |
cacheValue = obj.getMaterializedValue();
|
|
174 |
} else {
|
|
175 |
// assert !scalarAliases.containsKey(cacheValue);
|
|
176 |
cacheValue = closure.getScalarAlias(cacheValue);
|
|
177 |
}
|
|
178 |
return cacheValue;
|
|
179 |
}
|
|
180 |
|
|
181 |
public void killReadCache() {
|
|
182 |
readCache.clear();
|
|
183 |
}
|
|
184 |
|
|
185 |
public void killReadCache(LocationIdentity identity, int index) {
|
46344
|
186 |
Iterator<ReadCacheEntry> iter = readCache.getKeys().iterator();
|
43972
|
187 |
while (iter.hasNext()) {
|
46344
|
188 |
ReadCacheEntry entry = iter.next();
|
47667
|
189 |
if (entry.identity.equals(identity) && (index == -1 || entry.index == -1 || index == entry.index || entry.overflowAccess)) {
|
43972
|
190 |
iter.remove();
|
|
191 |
}
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
46344
|
195 |
public EconomicMap<ReadCacheEntry, ValueNode> getReadCache() {
|
43972
|
196 |
return readCache;
|
|
197 |
}
|
|
198 |
}
|