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.phases.schedule;
|
|
24 |
|
48861
|
25 |
import static org.graalvm.collections.Equivalence.IDENTITY;
|
|
26 |
import static org.graalvm.compiler.core.common.GraalOptions.GuardPriorities;
|
46640
|
27 |
import static org.graalvm.compiler.core.common.GraalOptions.OptScheduleOutOfLoops;
|
|
28 |
import static org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph.strictlyDominates;
|
|
29 |
|
|
30 |
import java.util.ArrayList;
|
|
31 |
import java.util.Arrays;
|
48861
|
32 |
import java.util.Comparator;
|
|
33 |
import java.util.EnumMap;
|
46640
|
34 |
import java.util.Formatter;
|
48861
|
35 |
import java.util.Iterator;
|
46640
|
36 |
import java.util.List;
|
48861
|
37 |
import java.util.SortedSet;
|
|
38 |
import java.util.TreeSet;
|
|
39 |
import java.util.function.Function;
|
46640
|
40 |
|
48861
|
41 |
import org.graalvm.collections.EconomicSet;
|
43972
|
42 |
import org.graalvm.compiler.core.common.SuppressFBWarnings;
|
|
43 |
import org.graalvm.compiler.core.common.cfg.AbstractControlFlowGraph;
|
|
44 |
import org.graalvm.compiler.core.common.cfg.BlockMap;
|
46344
|
45 |
import org.graalvm.compiler.debug.Assertions;
|
43972
|
46 |
import org.graalvm.compiler.graph.Graph.NodeEvent;
|
|
47 |
import org.graalvm.compiler.graph.Graph.NodeEventListener;
|
|
48 |
import org.graalvm.compiler.graph.Graph.NodeEventScope;
|
|
49 |
import org.graalvm.compiler.graph.Node;
|
|
50 |
import org.graalvm.compiler.graph.NodeBitMap;
|
|
51 |
import org.graalvm.compiler.graph.NodeMap;
|
|
52 |
import org.graalvm.compiler.graph.NodeStack;
|
|
53 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
54 |
import org.graalvm.compiler.nodes.AbstractEndNode;
|
|
55 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
56 |
import org.graalvm.compiler.nodes.ControlSinkNode;
|
|
57 |
import org.graalvm.compiler.nodes.ControlSplitNode;
|
|
58 |
import org.graalvm.compiler.nodes.DeoptimizeNode;
|
|
59 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
60 |
import org.graalvm.compiler.nodes.GuardNode;
|
46344
|
61 |
import org.graalvm.compiler.nodes.IfNode;
|
|
62 |
import org.graalvm.compiler.nodes.KillingBeginNode;
|
43972
|
63 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
|
64 |
import org.graalvm.compiler.nodes.LoopExitNode;
|
|
65 |
import org.graalvm.compiler.nodes.PhiNode;
|
|
66 |
import org.graalvm.compiler.nodes.ProxyNode;
|
|
67 |
import org.graalvm.compiler.nodes.StartNode;
|
48861
|
68 |
import org.graalvm.compiler.nodes.StaticDeoptimizingNode;
|
|
69 |
import org.graalvm.compiler.nodes.StaticDeoptimizingNode.GuardPriority;
|
43972
|
70 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
71 |
import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
|
|
72 |
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
|
|
73 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
74 |
import org.graalvm.compiler.nodes.VirtualState;
|
46344
|
75 |
import org.graalvm.compiler.nodes.calc.ConvertNode;
|
|
76 |
import org.graalvm.compiler.nodes.calc.IsNullNode;
|
43972
|
77 |
import org.graalvm.compiler.nodes.cfg.Block;
|
|
78 |
import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
|
|
79 |
import org.graalvm.compiler.nodes.cfg.HIRLoop;
|
|
80 |
import org.graalvm.compiler.nodes.cfg.LocationSet;
|
|
81 |
import org.graalvm.compiler.nodes.memory.FloatingReadNode;
|
|
82 |
import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
|
46344
|
83 |
import org.graalvm.compiler.nodes.spi.ValueProxy;
|
|
84 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
85 |
import org.graalvm.compiler.phases.Phase;
|
46551
|
86 |
import org.graalvm.word.LocationIdentity;
|
46509
|
87 |
|
43972
|
88 |
public final class SchedulePhase extends Phase {
|
|
89 |
|
|
90 |
public enum SchedulingStrategy {
|
48861
|
91 |
EARLIEST_WITH_GUARD_ORDER,
|
43972
|
92 |
EARLIEST,
|
|
93 |
LATEST,
|
|
94 |
LATEST_OUT_OF_LOOPS,
|
48861
|
95 |
FINAL_SCHEDULE;
|
|
96 |
|
|
97 |
public boolean isEarliest() {
|
|
98 |
return this == EARLIEST || this == EARLIEST_WITH_GUARD_ORDER;
|
|
99 |
}
|
|
100 |
|
|
101 |
public boolean isLatest() {
|
|
102 |
return !isEarliest();
|
|
103 |
}
|
43972
|
104 |
}
|
|
105 |
|
|
106 |
private final SchedulingStrategy selectedStrategy;
|
|
107 |
|
|
108 |
private final boolean immutableGraph;
|
|
109 |
|
46344
|
110 |
public SchedulePhase(OptionValues options) {
|
|
111 |
this(false, options);
|
43972
|
112 |
}
|
|
113 |
|
46344
|
114 |
public SchedulePhase(boolean immutableGraph, OptionValues options) {
|
|
115 |
this(OptScheduleOutOfLoops.getValue(options) ? SchedulingStrategy.LATEST_OUT_OF_LOOPS : SchedulingStrategy.LATEST, immutableGraph);
|
43972
|
116 |
}
|
|
117 |
|
|
118 |
public SchedulePhase(SchedulingStrategy strategy) {
|
|
119 |
this(strategy, false);
|
|
120 |
}
|
|
121 |
|
|
122 |
public SchedulePhase(SchedulingStrategy strategy, boolean immutableGraph) {
|
|
123 |
this.selectedStrategy = strategy;
|
|
124 |
this.immutableGraph = immutableGraph;
|
|
125 |
}
|
|
126 |
|
|
127 |
private NodeEventScope verifyImmutableGraph(StructuredGraph graph) {
|
46762
|
128 |
if (immutableGraph && Assertions.assertionsEnabled()) {
|
43972
|
129 |
return graph.trackNodeEvents(new NodeEventListener() {
|
|
130 |
@Override
|
47798
|
131 |
public void changed(NodeEvent e, Node node) {
|
43972
|
132 |
assert false : "graph changed: " + e + " on node " + node;
|
|
133 |
}
|
|
134 |
});
|
|
135 |
} else {
|
|
136 |
return null;
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
@Override
|
|
141 |
@SuppressWarnings("try")
|
|
142 |
protected void run(StructuredGraph graph) {
|
|
143 |
try (NodeEventScope scope = verifyImmutableGraph(graph)) {
|
|
144 |
Instance inst = new Instance();
|
|
145 |
inst.run(graph, selectedStrategy, immutableGraph);
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
46344
|
149 |
public static void run(StructuredGraph graph, SchedulingStrategy strategy, ControlFlowGraph cfg) {
|
|
150 |
Instance inst = new Instance(cfg);
|
|
151 |
inst.run(graph, strategy, false);
|
|
152 |
}
|
|
153 |
|
43972
|
154 |
public static class Instance {
|
|
155 |
|
46344
|
156 |
private static final double IMPLICIT_NULL_CHECK_OPPORTUNITY_PROBABILITY_FACTOR = 2;
|
43972
|
157 |
/**
|
|
158 |
* Map from blocks to the nodes in each block.
|
|
159 |
*/
|
|
160 |
protected ControlFlowGraph cfg;
|
|
161 |
protected BlockMap<List<Node>> blockToNodesMap;
|
|
162 |
protected NodeMap<Block> nodeToBlockMap;
|
|
163 |
|
46344
|
164 |
public Instance() {
|
|
165 |
this(null);
|
|
166 |
}
|
|
167 |
|
|
168 |
public Instance(ControlFlowGraph cfg) {
|
|
169 |
this.cfg = cfg;
|
|
170 |
}
|
|
171 |
|
43972
|
172 |
@SuppressWarnings("try")
|
|
173 |
public void run(StructuredGraph graph, SchedulingStrategy selectedStrategy, boolean immutableGraph) {
|
|
174 |
// assert GraphOrder.assertNonCyclicGraph(graph);
|
46344
|
175 |
|
|
176 |
if (this.cfg == null) {
|
|
177 |
this.cfg = ControlFlowGraph.compute(graph, true, true, true, false);
|
|
178 |
}
|
43972
|
179 |
|
|
180 |
NodeMap<Block> currentNodeMap = graph.createNodeMap();
|
|
181 |
NodeBitMap visited = graph.createNodeBitMap();
|
|
182 |
BlockMap<List<Node>> earliestBlockToNodesMap = new BlockMap<>(cfg);
|
|
183 |
this.nodeToBlockMap = currentNodeMap;
|
|
184 |
this.blockToNodesMap = earliestBlockToNodesMap;
|
|
185 |
|
48861
|
186 |
scheduleEarliestIterative(earliestBlockToNodesMap, currentNodeMap, visited, graph, immutableGraph, selectedStrategy == SchedulingStrategy.EARLIEST_WITH_GUARD_ORDER);
|
43972
|
187 |
|
48861
|
188 |
if (!selectedStrategy.isEarliest()) {
|
43972
|
189 |
// For non-earliest schedules, we need to do a second pass.
|
|
190 |
BlockMap<List<Node>> latestBlockToNodesMap = new BlockMap<>(cfg);
|
|
191 |
for (Block b : cfg.getBlocks()) {
|
48861
|
192 |
latestBlockToNodesMap.put(b, new ArrayList<>());
|
43972
|
193 |
}
|
|
194 |
|
|
195 |
BlockMap<ArrayList<FloatingReadNode>> watchListMap = calcLatestBlocks(selectedStrategy, currentNodeMap, earliestBlockToNodesMap, visited, latestBlockToNodesMap, immutableGraph);
|
|
196 |
sortNodesLatestWithinBlock(cfg, earliestBlockToNodesMap, latestBlockToNodesMap, currentNodeMap, watchListMap, visited);
|
|
197 |
|
|
198 |
assert verifySchedule(cfg, latestBlockToNodesMap, currentNodeMap);
|
46762
|
199 |
assert (!Assertions.detailedAssertionsEnabled(graph.getOptions())) || MemoryScheduleVerification.check(cfg.getStartBlock(), latestBlockToNodesMap);
|
43972
|
200 |
|
|
201 |
this.blockToNodesMap = latestBlockToNodesMap;
|
|
202 |
|
|
203 |
}
|
48861
|
204 |
cfg.setNodeToBlock(currentNodeMap);
|
43972
|
205 |
|
|
206 |
graph.setLastSchedule(new ScheduleResult(this.cfg, this.nodeToBlockMap, this.blockToNodesMap));
|
|
207 |
}
|
|
208 |
|
|
209 |
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "false positive found by findbugs")
|
|
210 |
private BlockMap<ArrayList<FloatingReadNode>> calcLatestBlocks(SchedulingStrategy strategy, NodeMap<Block> currentNodeMap, BlockMap<List<Node>> earliestBlockToNodesMap, NodeBitMap visited,
|
|
211 |
BlockMap<List<Node>> latestBlockToNodesMap, boolean immutableGraph) {
|
|
212 |
BlockMap<ArrayList<FloatingReadNode>> watchListMap = new BlockMap<>(cfg);
|
|
213 |
Block[] reversePostOrder = cfg.reversePostOrder();
|
|
214 |
for (int j = reversePostOrder.length - 1; j >= 0; --j) {
|
|
215 |
Block currentBlock = reversePostOrder[j];
|
|
216 |
List<Node> blockToNodes = earliestBlockToNodesMap.get(currentBlock);
|
|
217 |
LocationSet killed = null;
|
|
218 |
int previousIndex = blockToNodes.size();
|
|
219 |
for (int i = blockToNodes.size() - 1; i >= 0; --i) {
|
|
220 |
Node currentNode = blockToNodes.get(i);
|
|
221 |
assert currentNodeMap.get(currentNode) == currentBlock;
|
|
222 |
assert !(currentNode instanceof PhiNode) && !(currentNode instanceof ProxyNode);
|
|
223 |
assert visited.isMarked(currentNode);
|
|
224 |
if (currentNode instanceof FixedNode) {
|
|
225 |
// For these nodes, the earliest is at the same time the latest block.
|
|
226 |
} else {
|
|
227 |
Block latestBlock = null;
|
|
228 |
|
|
229 |
LocationIdentity constrainingLocation = null;
|
|
230 |
if (currentNode instanceof FloatingReadNode) {
|
|
231 |
// We are scheduling a floating read node => check memory
|
|
232 |
// anti-dependencies.
|
|
233 |
FloatingReadNode floatingReadNode = (FloatingReadNode) currentNode;
|
|
234 |
LocationIdentity location = floatingReadNode.getLocationIdentity();
|
|
235 |
if (location.isMutable()) {
|
|
236 |
// Location can be killed.
|
|
237 |
constrainingLocation = location;
|
|
238 |
if (currentBlock.canKill(location)) {
|
|
239 |
if (killed == null) {
|
|
240 |
killed = new LocationSet();
|
|
241 |
}
|
|
242 |
fillKillSet(killed, blockToNodes.subList(i + 1, previousIndex));
|
|
243 |
previousIndex = i;
|
|
244 |
if (killed.contains(location)) {
|
|
245 |
// Earliest block kills location => we need to stay within
|
|
246 |
// earliest block.
|
|
247 |
latestBlock = currentBlock;
|
|
248 |
}
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
if (latestBlock == null) {
|
|
254 |
// We are not constraint within earliest block => calculate optimized
|
|
255 |
// schedule.
|
|
256 |
calcLatestBlock(currentBlock, strategy, currentNode, currentNodeMap, constrainingLocation, watchListMap, latestBlockToNodesMap, visited, immutableGraph);
|
|
257 |
} else {
|
|
258 |
selectLatestBlock(currentNode, currentBlock, latestBlock, currentNodeMap, watchListMap, constrainingLocation, latestBlockToNodesMap);
|
|
259 |
}
|
|
260 |
}
|
|
261 |
}
|
|
262 |
}
|
|
263 |
return watchListMap;
|
|
264 |
}
|
|
265 |
|
|
266 |
protected static void selectLatestBlock(Node currentNode, Block currentBlock, Block latestBlock, NodeMap<Block> currentNodeMap, BlockMap<ArrayList<FloatingReadNode>> watchListMap,
|
|
267 |
LocationIdentity constrainingLocation, BlockMap<List<Node>> latestBlockToNodesMap) {
|
|
268 |
|
|
269 |
assert checkLatestEarliestRelation(currentNode, currentBlock, latestBlock);
|
|
270 |
if (currentBlock != latestBlock) {
|
46344
|
271 |
|
43972
|
272 |
currentNodeMap.setAndGrow(currentNode, latestBlock);
|
|
273 |
|
|
274 |
if (constrainingLocation != null && latestBlock.canKill(constrainingLocation)) {
|
|
275 |
if (watchListMap.get(latestBlock) == null) {
|
|
276 |
watchListMap.put(latestBlock, new ArrayList<>());
|
|
277 |
}
|
|
278 |
watchListMap.get(latestBlock).add((FloatingReadNode) currentNode);
|
|
279 |
}
|
|
280 |
}
|
|
281 |
|
|
282 |
latestBlockToNodesMap.get(latestBlock).add(currentNode);
|
|
283 |
}
|
|
284 |
|
|
285 |
private static boolean checkLatestEarliestRelation(Node currentNode, Block earliestBlock, Block latestBlock) {
|
|
286 |
assert AbstractControlFlowGraph.dominates(earliestBlock, latestBlock) || (currentNode instanceof VirtualState && latestBlock == earliestBlock.getDominator()) : String.format(
|
|
287 |
"%s %s (%s) %s (%s)", currentNode, earliestBlock, earliestBlock.getBeginNode(), latestBlock, latestBlock.getBeginNode());
|
|
288 |
return true;
|
|
289 |
}
|
|
290 |
|
|
291 |
private static boolean verifySchedule(ControlFlowGraph cfg, BlockMap<List<Node>> blockToNodesMap, NodeMap<Block> nodeMap) {
|
|
292 |
for (Block b : cfg.getBlocks()) {
|
|
293 |
List<Node> nodes = blockToNodesMap.get(b);
|
|
294 |
for (Node n : nodes) {
|
|
295 |
assert n.isAlive();
|
|
296 |
assert nodeMap.get(n) == b;
|
|
297 |
StructuredGraph g = (StructuredGraph) n.graph();
|
|
298 |
if (g.hasLoops() && g.getGuardsStage() == GuardsStage.AFTER_FSA && n instanceof DeoptimizeNode) {
|
|
299 |
assert b.getLoopDepth() == 0 : n;
|
|
300 |
}
|
|
301 |
}
|
|
302 |
}
|
|
303 |
return true;
|
|
304 |
}
|
|
305 |
|
|
306 |
public static Block checkKillsBetween(Block earliestBlock, Block latestBlock, LocationIdentity location) {
|
|
307 |
assert strictlyDominates(earliestBlock, latestBlock);
|
|
308 |
Block current = latestBlock.getDominator();
|
|
309 |
|
|
310 |
// Collect dominator chain that needs checking.
|
|
311 |
List<Block> dominatorChain = new ArrayList<>();
|
|
312 |
dominatorChain.add(latestBlock);
|
|
313 |
while (current != earliestBlock) {
|
|
314 |
// Current is an intermediate dominator between earliestBlock and latestBlock.
|
|
315 |
assert strictlyDominates(earliestBlock, current) && strictlyDominates(current, latestBlock);
|
|
316 |
if (current.canKill(location)) {
|
|
317 |
dominatorChain.clear();
|
|
318 |
}
|
|
319 |
dominatorChain.add(current);
|
|
320 |
current = current.getDominator();
|
|
321 |
}
|
|
322 |
|
|
323 |
// The first element of dominatorChain now contains the latest possible block.
|
|
324 |
assert dominatorChain.size() >= 1;
|
|
325 |
assert dominatorChain.get(dominatorChain.size() - 1).getDominator() == earliestBlock;
|
|
326 |
|
|
327 |
Block lastBlock = earliestBlock;
|
|
328 |
for (int i = dominatorChain.size() - 1; i >= 0; --i) {
|
|
329 |
Block currentBlock = dominatorChain.get(i);
|
|
330 |
if (currentBlock.getLoopDepth() > lastBlock.getLoopDepth()) {
|
|
331 |
// We are entering a loop boundary. The new loops must not kill the location for
|
|
332 |
// the crossing to be safe.
|
|
333 |
if (currentBlock.getLoop() != null && ((HIRLoop) currentBlock.getLoop()).canKill(location)) {
|
|
334 |
break;
|
|
335 |
}
|
|
336 |
}
|
|
337 |
|
|
338 |
if (currentBlock.canKillBetweenThisAndDominator(location)) {
|
|
339 |
break;
|
|
340 |
}
|
|
341 |
lastBlock = currentBlock;
|
|
342 |
}
|
|
343 |
|
46344
|
344 |
if (lastBlock.getBeginNode() instanceof KillingBeginNode) {
|
|
345 |
LocationIdentity locationIdentity = ((KillingBeginNode) lastBlock.getBeginNode()).getLocationIdentity();
|
|
346 |
if ((locationIdentity.isAny() || locationIdentity.equals(location)) && lastBlock != earliestBlock) {
|
|
347 |
// The begin of this block kills the location, so we *have* to schedule the node
|
|
348 |
// in the dominating block.
|
|
349 |
lastBlock = lastBlock.getDominator();
|
|
350 |
}
|
|
351 |
}
|
|
352 |
|
43972
|
353 |
return lastBlock;
|
|
354 |
}
|
|
355 |
|
|
356 |
private static void fillKillSet(LocationSet killed, List<Node> subList) {
|
|
357 |
if (!killed.isAny()) {
|
|
358 |
for (Node n : subList) {
|
|
359 |
// Check if this node kills a node in the watch list.
|
|
360 |
if (n instanceof MemoryCheckpoint.Single) {
|
|
361 |
LocationIdentity identity = ((MemoryCheckpoint.Single) n).getLocationIdentity();
|
|
362 |
killed.add(identity);
|
|
363 |
if (killed.isAny()) {
|
|
364 |
return;
|
|
365 |
}
|
|
366 |
} else if (n instanceof MemoryCheckpoint.Multi) {
|
|
367 |
for (LocationIdentity identity : ((MemoryCheckpoint.Multi) n).getLocationIdentities()) {
|
|
368 |
killed.add(identity);
|
|
369 |
if (killed.isAny()) {
|
|
370 |
return;
|
|
371 |
}
|
|
372 |
}
|
|
373 |
}
|
|
374 |
}
|
|
375 |
}
|
|
376 |
}
|
|
377 |
|
|
378 |
private static void sortNodesLatestWithinBlock(ControlFlowGraph cfg, BlockMap<List<Node>> earliestBlockToNodesMap, BlockMap<List<Node>> latestBlockToNodesMap, NodeMap<Block> currentNodeMap,
|
|
379 |
BlockMap<ArrayList<FloatingReadNode>> watchListMap, NodeBitMap visited) {
|
|
380 |
for (Block b : cfg.getBlocks()) {
|
|
381 |
sortNodesLatestWithinBlock(b, earliestBlockToNodesMap, latestBlockToNodesMap, currentNodeMap, watchListMap, visited);
|
|
382 |
}
|
|
383 |
}
|
|
384 |
|
|
385 |
private static void sortNodesLatestWithinBlock(Block b, BlockMap<List<Node>> earliestBlockToNodesMap, BlockMap<List<Node>> latestBlockToNodesMap, NodeMap<Block> nodeMap,
|
|
386 |
BlockMap<ArrayList<FloatingReadNode>> watchListMap, NodeBitMap unprocessed) {
|
|
387 |
List<Node> earliestSorting = earliestBlockToNodesMap.get(b);
|
|
388 |
ArrayList<Node> result = new ArrayList<>(earliestSorting.size());
|
|
389 |
ArrayList<FloatingReadNode> watchList = null;
|
|
390 |
if (watchListMap != null) {
|
|
391 |
watchList = watchListMap.get(b);
|
|
392 |
assert watchList == null || !b.getKillLocations().isEmpty();
|
|
393 |
}
|
|
394 |
AbstractBeginNode beginNode = b.getBeginNode();
|
|
395 |
if (beginNode instanceof LoopExitNode) {
|
|
396 |
LoopExitNode loopExitNode = (LoopExitNode) beginNode;
|
|
397 |
for (ProxyNode proxy : loopExitNode.proxies()) {
|
|
398 |
unprocessed.clear(proxy);
|
|
399 |
ValueNode value = proxy.value();
|
|
400 |
// if multiple proxies reference the same value, schedule the value of a
|
|
401 |
// proxy once
|
|
402 |
if (value != null && nodeMap.get(value) == b && unprocessed.isMarked(value)) {
|
|
403 |
sortIntoList(value, b, result, nodeMap, unprocessed, null);
|
|
404 |
}
|
|
405 |
}
|
|
406 |
}
|
|
407 |
FixedNode endNode = b.getEndNode();
|
|
408 |
FixedNode fixedEndNode = null;
|
|
409 |
if (isFixedEnd(endNode)) {
|
|
410 |
// Only if the end node is either a control split or an end node, we need to force
|
|
411 |
// it to be the last node in the schedule.
|
|
412 |
fixedEndNode = endNode;
|
|
413 |
}
|
|
414 |
for (Node n : earliestSorting) {
|
|
415 |
if (n != fixedEndNode) {
|
|
416 |
if (n instanceof FixedNode) {
|
|
417 |
assert nodeMap.get(n) == b;
|
|
418 |
checkWatchList(b, nodeMap, unprocessed, result, watchList, n);
|
|
419 |
sortIntoList(n, b, result, nodeMap, unprocessed, null);
|
|
420 |
} else if (nodeMap.get(n) == b && n instanceof FloatingReadNode) {
|
|
421 |
FloatingReadNode floatingReadNode = (FloatingReadNode) n;
|
46344
|
422 |
if (isImplicitNullOpportunity(floatingReadNode, b)) {
|
|
423 |
// Schedule at the beginning of the block.
|
|
424 |
sortIntoList(floatingReadNode, b, result, nodeMap, unprocessed, null);
|
|
425 |
} else {
|
|
426 |
LocationIdentity location = floatingReadNode.getLocationIdentity();
|
|
427 |
if (b.canKill(location)) {
|
|
428 |
// This read can be killed in this block, add to watch list.
|
|
429 |
if (watchList == null) {
|
|
430 |
watchList = new ArrayList<>();
|
|
431 |
}
|
|
432 |
watchList.add(floatingReadNode);
|
43972
|
433 |
}
|
|
434 |
}
|
|
435 |
}
|
|
436 |
}
|
|
437 |
}
|
|
438 |
|
|
439 |
for (Node n : latestBlockToNodesMap.get(b)) {
|
|
440 |
assert nodeMap.get(n) == b : n;
|
|
441 |
assert !(n instanceof FixedNode);
|
|
442 |
if (unprocessed.isMarked(n)) {
|
|
443 |
sortIntoList(n, b, result, nodeMap, unprocessed, fixedEndNode);
|
|
444 |
}
|
|
445 |
}
|
|
446 |
|
|
447 |
if (endNode != null && unprocessed.isMarked(endNode)) {
|
|
448 |
sortIntoList(endNode, b, result, nodeMap, unprocessed, null);
|
|
449 |
}
|
|
450 |
|
|
451 |
latestBlockToNodesMap.put(b, result);
|
|
452 |
}
|
|
453 |
|
|
454 |
private static void checkWatchList(Block b, NodeMap<Block> nodeMap, NodeBitMap unprocessed, ArrayList<Node> result, ArrayList<FloatingReadNode> watchList, Node n) {
|
|
455 |
if (watchList != null && !watchList.isEmpty()) {
|
|
456 |
// Check if this node kills a node in the watch list.
|
|
457 |
if (n instanceof MemoryCheckpoint.Single) {
|
|
458 |
LocationIdentity identity = ((MemoryCheckpoint.Single) n).getLocationIdentity();
|
|
459 |
checkWatchList(watchList, identity, b, result, nodeMap, unprocessed);
|
|
460 |
} else if (n instanceof MemoryCheckpoint.Multi) {
|
|
461 |
for (LocationIdentity identity : ((MemoryCheckpoint.Multi) n).getLocationIdentities()) {
|
|
462 |
checkWatchList(watchList, identity, b, result, nodeMap, unprocessed);
|
|
463 |
}
|
|
464 |
}
|
|
465 |
}
|
|
466 |
}
|
|
467 |
|
|
468 |
private static void checkWatchList(ArrayList<FloatingReadNode> watchList, LocationIdentity identity, Block b, ArrayList<Node> result, NodeMap<Block> nodeMap, NodeBitMap unprocessed) {
|
46344
|
469 |
if (identity.isImmutable()) {
|
|
470 |
// Nothing to do. This can happen for an initialization write.
|
|
471 |
} else if (identity.isAny()) {
|
43972
|
472 |
for (FloatingReadNode r : watchList) {
|
|
473 |
if (unprocessed.isMarked(r)) {
|
|
474 |
sortIntoList(r, b, result, nodeMap, unprocessed, null);
|
|
475 |
}
|
|
476 |
}
|
|
477 |
watchList.clear();
|
|
478 |
} else {
|
|
479 |
int index = 0;
|
|
480 |
while (index < watchList.size()) {
|
|
481 |
FloatingReadNode r = watchList.get(index);
|
|
482 |
LocationIdentity locationIdentity = r.getLocationIdentity();
|
|
483 |
assert locationIdentity.isMutable();
|
|
484 |
if (unprocessed.isMarked(r)) {
|
|
485 |
if (identity.overlaps(locationIdentity)) {
|
|
486 |
sortIntoList(r, b, result, nodeMap, unprocessed, null);
|
|
487 |
} else {
|
|
488 |
++index;
|
|
489 |
continue;
|
|
490 |
}
|
|
491 |
}
|
|
492 |
int lastIndex = watchList.size() - 1;
|
|
493 |
watchList.set(index, watchList.get(lastIndex));
|
|
494 |
watchList.remove(lastIndex);
|
|
495 |
}
|
|
496 |
}
|
|
497 |
}
|
|
498 |
|
|
499 |
private static void sortIntoList(Node n, Block b, ArrayList<Node> result, NodeMap<Block> nodeMap, NodeBitMap unprocessed, Node excludeNode) {
|
|
500 |
assert unprocessed.isMarked(n) : n;
|
|
501 |
assert nodeMap.get(n) == b;
|
|
502 |
|
|
503 |
if (n instanceof PhiNode) {
|
|
504 |
return;
|
|
505 |
}
|
|
506 |
|
|
507 |
unprocessed.clear(n);
|
|
508 |
|
|
509 |
for (Node input : n.inputs()) {
|
|
510 |
if (nodeMap.get(input) == b && unprocessed.isMarked(input) && input != excludeNode) {
|
|
511 |
sortIntoList(input, b, result, nodeMap, unprocessed, excludeNode);
|
|
512 |
}
|
|
513 |
}
|
|
514 |
|
|
515 |
if (n instanceof ProxyNode) {
|
|
516 |
// Skip proxy nodes.
|
|
517 |
} else {
|
|
518 |
result.add(n);
|
|
519 |
}
|
|
520 |
|
|
521 |
}
|
|
522 |
|
|
523 |
protected void calcLatestBlock(Block earliestBlock, SchedulingStrategy strategy, Node currentNode, NodeMap<Block> currentNodeMap, LocationIdentity constrainingLocation,
|
|
524 |
BlockMap<ArrayList<FloatingReadNode>> watchListMap, BlockMap<List<Node>> latestBlockToNodesMap, NodeBitMap visited, boolean immutableGraph) {
|
|
525 |
Block latestBlock = null;
|
46344
|
526 |
if (!currentNode.hasUsages()) {
|
|
527 |
assert currentNode instanceof GuardNode;
|
|
528 |
latestBlock = earliestBlock;
|
|
529 |
} else {
|
|
530 |
assert currentNode.hasUsages();
|
|
531 |
for (Node usage : currentNode.usages()) {
|
|
532 |
if (immutableGraph && !visited.contains(usage)) {
|
|
533 |
/*
|
|
534 |
* Normally, dead nodes are deleted by the scheduler before we reach this
|
|
535 |
* point. Only when the scheduler is asked to not modify a graph, we can see
|
|
536 |
* dead nodes here.
|
|
537 |
*/
|
|
538 |
continue;
|
|
539 |
}
|
|
540 |
latestBlock = calcBlockForUsage(currentNode, usage, latestBlock, currentNodeMap);
|
43972
|
541 |
}
|
46344
|
542 |
|
|
543 |
assert latestBlock != null : currentNode;
|
43972
|
544 |
|
46344
|
545 |
if (strategy == SchedulingStrategy.FINAL_SCHEDULE || strategy == SchedulingStrategy.LATEST_OUT_OF_LOOPS) {
|
|
546 |
Block currentBlock = latestBlock;
|
|
547 |
while (currentBlock.getLoopDepth() > earliestBlock.getLoopDepth() && currentBlock != earliestBlock.getDominator()) {
|
|
548 |
Block previousCurrentBlock = currentBlock;
|
|
549 |
currentBlock = currentBlock.getDominator();
|
|
550 |
if (previousCurrentBlock.isLoopHeader()) {
|
|
551 |
if (currentBlock.probability() < latestBlock.probability() || ((StructuredGraph) currentNode.graph()).hasValueProxies()) {
|
|
552 |
// Only assign new latest block if frequency is actually lower or if
|
|
553 |
// loop proxies would be required otherwise.
|
|
554 |
latestBlock = currentBlock;
|
|
555 |
}
|
|
556 |
}
|
|
557 |
}
|
|
558 |
}
|
|
559 |
|
|
560 |
if (latestBlock != earliestBlock && latestBlock != earliestBlock.getDominator() && constrainingLocation != null) {
|
|
561 |
latestBlock = checkKillsBetween(earliestBlock, latestBlock, constrainingLocation);
|
43972
|
562 |
}
|
|
563 |
}
|
|
564 |
|
46344
|
565 |
if (latestBlock != earliestBlock && currentNode instanceof FloatingReadNode) {
|
|
566 |
|
|
567 |
FloatingReadNode floatingReadNode = (FloatingReadNode) currentNode;
|
|
568 |
if (isImplicitNullOpportunity(floatingReadNode, earliestBlock) && earliestBlock.probability() < latestBlock.probability() * IMPLICIT_NULL_CHECK_OPPORTUNITY_PROBABILITY_FACTOR) {
|
|
569 |
latestBlock = earliestBlock;
|
|
570 |
}
|
43972
|
571 |
}
|
|
572 |
|
|
573 |
selectLatestBlock(currentNode, earliestBlock, latestBlock, currentNodeMap, watchListMap, constrainingLocation, latestBlockToNodesMap);
|
|
574 |
}
|
|
575 |
|
46344
|
576 |
private static boolean isImplicitNullOpportunity(FloatingReadNode floatingReadNode, Block block) {
|
|
577 |
|
|
578 |
Node pred = block.getBeginNode().predecessor();
|
|
579 |
if (pred instanceof IfNode) {
|
|
580 |
IfNode ifNode = (IfNode) pred;
|
|
581 |
if (ifNode.condition() instanceof IsNullNode) {
|
|
582 |
IsNullNode isNullNode = (IsNullNode) ifNode.condition();
|
|
583 |
if (getUnproxifiedUncompressed(floatingReadNode.getAddress().getBase()) == getUnproxifiedUncompressed(isNullNode.getValue())) {
|
|
584 |
return true;
|
|
585 |
}
|
|
586 |
}
|
|
587 |
}
|
|
588 |
return false;
|
|
589 |
}
|
|
590 |
|
|
591 |
private static Node getUnproxifiedUncompressed(Node node) {
|
|
592 |
Node result = node;
|
|
593 |
while (true) {
|
|
594 |
if (result instanceof ValueProxy) {
|
|
595 |
ValueProxy valueProxy = (ValueProxy) result;
|
|
596 |
result = valueProxy.getOriginalNode();
|
|
597 |
} else if (result instanceof ConvertNode) {
|
|
598 |
ConvertNode convertNode = (ConvertNode) result;
|
|
599 |
if (convertNode.mayNullCheckSkipConversion()) {
|
|
600 |
result = convertNode.getValue();
|
|
601 |
} else {
|
|
602 |
break;
|
|
603 |
}
|
|
604 |
} else {
|
|
605 |
break;
|
|
606 |
}
|
|
607 |
}
|
|
608 |
return result;
|
|
609 |
}
|
|
610 |
|
43972
|
611 |
private static Block calcBlockForUsage(Node node, Node usage, Block startBlock, NodeMap<Block> currentNodeMap) {
|
|
612 |
assert !(node instanceof PhiNode);
|
|
613 |
Block currentBlock = startBlock;
|
|
614 |
if (usage instanceof PhiNode) {
|
|
615 |
// An input to a PhiNode is used at the end of the predecessor block that
|
|
616 |
// corresponds to the PhiNode input. One PhiNode can use an input multiple times.
|
|
617 |
PhiNode phi = (PhiNode) usage;
|
|
618 |
AbstractMergeNode merge = phi.merge();
|
|
619 |
Block mergeBlock = currentNodeMap.get(merge);
|
|
620 |
for (int i = 0; i < phi.valueCount(); ++i) {
|
|
621 |
if (phi.valueAt(i) == node) {
|
|
622 |
Block otherBlock = mergeBlock.getPredecessors()[i];
|
|
623 |
currentBlock = AbstractControlFlowGraph.commonDominatorTyped(currentBlock, otherBlock);
|
|
624 |
}
|
|
625 |
}
|
|
626 |
} else if (usage instanceof AbstractBeginNode) {
|
|
627 |
AbstractBeginNode abstractBeginNode = (AbstractBeginNode) usage;
|
|
628 |
if (abstractBeginNode instanceof StartNode) {
|
|
629 |
currentBlock = AbstractControlFlowGraph.commonDominatorTyped(currentBlock, currentNodeMap.get(abstractBeginNode));
|
|
630 |
} else {
|
|
631 |
Block otherBlock = currentNodeMap.get(abstractBeginNode).getDominator();
|
|
632 |
currentBlock = AbstractControlFlowGraph.commonDominatorTyped(currentBlock, otherBlock);
|
|
633 |
}
|
|
634 |
} else {
|
|
635 |
// All other types of usages: Put the input into the same block as the usage.
|
|
636 |
Block otherBlock = currentNodeMap.get(usage);
|
46344
|
637 |
if (usage instanceof ProxyNode) {
|
|
638 |
ProxyNode proxyNode = (ProxyNode) usage;
|
|
639 |
otherBlock = currentNodeMap.get(proxyNode.proxyPoint());
|
|
640 |
|
|
641 |
}
|
43972
|
642 |
currentBlock = AbstractControlFlowGraph.commonDominatorTyped(currentBlock, otherBlock);
|
|
643 |
}
|
|
644 |
return currentBlock;
|
|
645 |
}
|
|
646 |
|
46344
|
647 |
/**
|
|
648 |
* Micro block that is allocated for each fixed node and captures all floating nodes that
|
|
649 |
* need to be scheduled immediately after the corresponding fixed node.
|
|
650 |
*/
|
|
651 |
private static class MicroBlock {
|
|
652 |
private final int id;
|
|
653 |
private int nodeCount;
|
|
654 |
private NodeEntry head;
|
|
655 |
private NodeEntry tail;
|
|
656 |
|
|
657 |
MicroBlock(int id) {
|
|
658 |
this.id = id;
|
|
659 |
}
|
43972
|
660 |
|
46344
|
661 |
/**
|
|
662 |
* Adds a new floating node into the micro block.
|
|
663 |
*/
|
|
664 |
public void add(Node node) {
|
|
665 |
assert !(node instanceof FixedNode) : node;
|
48861
|
666 |
NodeEntry newTail = new NodeEntry(node);
|
46344
|
667 |
if (tail == null) {
|
|
668 |
tail = head = newTail;
|
|
669 |
} else {
|
|
670 |
tail.next = newTail;
|
|
671 |
tail = newTail;
|
|
672 |
}
|
|
673 |
nodeCount++;
|
|
674 |
}
|
43972
|
675 |
|
46344
|
676 |
/**
|
|
677 |
* Number of nodes in this micro block.
|
|
678 |
*/
|
|
679 |
public int getNodeCount() {
|
48861
|
680 |
assert getActualNodeCount() == nodeCount : getActualNodeCount() + " != " + nodeCount;
|
46344
|
681 |
return nodeCount;
|
|
682 |
}
|
|
683 |
|
48861
|
684 |
private int getActualNodeCount() {
|
|
685 |
int count = 0;
|
|
686 |
for (NodeEntry e = head; e != null; e = e.next) {
|
|
687 |
count++;
|
|
688 |
}
|
|
689 |
return count;
|
|
690 |
}
|
|
691 |
|
46344
|
692 |
/**
|
|
693 |
* The id of the micro block, with a block always associated with a lower id than its
|
|
694 |
* successors.
|
|
695 |
*/
|
|
696 |
public int getId() {
|
|
697 |
return id;
|
|
698 |
}
|
43972
|
699 |
|
46344
|
700 |
/**
|
|
701 |
* First node of the linked list of nodes of this micro block.
|
|
702 |
*/
|
|
703 |
public NodeEntry getFirstNode() {
|
|
704 |
return head;
|
|
705 |
}
|
|
706 |
|
|
707 |
/**
|
|
708 |
* Takes all nodes in this micro blocks and prepends them to the nodes of the given
|
|
709 |
* parameter.
|
|
710 |
*
|
|
711 |
* @param newBlock the new block for the nodes
|
|
712 |
*/
|
|
713 |
public void prependChildrenTo(MicroBlock newBlock) {
|
|
714 |
if (tail != null) {
|
48861
|
715 |
assert head != null;
|
46344
|
716 |
tail.next = newBlock.head;
|
|
717 |
newBlock.head = head;
|
|
718 |
head = tail = null;
|
|
719 |
newBlock.nodeCount += nodeCount;
|
|
720 |
nodeCount = 0;
|
43972
|
721 |
}
|
|
722 |
}
|
|
723 |
|
46344
|
724 |
@Override
|
|
725 |
public String toString() {
|
|
726 |
return String.format("MicroBlock[id=%d]", id);
|
|
727 |
}
|
48861
|
728 |
|
|
729 |
@Override
|
|
730 |
public int hashCode() {
|
|
731 |
return id;
|
|
732 |
}
|
46344
|
733 |
}
|
|
734 |
|
|
735 |
/**
|
|
736 |
* Entry in the linked list of nodes.
|
|
737 |
*/
|
|
738 |
private static class NodeEntry {
|
|
739 |
private final Node node;
|
|
740 |
private NodeEntry next;
|
|
741 |
|
48861
|
742 |
NodeEntry(Node node) {
|
46344
|
743 |
this.node = node;
|
48861
|
744 |
this.next = null;
|
46344
|
745 |
}
|
|
746 |
|
|
747 |
public NodeEntry getNext() {
|
|
748 |
return next;
|
|
749 |
}
|
|
750 |
|
|
751 |
public Node getNode() {
|
|
752 |
return node;
|
|
753 |
}
|
|
754 |
}
|
|
755 |
|
48861
|
756 |
private void scheduleEarliestIterative(BlockMap<List<Node>> blockToNodes, NodeMap<Block> nodeToBlock, NodeBitMap visited, StructuredGraph graph, boolean immutableGraph,
|
|
757 |
boolean withGuardOrder) {
|
46344
|
758 |
|
|
759 |
NodeMap<MicroBlock> entries = graph.createNodeMap();
|
43972
|
760 |
NodeStack stack = new NodeStack();
|
|
761 |
|
46344
|
762 |
// Initialize with fixed nodes.
|
|
763 |
MicroBlock startBlock = null;
|
|
764 |
int nextId = 1;
|
|
765 |
for (Block b : cfg.reversePostOrder()) {
|
48861
|
766 |
for (FixedNode current : b.getBeginNode().getBlockNodes()) {
|
46344
|
767 |
MicroBlock microBlock = new MicroBlock(nextId++);
|
48861
|
768 |
entries.set(current, microBlock);
|
|
769 |
boolean isNew = visited.checkAndMarkInc(current);
|
|
770 |
assert isNew;
|
46344
|
771 |
if (startBlock == null) {
|
|
772 |
startBlock = microBlock;
|
|
773 |
}
|
43972
|
774 |
}
|
|
775 |
}
|
|
776 |
|
48861
|
777 |
if (graph.getGuardsStage().allowsFloatingGuards() && graph.getNodes(GuardNode.TYPE).isNotEmpty()) {
|
|
778 |
// Now process guards.
|
|
779 |
if (GuardPriorities.getValue(graph.getOptions()) && withGuardOrder) {
|
|
780 |
EnumMap<GuardPriority, List<GuardNode>> guardsByPriority = new EnumMap<>(GuardPriority.class);
|
|
781 |
for (GuardNode guard : graph.getNodes(GuardNode.TYPE)) {
|
|
782 |
guardsByPriority.computeIfAbsent(guard.computePriority(), p -> new ArrayList<>()).add(guard);
|
|
783 |
}
|
|
784 |
// `EnumMap.values` returns values in "natural" key order
|
|
785 |
for (List<GuardNode> guards : guardsByPriority.values()) {
|
|
786 |
processNodes(visited, entries, stack, startBlock, guards);
|
|
787 |
}
|
|
788 |
GuardOrder.resortGuards(graph, entries, stack);
|
|
789 |
} else {
|
|
790 |
processNodes(visited, entries, stack, startBlock, graph.getNodes(GuardNode.TYPE));
|
46344
|
791 |
}
|
48861
|
792 |
} else {
|
|
793 |
assert graph.getNodes(GuardNode.TYPE).isEmpty();
|
46344
|
794 |
}
|
43972
|
795 |
|
46344
|
796 |
// Now process inputs of fixed nodes.
|
|
797 |
for (Block b : cfg.reversePostOrder()) {
|
48861
|
798 |
for (FixedNode current : b.getBeginNode().getBlockNodes()) {
|
|
799 |
processNodes(visited, entries, stack, startBlock, current.inputs());
|
43972
|
800 |
}
|
46344
|
801 |
}
|
|
802 |
|
|
803 |
if (visited.getCounter() < graph.getNodeCount()) {
|
|
804 |
// Visit back input edges of loop phis.
|
|
805 |
boolean changed;
|
|
806 |
boolean unmarkedPhi;
|
|
807 |
do {
|
|
808 |
changed = false;
|
|
809 |
unmarkedPhi = false;
|
|
810 |
for (LoopBeginNode loopBegin : graph.getNodes(LoopBeginNode.TYPE)) {
|
|
811 |
for (PhiNode phi : loopBegin.phis()) {
|
|
812 |
if (visited.isMarked(phi)) {
|
|
813 |
for (int i = 0; i < loopBegin.getLoopEndCount(); ++i) {
|
|
814 |
Node node = phi.valueAt(i + loopBegin.forwardEndCount());
|
|
815 |
if (node != null && entries.get(node) == null) {
|
|
816 |
changed = true;
|
|
817 |
processStack(node, startBlock, entries, visited, stack);
|
|
818 |
}
|
|
819 |
}
|
|
820 |
} else {
|
|
821 |
unmarkedPhi = true;
|
|
822 |
}
|
|
823 |
}
|
|
824 |
}
|
|
825 |
|
|
826 |
/*
|
|
827 |
* the processing of one loop phi could have marked a previously checked loop
|
|
828 |
* phi, therefore this needs to be iterative.
|
|
829 |
*/
|
|
830 |
} while (unmarkedPhi && changed);
|
|
831 |
}
|
43972
|
832 |
|
|
833 |
// Check for dead nodes.
|
|
834 |
if (!immutableGraph && visited.getCounter() < graph.getNodeCount()) {
|
|
835 |
for (Node n : graph.getNodes()) {
|
|
836 |
if (!visited.isMarked(n)) {
|
|
837 |
n.clearInputs();
|
|
838 |
n.markDeleted();
|
|
839 |
}
|
|
840 |
}
|
|
841 |
}
|
|
842 |
|
46344
|
843 |
for (Block b : cfg.reversePostOrder()) {
|
|
844 |
FixedNode fixedNode = b.getEndNode();
|
|
845 |
if (fixedNode instanceof ControlSplitNode) {
|
|
846 |
ControlSplitNode controlSplitNode = (ControlSplitNode) fixedNode;
|
|
847 |
MicroBlock endBlock = entries.get(fixedNode);
|
48861
|
848 |
AbstractBeginNode primarySuccessor = controlSplitNode.getPrimarySuccessor();
|
|
849 |
if (primarySuccessor != null) {
|
|
850 |
endBlock.prependChildrenTo(entries.get(primarySuccessor));
|
|
851 |
} else {
|
|
852 |
assert endBlock.tail == null;
|
|
853 |
}
|
43972
|
854 |
}
|
|
855 |
}
|
|
856 |
|
48861
|
857 |
// Create lists for each block
|
46344
|
858 |
for (Block b : cfg.reversePostOrder()) {
|
48861
|
859 |
// Count nodes in block
|
46344
|
860 |
int totalCount = 0;
|
48861
|
861 |
for (FixedNode current : b.getBeginNode().getBlockNodes()) {
|
46344
|
862 |
MicroBlock microBlock = entries.get(current);
|
|
863 |
totalCount += microBlock.getNodeCount() + 1;
|
|
864 |
}
|
|
865 |
|
|
866 |
// Initialize with begin node, it is always the first node.
|
|
867 |
ArrayList<Node> nodes = new ArrayList<>(totalCount);
|
|
868 |
blockToNodes.put(b, nodes);
|
|
869 |
|
48861
|
870 |
for (FixedNode current : b.getBeginNode().getBlockNodes()) {
|
46344
|
871 |
MicroBlock microBlock = entries.get(current);
|
|
872 |
nodeToBlock.set(current, b);
|
|
873 |
nodes.add(current);
|
|
874 |
NodeEntry next = microBlock.getFirstNode();
|
|
875 |
while (next != null) {
|
|
876 |
Node nextNode = next.getNode();
|
|
877 |
nodeToBlock.set(nextNode, b);
|
|
878 |
nodes.add(nextNode);
|
|
879 |
next = next.getNext();
|
|
880 |
}
|
43972
|
881 |
}
|
|
882 |
}
|
|
883 |
|
46762
|
884 |
assert (!Assertions.detailedAssertionsEnabled(cfg.graph.getOptions())) || MemoryScheduleVerification.check(cfg.getStartBlock(), blockToNodes);
|
43972
|
885 |
}
|
|
886 |
|
48861
|
887 |
private static void processNodes(NodeBitMap visited, NodeMap<MicroBlock> entries, NodeStack stack, MicroBlock startBlock, Iterable<? extends Node> nodes) {
|
|
888 |
for (Node node : nodes) {
|
|
889 |
if (entries.get(node) == null) {
|
|
890 |
processStack(node, startBlock, entries, visited, stack);
|
|
891 |
}
|
|
892 |
}
|
|
893 |
}
|
|
894 |
|
46344
|
895 |
private static void processStackPhi(NodeStack stack, PhiNode phiNode, NodeMap<MicroBlock> nodeToBlock, NodeBitMap visited) {
|
|
896 |
stack.pop();
|
|
897 |
if (visited.checkAndMarkInc(phiNode)) {
|
|
898 |
MicroBlock mergeBlock = nodeToBlock.get(phiNode.merge());
|
|
899 |
assert mergeBlock != null : phiNode;
|
|
900 |
nodeToBlock.set(phiNode, mergeBlock);
|
|
901 |
AbstractMergeNode merge = phiNode.merge();
|
|
902 |
for (int i = 0; i < merge.forwardEndCount(); ++i) {
|
|
903 |
Node input = phiNode.valueAt(i);
|
|
904 |
if (input != null && nodeToBlock.get(input) == null) {
|
|
905 |
stack.push(input);
|
43972
|
906 |
}
|
|
907 |
}
|
|
908 |
}
|
|
909 |
}
|
|
910 |
|
46344
|
911 |
private static void processStackProxy(NodeStack stack, ProxyNode proxyNode, NodeMap<MicroBlock> nodeToBlock, NodeBitMap visited) {
|
|
912 |
stack.pop();
|
|
913 |
if (visited.checkAndMarkInc(proxyNode)) {
|
|
914 |
nodeToBlock.set(proxyNode, nodeToBlock.get(proxyNode.proxyPoint()));
|
|
915 |
Node input = proxyNode.value();
|
|
916 |
if (input != null && nodeToBlock.get(input) == null) {
|
43972
|
917 |
stack.push(input);
|
|
918 |
}
|
|
919 |
}
|
|
920 |
}
|
|
921 |
|
46344
|
922 |
private static void processStack(Node first, MicroBlock startBlock, NodeMap<MicroBlock> nodeToMicroBlock, NodeBitMap visited, NodeStack stack) {
|
|
923 |
assert stack.isEmpty();
|
|
924 |
assert !visited.isMarked(first);
|
|
925 |
stack.push(first);
|
|
926 |
Node current = first;
|
|
927 |
while (true) {
|
|
928 |
if (current instanceof PhiNode) {
|
|
929 |
processStackPhi(stack, (PhiNode) current, nodeToMicroBlock, visited);
|
|
930 |
} else if (current instanceof ProxyNode) {
|
|
931 |
processStackProxy(stack, (ProxyNode) current, nodeToMicroBlock, visited);
|
|
932 |
} else {
|
|
933 |
MicroBlock currentBlock = nodeToMicroBlock.get(current);
|
|
934 |
if (currentBlock == null) {
|
|
935 |
MicroBlock earliestBlock = processInputs(nodeToMicroBlock, stack, startBlock, current);
|
|
936 |
if (earliestBlock == null) {
|
|
937 |
// We need to delay until inputs are processed.
|
|
938 |
} else {
|
|
939 |
// Can immediately process and pop.
|
|
940 |
stack.pop();
|
|
941 |
visited.checkAndMarkInc(current);
|
|
942 |
nodeToMicroBlock.set(current, earliestBlock);
|
|
943 |
earliestBlock.add(current);
|
|
944 |
}
|
|
945 |
} else {
|
|
946 |
stack.pop();
|
|
947 |
}
|
43972
|
948 |
}
|
46344
|
949 |
|
|
950 |
if (stack.isEmpty()) {
|
|
951 |
break;
|
|
952 |
}
|
|
953 |
current = stack.peek();
|
43972
|
954 |
}
|
|
955 |
}
|
|
956 |
|
48861
|
957 |
private static class GuardOrder {
|
|
958 |
/**
|
|
959 |
* After an earliest schedule, this will re-sort guards to honor their
|
|
960 |
* {@linkplain StaticDeoptimizingNode#computePriority() priority}.
|
|
961 |
*
|
|
962 |
* Note that this only changes the order of nodes within {@linkplain MicroBlock
|
|
963 |
* micro-blocks}, nodes will not be moved from one micro-block to another.
|
|
964 |
*/
|
|
965 |
private static void resortGuards(StructuredGraph graph, NodeMap<MicroBlock> entries, NodeStack stack) {
|
|
966 |
assert stack.isEmpty();
|
|
967 |
EconomicSet<MicroBlock> blocksWithGuards = EconomicSet.create(IDENTITY);
|
|
968 |
for (GuardNode guard : graph.getNodes(GuardNode.TYPE)) {
|
|
969 |
MicroBlock block = entries.get(guard);
|
|
970 |
assert block != null : guard + "should already be scheduled to a micro-block";
|
|
971 |
blocksWithGuards.add(block);
|
|
972 |
}
|
|
973 |
assert !blocksWithGuards.isEmpty();
|
|
974 |
NodeMap<GuardPriority> priorities = graph.createNodeMap();
|
|
975 |
NodeBitMap blockNodes = graph.createNodeBitMap();
|
|
976 |
for (MicroBlock block : blocksWithGuards) {
|
|
977 |
MicroBlock newBlock = resortGuards(block, stack, blockNodes, priorities);
|
|
978 |
assert stack.isEmpty();
|
|
979 |
assert blockNodes.isEmpty();
|
|
980 |
if (newBlock != null) {
|
|
981 |
assert block.getNodeCount() == newBlock.getNodeCount();
|
|
982 |
block.head = newBlock.head;
|
|
983 |
block.tail = newBlock.tail;
|
|
984 |
}
|
|
985 |
}
|
|
986 |
}
|
|
987 |
|
|
988 |
/**
|
|
989 |
* This resorts guards within one micro-block.
|
|
990 |
*
|
|
991 |
* {@code stack}, {@code blockNodes} and {@code priorities} are just temporary
|
|
992 |
* data-structures which are allocated once by the callers of this method. They should
|
|
993 |
* be in their "initial"/"empty" state when calling this method and when it returns.
|
|
994 |
*/
|
|
995 |
private static MicroBlock resortGuards(MicroBlock block, NodeStack stack, NodeBitMap blockNodes, NodeMap<GuardPriority> priorities) {
|
|
996 |
if (!propagatePriority(block, stack, priorities, blockNodes)) {
|
|
997 |
return null;
|
|
998 |
}
|
|
999 |
|
|
1000 |
Function<GuardNode, GuardPriority> transitiveGuardPriorityGetter = priorities::get;
|
|
1001 |
Comparator<GuardNode> globalGuardPriorityComparator = Comparator.comparing(transitiveGuardPriorityGetter).thenComparing(GuardNode::computePriority).thenComparingInt(Node::hashCode);
|
|
1002 |
|
|
1003 |
SortedSet<GuardNode> availableGuards = new TreeSet<>(globalGuardPriorityComparator);
|
|
1004 |
MicroBlock newBlock = new MicroBlock(block.getId());
|
|
1005 |
|
|
1006 |
NodeBitMap sorted = blockNodes;
|
|
1007 |
sorted.invert();
|
|
1008 |
|
|
1009 |
for (NodeEntry e = block.head; e != null; e = e.next) {
|
|
1010 |
checkIfAvailable(e.node, stack, sorted, newBlock, availableGuards, false);
|
|
1011 |
}
|
|
1012 |
do {
|
|
1013 |
while (!stack.isEmpty()) {
|
|
1014 |
checkIfAvailable(stack.pop(), stack, sorted, newBlock, availableGuards, true);
|
|
1015 |
}
|
|
1016 |
Iterator<GuardNode> iterator = availableGuards.iterator();
|
|
1017 |
if (iterator.hasNext()) {
|
|
1018 |
addNodeToResort(iterator.next(), stack, sorted, newBlock, true);
|
|
1019 |
iterator.remove();
|
|
1020 |
}
|
|
1021 |
} while (!stack.isEmpty() || !availableGuards.isEmpty());
|
|
1022 |
|
|
1023 |
blockNodes.clearAll();
|
|
1024 |
return newBlock;
|
|
1025 |
}
|
|
1026 |
|
|
1027 |
/**
|
|
1028 |
* This checks if {@code n} can be scheduled, if it is the case, it schedules it now by
|
|
1029 |
* calling {@link #addNodeToResort(Node, NodeStack, NodeBitMap, MicroBlock, boolean)}.
|
|
1030 |
*/
|
|
1031 |
private static void checkIfAvailable(Node n, NodeStack stack, NodeBitMap sorted, Instance.MicroBlock newBlock, SortedSet<GuardNode> availableGuardNodes, boolean pushUsages) {
|
|
1032 |
if (sorted.isMarked(n)) {
|
|
1033 |
return;
|
|
1034 |
}
|
|
1035 |
for (Node in : n.inputs()) {
|
|
1036 |
if (!sorted.isMarked(in)) {
|
|
1037 |
return;
|
|
1038 |
}
|
|
1039 |
}
|
|
1040 |
if (n instanceof GuardNode) {
|
|
1041 |
availableGuardNodes.add((GuardNode) n);
|
|
1042 |
} else {
|
|
1043 |
addNodeToResort(n, stack, sorted, newBlock, pushUsages);
|
|
1044 |
}
|
|
1045 |
}
|
|
1046 |
|
|
1047 |
/**
|
|
1048 |
* Add a node to the re-sorted micro-block. This also pushes nodes that need to be
|
|
1049 |
* (re-)examined on the stack.
|
|
1050 |
*/
|
|
1051 |
private static void addNodeToResort(Node n, NodeStack stack, NodeBitMap sorted, MicroBlock newBlock, boolean pushUsages) {
|
|
1052 |
sorted.mark(n);
|
|
1053 |
newBlock.add(n);
|
|
1054 |
if (pushUsages) {
|
|
1055 |
for (Node u : n.usages()) {
|
|
1056 |
if (!sorted.isMarked(u)) {
|
|
1057 |
stack.push(u);
|
|
1058 |
}
|
|
1059 |
}
|
|
1060 |
}
|
|
1061 |
}
|
|
1062 |
|
|
1063 |
/**
|
|
1064 |
* This fills in a map of transitive priorities ({@code priorities}). It also marks the
|
|
1065 |
* nodes from this micro-block in {@code blockNodes}.
|
|
1066 |
*
|
|
1067 |
* The transitive priority of a guard is the highest of its priority and the priority of
|
|
1068 |
* the guards that depend on it (transitively).
|
|
1069 |
*
|
|
1070 |
* This method returns {@code false} if no re-ordering is necessary in this micro-block.
|
|
1071 |
*/
|
|
1072 |
private static boolean propagatePriority(MicroBlock block, NodeStack stack, NodeMap<GuardPriority> priorities, NodeBitMap blockNodes) {
|
|
1073 |
assert stack.isEmpty();
|
|
1074 |
assert blockNodes.isEmpty();
|
|
1075 |
GuardPriority lowestPriority = GuardPriority.highest();
|
|
1076 |
for (NodeEntry e = block.head; e != null; e = e.next) {
|
|
1077 |
blockNodes.mark(e.node);
|
|
1078 |
if (e.node instanceof GuardNode) {
|
|
1079 |
GuardNode guard = (GuardNode) e.node;
|
|
1080 |
GuardPriority priority = guard.computePriority();
|
|
1081 |
if (lowestPriority != null) {
|
|
1082 |
if (priority.isLowerPriorityThan(lowestPriority)) {
|
|
1083 |
lowestPriority = priority;
|
|
1084 |
} else if (priority.isHigherPriorityThan(lowestPriority)) {
|
|
1085 |
lowestPriority = null;
|
|
1086 |
}
|
|
1087 |
}
|
|
1088 |
stack.push(guard);
|
|
1089 |
priorities.set(guard, priority);
|
|
1090 |
}
|
|
1091 |
}
|
|
1092 |
if (lowestPriority != null) {
|
|
1093 |
stack.clear();
|
|
1094 |
blockNodes.clearAll();
|
|
1095 |
return false;
|
|
1096 |
}
|
|
1097 |
|
|
1098 |
do {
|
|
1099 |
Node current = stack.pop();
|
|
1100 |
assert blockNodes.isMarked(current);
|
|
1101 |
GuardPriority priority = priorities.get(current);
|
|
1102 |
for (Node input : current.inputs()) {
|
|
1103 |
if (!blockNodes.isMarked(input)) {
|
|
1104 |
continue;
|
|
1105 |
}
|
|
1106 |
GuardPriority inputPriority = priorities.get(input);
|
|
1107 |
if (inputPriority == null || inputPriority.isLowerPriorityThan(priority)) {
|
|
1108 |
priorities.set(input, priority);
|
|
1109 |
stack.push(input);
|
|
1110 |
}
|
|
1111 |
}
|
|
1112 |
} while (!stack.isEmpty());
|
|
1113 |
return true;
|
|
1114 |
}
|
|
1115 |
}
|
|
1116 |
|
46344
|
1117 |
/**
|
|
1118 |
* Processes the inputs of given block. Pushes unprocessed inputs onto the stack. Returns
|
|
1119 |
* null if there were still unprocessed inputs, otherwise returns the earliest block given
|
|
1120 |
* node can be scheduled in.
|
|
1121 |
*/
|
|
1122 |
private static MicroBlock processInputs(NodeMap<MicroBlock> nodeToBlock, NodeStack stack, MicroBlock startBlock, Node current) {
|
|
1123 |
if (current.getNodeClass().isLeafNode()) {
|
|
1124 |
return startBlock;
|
|
1125 |
}
|
|
1126 |
|
|
1127 |
MicroBlock earliestBlock = startBlock;
|
|
1128 |
for (Node input : current.inputs()) {
|
|
1129 |
MicroBlock inputBlock = nodeToBlock.get(input);
|
|
1130 |
if (inputBlock == null) {
|
|
1131 |
earliestBlock = null;
|
43972
|
1132 |
stack.push(input);
|
48861
|
1133 |
} else if (earliestBlock != null && inputBlock.getId() > earliestBlock.getId()) {
|
46344
|
1134 |
earliestBlock = inputBlock;
|
43972
|
1135 |
}
|
|
1136 |
}
|
46344
|
1137 |
return earliestBlock;
|
|
1138 |
}
|
|
1139 |
|
|
1140 |
private static boolean isFixedEnd(FixedNode endNode) {
|
|
1141 |
return endNode instanceof ControlSplitNode || endNode instanceof ControlSinkNode || endNode instanceof AbstractEndNode;
|
43972
|
1142 |
}
|
|
1143 |
|
|
1144 |
public String printScheduleHelper(String desc) {
|
|
1145 |
Formatter buf = new Formatter();
|
|
1146 |
buf.format("=== %s / %s ===%n", getCFG().getStartBlock().getBeginNode().graph(), desc);
|
|
1147 |
for (Block b : getCFG().getBlocks()) {
|
|
1148 |
buf.format("==== b: %s (loopDepth: %s). ", b, b.getLoopDepth());
|
|
1149 |
buf.format("dom: %s. ", b.getDominator());
|
|
1150 |
buf.format("preds: %s. ", Arrays.toString(b.getPredecessors()));
|
|
1151 |
buf.format("succs: %s ====%n", Arrays.toString(b.getSuccessors()));
|
|
1152 |
|
|
1153 |
if (blockToNodesMap.get(b) != null) {
|
|
1154 |
for (Node n : nodesFor(b)) {
|
|
1155 |
printNode(n);
|
|
1156 |
}
|
|
1157 |
} else {
|
|
1158 |
for (Node n : b.getNodes()) {
|
|
1159 |
printNode(n);
|
|
1160 |
}
|
|
1161 |
}
|
|
1162 |
}
|
|
1163 |
buf.format("%n");
|
|
1164 |
return buf.toString();
|
|
1165 |
}
|
|
1166 |
|
|
1167 |
private static void printNode(Node n) {
|
|
1168 |
Formatter buf = new Formatter();
|
|
1169 |
buf.format("%s", n);
|
|
1170 |
if (n instanceof MemoryCheckpoint.Single) {
|
|
1171 |
buf.format(" // kills %s", ((MemoryCheckpoint.Single) n).getLocationIdentity());
|
|
1172 |
} else if (n instanceof MemoryCheckpoint.Multi) {
|
|
1173 |
buf.format(" // kills ");
|
|
1174 |
for (LocationIdentity locid : ((MemoryCheckpoint.Multi) n).getLocationIdentities()) {
|
|
1175 |
buf.format("%s, ", locid);
|
|
1176 |
}
|
|
1177 |
} else if (n instanceof FloatingReadNode) {
|
|
1178 |
FloatingReadNode frn = (FloatingReadNode) n;
|
|
1179 |
buf.format(" // from %s", frn.getLocationIdentity());
|
|
1180 |
buf.format(", lastAccess: %s", frn.getLastLocationAccess());
|
|
1181 |
buf.format(", address: %s", frn.getAddress());
|
|
1182 |
} else if (n instanceof GuardNode) {
|
|
1183 |
buf.format(", anchor: %s", ((GuardNode) n).getAnchor());
|
|
1184 |
}
|
46640
|
1185 |
n.getDebug().log("%s", buf);
|
43972
|
1186 |
}
|
|
1187 |
|
|
1188 |
public ControlFlowGraph getCFG() {
|
|
1189 |
return cfg;
|
|
1190 |
}
|
|
1191 |
|
|
1192 |
/**
|
|
1193 |
* Gets the nodes in a given block.
|
|
1194 |
*/
|
|
1195 |
public List<Node> nodesFor(Block block) {
|
|
1196 |
return blockToNodesMap.get(block);
|
|
1197 |
}
|
|
1198 |
}
|
|
1199 |
|
|
1200 |
}
|