43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2009, 2015, 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.nodes.extended;
|
|
24 |
|
46393
|
25 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
|
|
26 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_64;
|
|
27 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_8;
|
43972
|
28 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN;
|
46393
|
29 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
|
|
30 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_64;
|
|
31 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
|
43972
|
32 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_UNKNOWN;
|
|
33 |
|
|
34 |
import java.util.Arrays;
|
|
35 |
|
|
36 |
import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
|
46344
|
37 |
import org.graalvm.compiler.core.common.type.Stamp;
|
43972
|
38 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
39 |
import org.graalvm.compiler.debug.GraalError;
|
|
40 |
import org.graalvm.compiler.graph.Node;
|
|
41 |
import org.graalvm.compiler.graph.NodeClass;
|
|
42 |
import org.graalvm.compiler.graph.NodeSuccessorList;
|
|
43 |
import org.graalvm.compiler.graph.spi.SimplifierTool;
|
46393
|
44 |
import org.graalvm.compiler.nodeinfo.NodeCycles;
|
43972
|
45 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
46393
|
46 |
import org.graalvm.compiler.nodeinfo.NodeSize;
|
43972
|
47 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
48 |
import org.graalvm.compiler.nodes.ControlSplitNode;
|
48190
|
49 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
50 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
51 |
|
|
52 |
import jdk.vm.ci.meta.Constant;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* The {@code SwitchNode} class is the base of both lookup and table switches.
|
|
56 |
*/
|
|
57 |
// @formatter:off
|
|
58 |
@NodeInfo(cycles = CYCLES_UNKNOWN,
|
|
59 |
cyclesRationale = "We cannot estimate the runtime cost of a switch statement without knowing the number" +
|
|
60 |
"of case statements and the involved keys.",
|
|
61 |
size = SIZE_UNKNOWN,
|
|
62 |
sizeRationale = "We cannot estimate the code size of a switch statement without knowing the number" +
|
|
63 |
"of case statements.")
|
|
64 |
// @formatter:on
|
|
65 |
public abstract class SwitchNode extends ControlSplitNode {
|
|
66 |
|
|
67 |
public static final NodeClass<SwitchNode> TYPE = NodeClass.create(SwitchNode.class);
|
|
68 |
@Successor protected NodeSuccessorList<AbstractBeginNode> successors;
|
|
69 |
@Input protected ValueNode value;
|
|
70 |
|
|
71 |
// do not change the contents of these arrays:
|
|
72 |
protected final double[] keyProbabilities;
|
|
73 |
protected final int[] keySuccessors;
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Constructs a new Switch.
|
|
77 |
*
|
|
78 |
* @param value the instruction that provides the value to be switched over
|
|
79 |
* @param successors the list of successors of this switch
|
|
80 |
*/
|
|
81 |
protected SwitchNode(NodeClass<? extends SwitchNode> c, ValueNode value, AbstractBeginNode[] successors, int[] keySuccessors, double[] keyProbabilities) {
|
|
82 |
super(c, StampFactory.forVoid());
|
48190
|
83 |
assert value.stamp(NodeView.DEFAULT).getStackKind().isNumericInteger() || value.stamp(NodeView.DEFAULT) instanceof AbstractPointerStamp : value.stamp(NodeView.DEFAULT) +
|
|
84 |
" key not supported by SwitchNode";
|
43972
|
85 |
assert keySuccessors.length == keyProbabilities.length;
|
|
86 |
this.successors = new NodeSuccessorList<>(this, successors);
|
|
87 |
this.value = value;
|
|
88 |
this.keySuccessors = keySuccessors;
|
|
89 |
this.keyProbabilities = keyProbabilities;
|
|
90 |
assert assertProbabilities();
|
|
91 |
}
|
|
92 |
|
|
93 |
private boolean assertProbabilities() {
|
|
94 |
double total = 0;
|
|
95 |
for (double d : keyProbabilities) {
|
|
96 |
total += d;
|
|
97 |
assert d >= 0.0 : "Cannot have negative probabilities in switch node: " + d;
|
|
98 |
}
|
|
99 |
assert total > 0.999 && total < 1.001 : "Total " + total;
|
|
100 |
return true;
|
|
101 |
}
|
|
102 |
|
|
103 |
@Override
|
46344
|
104 |
public int getSuccessorCount() {
|
|
105 |
return successors.count();
|
|
106 |
}
|
|
107 |
|
|
108 |
@Override
|
43972
|
109 |
public double probability(AbstractBeginNode successor) {
|
|
110 |
double sum = 0;
|
|
111 |
for (int i = 0; i < keySuccessors.length; i++) {
|
|
112 |
if (successors.get(keySuccessors[i]) == successor) {
|
|
113 |
sum += keyProbabilities[i];
|
|
114 |
}
|
|
115 |
}
|
|
116 |
return sum;
|
|
117 |
}
|
|
118 |
|
46344
|
119 |
@Override
|
|
120 |
public boolean setProbability(AbstractBeginNode successor, double value) {
|
|
121 |
assert value <= 1.0 && value >= 0.0 : value;
|
|
122 |
assert assertProbabilities();
|
|
123 |
|
|
124 |
double sum = 0;
|
|
125 |
double otherSum = 0;
|
|
126 |
for (int i = 0; i < keySuccessors.length; i++) {
|
|
127 |
if (successors.get(keySuccessors[i]) == successor) {
|
|
128 |
sum += keyProbabilities[i];
|
|
129 |
} else {
|
|
130 |
otherSum += keyProbabilities[i];
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
if (otherSum == 0 || sum == 0) {
|
|
135 |
// Cannot correctly adjust probabilities.
|
|
136 |
return false;
|
|
137 |
}
|
|
138 |
|
|
139 |
double delta = value - sum;
|
|
140 |
|
|
141 |
for (int i = 0; i < keySuccessors.length; i++) {
|
|
142 |
if (successors.get(keySuccessors[i]) == successor) {
|
|
143 |
keyProbabilities[i] = Math.max(0.0, keyProbabilities[i] + (delta * keyProbabilities[i]) / sum);
|
|
144 |
} else {
|
|
145 |
keyProbabilities[i] = Math.max(0.0, keyProbabilities[i] - (delta * keyProbabilities[i]) / otherSum);
|
|
146 |
}
|
|
147 |
}
|
|
148 |
assert assertProbabilities();
|
|
149 |
return true;
|
|
150 |
}
|
|
151 |
|
43972
|
152 |
public ValueNode value() {
|
|
153 |
return value;
|
|
154 |
}
|
|
155 |
|
|
156 |
public abstract boolean isSorted();
|
|
157 |
|
|
158 |
/**
|
|
159 |
* The number of distinct keys in this switch.
|
|
160 |
*/
|
|
161 |
public abstract int keyCount();
|
|
162 |
|
|
163 |
/**
|
|
164 |
* The key at the specified position, encoded in a Constant.
|
|
165 |
*/
|
|
166 |
public abstract Constant keyAt(int i);
|
|
167 |
|
|
168 |
public boolean structureEquals(SwitchNode switchNode) {
|
|
169 |
return Arrays.equals(keySuccessors, switchNode.keySuccessors) && equalKeys(switchNode);
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Returns true if the switch has the same keys in the same order as this switch.
|
|
174 |
*/
|
|
175 |
public abstract boolean equalKeys(SwitchNode switchNode);
|
|
176 |
|
|
177 |
/**
|
|
178 |
* Returns the index of the successor belonging to the key at the specified index.
|
|
179 |
*/
|
|
180 |
public int keySuccessorIndex(int i) {
|
|
181 |
return keySuccessors[i];
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* Returns the successor for the key at the given index.
|
|
186 |
*/
|
|
187 |
public AbstractBeginNode keySuccessor(int i) {
|
|
188 |
return successors.get(keySuccessors[i]);
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Returns the probability of the key at the given index.
|
|
193 |
*/
|
|
194 |
public double keyProbability(int i) {
|
|
195 |
return keyProbabilities[i];
|
|
196 |
}
|
|
197 |
|
|
198 |
/**
|
|
199 |
* Returns the index of the default (fall through) successor of this switch.
|
|
200 |
*/
|
|
201 |
public int defaultSuccessorIndex() {
|
|
202 |
return keySuccessors[keySuccessors.length - 1];
|
|
203 |
}
|
|
204 |
|
|
205 |
public AbstractBeginNode blockSuccessor(int i) {
|
|
206 |
return successors.get(i);
|
|
207 |
}
|
|
208 |
|
|
209 |
public void setBlockSuccessor(int i, AbstractBeginNode s) {
|
|
210 |
successors.set(i, s);
|
|
211 |
}
|
|
212 |
|
|
213 |
public int blockSuccessorCount() {
|
|
214 |
return successors.count();
|
|
215 |
}
|
|
216 |
|
|
217 |
/**
|
|
218 |
* Gets the successor corresponding to the default (fall through) case.
|
|
219 |
*
|
|
220 |
* @return the default successor
|
|
221 |
*/
|
|
222 |
public AbstractBeginNode defaultSuccessor() {
|
|
223 |
if (defaultSuccessorIndex() == -1) {
|
|
224 |
throw new GraalError("unexpected");
|
|
225 |
}
|
|
226 |
return successors.get(defaultSuccessorIndex());
|
|
227 |
}
|
|
228 |
|
|
229 |
@Override
|
|
230 |
public AbstractBeginNode getPrimarySuccessor() {
|
48861
|
231 |
return null;
|
43972
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Delete all other successors except for the one reached by {@code survivingEdge}.
|
|
236 |
*
|
|
237 |
* @param tool
|
|
238 |
* @param survivingEdge index of the edge in the {@link SwitchNode#successors} list
|
|
239 |
*/
|
|
240 |
protected void killOtherSuccessors(SimplifierTool tool, int survivingEdge) {
|
|
241 |
for (Node successor : successors()) {
|
|
242 |
/*
|
|
243 |
* Deleting a branch change change the successors so reload the surviving successor each
|
|
244 |
* time.
|
|
245 |
*/
|
|
246 |
if (successor != blockSuccessor(survivingEdge)) {
|
|
247 |
tool.deleteBranch(successor);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
tool.addToWorkList(blockSuccessor(survivingEdge));
|
|
251 |
graph().removeSplit(this, blockSuccessor(survivingEdge));
|
|
252 |
}
|
46344
|
253 |
|
|
254 |
public abstract Stamp getValueStampForSuccessor(AbstractBeginNode beginNode);
|
46393
|
255 |
|
|
256 |
@Override
|
|
257 |
public NodeCycles estimatedNodeCycles() {
|
|
258 |
if (keyCount() == 1) {
|
|
259 |
// if
|
|
260 |
return CYCLES_2;
|
|
261 |
} else if (isSorted()) {
|
|
262 |
// good heuristic
|
|
263 |
return CYCLES_8;
|
|
264 |
} else {
|
|
265 |
// not so good
|
|
266 |
return CYCLES_64;
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
@Override
|
|
271 |
public NodeSize estimatedNodeSize() {
|
|
272 |
if (keyCount() == 1) {
|
|
273 |
// if
|
|
274 |
return SIZE_2;
|
|
275 |
} else if (isSorted()) {
|
|
276 |
// good heuristic
|
|
277 |
return SIZE_8;
|
|
278 |
} else {
|
|
279 |
// not so good
|
|
280 |
return SIZE_64;
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
43972
|
284 |
}
|