author | never |
Wed, 27 Jun 2018 17:02:41 -0700 | |
changeset 50858 | 2d3e99a72541 |
parent 49873 | 26ebfe8ce852 |
child 52910 | 583fd71c47d6 |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
46459 | 2 |
* Copyright (c) 2016, 2017, 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.hotspot.phases.aot; |
26 |
||
46459 | 27 |
import static org.graalvm.util.CollectionsUtil.anyMatch; |
43972 | 28 |
|
29 |
import java.util.ArrayList; |
|
46459 | 30 |
import java.util.Iterator; |
31 |
import java.util.List; |
|
43972 | 32 |
|
48861 | 33 |
import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant; |
34 |
import jdk.vm.ci.meta.Constant; |
|
35 |
import jdk.vm.ci.meta.ResolvedJavaType; |
|
36 |
||
49873 | 37 |
import jdk.internal.vm.compiler.collections.EconomicSet; |
43972 | 38 |
import org.graalvm.compiler.graph.Node; |
39 |
import org.graalvm.compiler.hotspot.nodes.aot.InitializeKlassNode; |
|
46566 | 40 |
import org.graalvm.compiler.hotspot.nodes.aot.ResolveConstantNode; |
46459 | 41 |
import org.graalvm.compiler.nodes.AbstractMergeNode; |
42 |
import org.graalvm.compiler.nodes.FixedNode; |
|
43972 | 43 |
import org.graalvm.compiler.nodes.FixedWithNextNode; |
44 |
import org.graalvm.compiler.nodes.Invoke; |
|
45 |
import org.graalvm.compiler.nodes.StructuredGraph; |
|
46 |
import org.graalvm.compiler.phases.BasePhase; |
|
46459 | 47 |
import org.graalvm.compiler.phases.graph.MergeableState; |
48 |
import org.graalvm.compiler.phases.graph.PostOrderNodeIterator; |
|
43972 | 49 |
import org.graalvm.compiler.phases.tiers.PhaseContext; |
44516
6da0313ebe17
8177856: [AOT] EliminateRedundantInitializationPhase is not working
iveresov
parents:
43972
diff
changeset
|
50 |
|
43972 | 51 |
public class EliminateRedundantInitializationPhase extends BasePhase<PhaseContext> { |
52 |
/** |
|
53 |
* Find each {@link Invoke} that has a corresponding {@link InitializeKlassNode}. These |
|
54 |
* {@link InitializeKlassNode} are redundant and are removed. |
|
55 |
* |
|
56 |
*/ |
|
57 |
private static void removeInitsAtStaticCalls(StructuredGraph graph) { |
|
58 |
for (Invoke invoke : graph.getInvokes()) { |
|
59 |
if (invoke.classInit() != null) { |
|
60 |
Node classInit = invoke.classInit(); |
|
61 |
classInit.replaceAtUsages(null); |
|
62 |
graph.removeFixed((FixedWithNextNode) classInit); |
|
63 |
} |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
/** |
|
46566 | 68 |
* Remove redundant {@link InitializeKlassNode} or {@link ResolveConstantNode} instances from |
69 |
* the graph. |
|
46459 | 70 |
* |
71 |
* @param graph the program graph |
|
72 |
*/ |
|
73 |
private static void removeRedundantInits(StructuredGraph graph) { |
|
46566 | 74 |
// Find and remove redundant nodes from the graph. |
75 |
List<FixedWithNextNode> redundantNodes = findRedundantInits(graph); |
|
76 |
for (FixedWithNextNode n : redundantNodes) { |
|
46459 | 77 |
graph.removeFixed(n); |
78 |
} |
|
79 |
} |
|
80 |
||
81 |
/** |
|
46566 | 82 |
* Find {@link InitializeKlassNode} and {@link ResolveConstantNode} instances that can be |
83 |
* removed because there is an existing dominating node. |
|
43972 | 84 |
* |
46459 | 85 |
* @param graph the program graph |
86 |
*/ |
|
46566 | 87 |
private static List<FixedWithNextNode> findRedundantInits(StructuredGraph graph) { |
46459 | 88 |
EliminateRedundantInitializationIterator i = new EliminateRedundantInitializationIterator(graph.start(), new InitializedTypes()); |
89 |
i.apply(); |
|
46566 | 90 |
return i.getRedundantNodes(); |
46459 | 91 |
} |
92 |
||
93 |
/** |
|
94 |
* State for {@link EliminateRedundantInitializationIterator}. |
|
43972 | 95 |
*/ |
46459 | 96 |
private static class InitializedTypes extends MergeableState<InitializedTypes> implements Cloneable { |
97 |
private EconomicSet<ResolvedJavaType> types; |
|
98 |
||
99 |
InitializedTypes() { |
|
100 |
types = EconomicSet.create(); |
|
101 |
} |
|
102 |
||
103 |
private InitializedTypes(EconomicSet<ResolvedJavaType> types) { |
|
104 |
this.types = types; |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public InitializedTypes clone() { |
|
109 |
return new InitializedTypes(EconomicSet.create(types)); |
|
110 |
} |
|
111 |
||
112 |
public boolean contains(ResolvedJavaType type) { |
|
46566 | 113 |
if (type.isInterface() || type.isArray()) { |
46459 | 114 |
// Check for exact match for interfaces |
115 |
return types.contains(type); |
|
116 |
} |
|
117 |
// For other types see if there is the same type or a subtype |
|
118 |
return anyMatch(types, t -> type.isAssignableFrom(t)); |
|
119 |
} |
|
120 |
||
121 |
public void add(ResolvedJavaType type) { |
|
122 |
types.add(type); |
|
123 |
} |
|
124 |
||
125 |
/** |
|
46566 | 126 |
* Merge two given types. Interfaces and arrays have to be the same to merge successfully. |
127 |
* For other types the answer is the LCA. |
|
46459 | 128 |
* |
129 |
* @param a initialized type |
|
130 |
* @param b initialized type |
|
131 |
* @return lowest common type that is initialized if either a or b are initialized, null if |
|
132 |
* no such type exists. |
|
133 |
*/ |
|
134 |
private static ResolvedJavaType merge(ResolvedJavaType a, ResolvedJavaType b) { |
|
46566 | 135 |
// We want exact match for interfaces or arrays |
136 |
if (a.isInterface() || b.isInterface() || a.isArray() || b.isArray()) { |
|
46459 | 137 |
if (a.equals(b)) { |
138 |
return a; |
|
139 |
} else { |
|
140 |
return null; |
|
141 |
} |
|
142 |
} else { |
|
143 |
// And LCA for other types |
|
144 |
ResolvedJavaType c = a.findLeastCommonAncestor(b); |
|
145 |
if (c.isJavaLangObject()) { |
|
146 |
// Not a very useful type, always initialized, don't pollute the sets. |
|
147 |
return null; |
|
148 |
} |
|
149 |
return c; |
|
43972 | 150 |
} |
151 |
} |
|
46459 | 152 |
|
153 |
/** |
|
154 |
* Merge two sets of types. Essentially a computation of the LCA for each element of the |
|
155 |
* cartesian product of the input sets. Interfaces have to match exactly. |
|
156 |
* |
|
157 |
* @param a set of initialized types |
|
158 |
* @param b set of initialized types |
|
159 |
* @return set of common types that would be initialized if types in either a or b are |
|
160 |
* initialized |
|
161 |
*/ |
|
162 |
private static EconomicSet<ResolvedJavaType> merge(EconomicSet<ResolvedJavaType> a, EconomicSet<ResolvedJavaType> b) { |
|
163 |
EconomicSet<ResolvedJavaType> c = EconomicSet.create(); |
|
164 |
for (ResolvedJavaType ta : a) { |
|
165 |
for (ResolvedJavaType tb : b) { |
|
166 |
ResolvedJavaType tc = merge(ta, tb); |
|
167 |
if (tc != null) { |
|
168 |
c.add(tc); |
|
46566 | 169 |
if (tc.isInterface() || tc.isArray()) { |
170 |
// Interfaces and arrays are not going merge with anything else, so bail |
|
171 |
// out early. |
|
46459 | 172 |
break; |
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
return c; |
|
178 |
} |
|
179 |
||
180 |
@Override |
|
181 |
public boolean merge(AbstractMergeNode merge, List<InitializedTypes> withStates) { |
|
182 |
for (InitializedTypes ts : withStates) { |
|
183 |
types = merge(types, ts.types); |
|
184 |
} |
|
185 |
return true; |
|
43972 | 186 |
} |
46459 | 187 |
|
188 |
protected static String toString(EconomicSet<ResolvedJavaType> types) { |
|
189 |
StringBuilder b = new StringBuilder(); |
|
190 |
b.append("["); |
|
191 |
Iterator<ResolvedJavaType> i = types.iterator(); |
|
192 |
while (i.hasNext()) { |
|
193 |
ResolvedJavaType t = i.next(); |
|
194 |
b.append(t.toString()); |
|
195 |
if (i.hasNext()) { |
|
196 |
b.append(","); |
|
197 |
} |
|
198 |
} |
|
199 |
b.append("]"); |
|
200 |
return b.toString(); |
|
201 |
} |
|
202 |
||
203 |
@Override |
|
204 |
public String toString() { |
|
205 |
return toString(types); |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
/** |
|
46566 | 210 |
* Do data flow analysis of class initializations and array resolutions. Collect redundant |
211 |
* nodes. |
|
46459 | 212 |
*/ |
213 |
private static class EliminateRedundantInitializationIterator extends PostOrderNodeIterator<InitializedTypes> { |
|
46566 | 214 |
private List<FixedWithNextNode> redundantNodes = new ArrayList<>(); |
46459 | 215 |
|
46566 | 216 |
public List<FixedWithNextNode> getRedundantNodes() { |
217 |
return redundantNodes; |
|
46459 | 218 |
} |
219 |
||
220 |
EliminateRedundantInitializationIterator(FixedNode start, InitializedTypes initialState) { |
|
221 |
super(start, initialState); |
|
222 |
} |
|
223 |
||
46566 | 224 |
private void processType(FixedWithNextNode node, Constant c) { |
225 |
HotSpotMetaspaceConstant klass = (HotSpotMetaspaceConstant) c; |
|
226 |
ResolvedJavaType t = klass.asResolvedJavaType(); |
|
227 |
if (t != null) { |
|
228 |
if (state.contains(t)) { |
|
229 |
redundantNodes.add(node); |
|
230 |
} else { |
|
231 |
state.add(t); |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
||
46459 | 236 |
@Override |
237 |
protected void node(FixedNode node) { |
|
238 |
if (node instanceof InitializeKlassNode) { |
|
239 |
InitializeKlassNode i = (InitializeKlassNode) node; |
|
46566 | 240 |
if (i.value().isConstant()) { |
241 |
processType(i, i.value().asConstant()); |
|
242 |
} |
|
243 |
} else if (node instanceof ResolveConstantNode) { |
|
244 |
ResolveConstantNode r = (ResolveConstantNode) node; |
|
245 |
if (r.hasNoUsages()) { |
|
246 |
if (r.value().isConstant()) { |
|
247 |
processType(r, r.value().asConstant()); |
|
248 |
} |
|
46459 | 249 |
} |
250 |
} |
|
251 |
} |
|
252 |
||
43972 | 253 |
} |
254 |
||
255 |
@Override |
|
256 |
protected void run(StructuredGraph graph, PhaseContext context) { |
|
257 |
removeInitsAtStaticCalls(graph); |
|
258 |
removeRedundantInits(graph); |
|
259 |
} |
|
260 |
} |