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