comparison graal/com.oracle.graal.compiler.phases/src/com/oracle/graal/compiler/phases/TailDuplicationPhase.java @ 6411:c5afcc2ebd3d

change of project structure: separate compiler and LIR, put EA into separate project
author Lukas Stadler <lukas.stadler@jku.at>
date Mon, 17 Sep 2012 16:08:46 +0200
parents graal/com.oracle.graal.compiler/src/com/oracle/graal/compiler/phases/TailDuplicationPhase.java@1cb45c7dba55
children 4bd8711d824a
comparison
equal deleted inserted replaced
6410:debe42b2b92f 6411:c5afcc2ebd3d
1 /*
2 * Copyright (c) 2012, 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 com.oracle.graal.compiler.phases;
24
25 import java.util.*;
26
27 import com.oracle.graal.compiler.*;
28 import com.oracle.graal.debug.*;
29 import com.oracle.graal.graph.Graph.DuplicationReplacement;
30 import com.oracle.graal.graph.*;
31 import com.oracle.graal.nodes.*;
32 import com.oracle.graal.nodes.VirtualState.NodeClosure;
33 import com.oracle.graal.nodes.extended.*;
34 import com.oracle.graal.nodes.java.*;
35 import com.oracle.graal.nodes.type.*;
36 import com.oracle.graal.nodes.util.*;
37
38 /**
39 * This class is a phase that looks for opportunities for tail duplication. The static method
40 * {@link #tailDuplicate(MergeNode, TailDuplicationDecision, List)} can also be used to drive tail duplication from
41 * other places, e.g., inlining.
42 */
43 public class TailDuplicationPhase extends Phase {
44
45 /*
46 * Various metrics on the circumstances in which tail duplication was/wasn't performed.
47 */
48 private static final DebugMetric metricDuplicationMonitors = Debug.metric("DuplicationMonitors");
49 private static final DebugMetric metricDuplicationEnd = Debug.metric("DuplicationEnd");
50 private static final DebugMetric metricDuplicationEndPerformed = Debug.metric("DuplicationEndPerformed");
51 private static final DebugMetric metricDuplicationOther = Debug.metric("DuplicationOther");
52 private static final DebugMetric metricDuplicationOtherPerformed = Debug.metric("DuplicationOtherPerformed");
53
54 /**
55 * This interface is used by tail duplication to let clients decide if tail duplication should be performed.
56 */
57 public interface TailDuplicationDecision {
58
59 /**
60 * Queries if tail duplication should be performed at the given merge. If this method returns true then the tail
61 * duplication will be performed, because all other checks have happened before.
62 *
63 * @param merge The merge at which tail duplication can be performed.
64 * @param fixedNodeCount The size of the set of fixed nodes that forms the base for the duplicated set of nodes.
65 * @return true if the tail duplication should be performed, false otherwise.
66 */
67 boolean doTransform(MergeNode merge, int fixedNodeCount);
68 }
69
70 /**
71 * A tail duplication decision closure that employs the default algorithm: Check if there are any phis on the merge
72 * whose stamps improve and that have usages within the duplicated set of fixed nodes.
73 */
74 public static final TailDuplicationDecision DEFAULT_DECISION = new TailDuplicationDecision() {
75
76 public boolean doTransform(MergeNode merge, int fixedNodeCount) {
77 if (fixedNodeCount < GraalOptions.TailDuplicationTrivialSize) {
78 return true;
79 }
80 HashSet<PhiNode> improvements = new HashSet<>();
81 for (PhiNode phi : merge.phis()) {
82 Stamp phiStamp = phi.stamp();
83 for (ValueNode input : phi.values()) {
84 if (!input.stamp().equals(phiStamp)) {
85 improvements.add(phi);
86 break;
87 }
88 }
89 }
90 if (improvements.isEmpty()) {
91 return false;
92 }
93 FixedNode current = merge;
94 int opportunities = 0;
95 while (current instanceof FixedWithNextNode) {
96 current = ((FixedWithNextNode) current).next();
97 for (PhiNode phi : improvements) {
98 if (current.inputs().contains(phi)) {
99 opportunities++;
100 }
101 }
102 }
103 return opportunities > 0;
104 }
105 };
106
107 /**
108 * A tail duplication decision closure that always returns true.
109 */
110 public static final TailDuplicationDecision TRUE_DECISION = new TailDuplicationDecision() {
111
112 @Override
113 public boolean doTransform(MergeNode merge, int fixedNodeCount) {
114 return true;
115 }
116 };
117
118 @Override
119 protected void run(StructuredGraph graph) {
120 // A snapshot is taken here, so that new MergeNode instances aren't considered for tail duplication.
121 for (MergeNode merge : graph.getNodes(MergeNode.class).snapshot()) {
122 if (!(merge instanceof LoopBeginNode) && merge.probability() >= GraalOptions.TailDuplicationProbability) {
123 tailDuplicate(merge, DEFAULT_DECISION, null);
124 }
125 }
126 }
127
128 /**
129 * This method attempts to duplicate the tail of the given merge. The merge must not be a {@link LoopBeginNode}. If
130 * the merge is eligible for duplication (at least one fixed node in its tail, no {@link MonitorEnterNode}/
131 * {@link MonitorExitNode}, non-null {@link MergeNode#stateAfter()}) then the decision callback is used to determine
132 * whether the tail duplication should actually be performed. If replacements is non-null, then this list of
133 * {@link PiNode}s is used to replace one value per merge end.
134 *
135 * @param merge The merge whose tail should be duplicated.
136 * @param decision A callback that can make the final decision if tail duplication should occur or not.
137 * @param replacements A list of {@link PiNode}s, or null. If this list is non-null then its size needs to match the
138 * merge's end count. Each entry can either be null or a {@link PiNode}, and is used to replace
139 * {@link PiNode#object()} with the {@link PiNode} in the duplicated branch that corresponds to the
140 * entry.
141 */
142 public static void tailDuplicate(MergeNode merge, TailDuplicationDecision decision, List<PiNode> replacements) {
143 assert !(merge instanceof LoopBeginNode);
144 assert replacements == null || replacements.size() == merge.forwardEndCount();
145 FixedNode fixed = merge;
146 int fixedCount = 0;
147 boolean containsMonitor = false;
148 while (fixed instanceof FixedWithNextNode) {
149 if (fixed instanceof MonitorEnterNode || fixed instanceof MonitorExitNode) {
150 containsMonitor = true;
151 }
152 fixed = ((FixedWithNextNode) fixed).next();
153 fixedCount++;
154 }
155 if (containsMonitor) {
156 // cannot currently be handled
157 // TODO (ls) re-evaluate this limitation after changes to the lock representation and the LIR generator
158 metricDuplicationMonitors.increment();
159 } else if (fixedCount > 1) {
160 if (fixed instanceof EndNode && !(((EndNode) fixed).merge() instanceof LoopBeginNode)) {
161 metricDuplicationEnd.increment();
162 if (decision.doTransform(merge, fixedCount)) {
163 metricDuplicationEndPerformed.increment();
164 new DuplicationOperation(merge, replacements).duplicate();
165 }
166 } else if (merge.stateAfter() != null) {
167 metricDuplicationOther.increment();
168 if (decision.doTransform(merge, fixedCount)) {
169 metricDuplicationOtherPerformed.increment();
170 new DuplicationOperation(merge, replacements).duplicate();
171 }
172 }
173 }
174 }
175
176 /**
177 * This class encapsulates one tail duplication operation on a specific {@link MergeNode}.
178 */
179 private static class DuplicationOperation {
180
181 private final MergeNode merge;
182 private final StructuredGraph graph;
183
184 private final HashMap<ValueNode, PhiNode> bottomPhis = new HashMap<>();
185 private final List<PiNode> replacements;
186
187 /**
188 * Initializes the tail duplication operation without actually performing any work.
189 *
190 * @param merge The merge whose tail should be duplicated.
191 * @param replacements A list of replacement {@link PiNode}s, or null. If this is non-null, then the size of the
192 * list needs to match the number of end nodes at the merge.
193 */
194 public DuplicationOperation(MergeNode merge, List<PiNode> replacements) {
195 this.merge = merge;
196 this.replacements = replacements;
197 this.graph = (StructuredGraph) merge.graph();
198 }
199
200 /**
201 * Performs the actual tail duplication:
202 * <ul>
203 * <li>Creates a new {@link ValueAnchorNode} at the beginning of the duplicated area, an transfers all
204 * dependencies from the merge to this anchor.</li>
205 * <li>Determines the set of fixed nodes to be duplicated.</li>
206 * <li>Creates the new merge at the bottom of the duplicated area.</li>
207 * <li>Determines the complete set of duplicated nodes.</li>
208 * <li>Performs the actual duplication.</li>
209 * </ul>
210 */
211 private void duplicate() {
212 Debug.log("tail duplication at merge %s in %s (prob %f)", merge, graph.method(), merge.probability());
213
214 ValueAnchorNode anchor = addValueAnchor();
215
216 // determine the fixed nodes that should be duplicated (currently: all nodes up until the first control
217 // split, end node, deopt or return.
218 ArrayList<FixedNode> fixedNodes = new ArrayList<>();
219 FixedNode fixed = merge.next();
220 FrameState stateAfter = merge.stateAfter();
221 while (fixed instanceof FixedWithNextNode) {
222 fixedNodes.add(fixed);
223 if (fixed instanceof StateSplit && ((StateSplit) fixed).stateAfter() != null) {
224 stateAfter = ((StateSplit) fixed).stateAfter();
225 }
226 fixed = ((FixedWithNextNode) fixed).next();
227 }
228
229 EndNode endAfter = createNewMerge(fixed, stateAfter);
230 MergeNode mergeAfter = endAfter.merge();
231 fixedNodes.add(endAfter);
232 final HashSet<Node> duplicatedNodes = buildDuplicatedNodeSet(fixedNodes, stateAfter);
233 mergeAfter.clearEnds();
234 expandDuplicated(duplicatedNodes, mergeAfter);
235 retargetDependencies(duplicatedNodes, anchor);
236
237 List<EndNode> endSnapshot = merge.forwardEnds().snapshot();
238 List<PhiNode> phiSnapshot = merge.phis().snapshot();
239
240 int endIndex = 0;
241 for (final EndNode forwardEnd : merge.forwardEnds()) {
242 Map<Node, Node> duplicates;
243 if (replacements == null || replacements.get(endIndex) == null) {
244 duplicates = graph.addDuplicates(duplicatedNodes, (DuplicationReplacement) null);
245 } else {
246 HashMap<Node, Node> replace = new HashMap<>();
247 replace.put(replacements.get(endIndex).object(), replacements.get(endIndex));
248 duplicates = graph.addDuplicates(duplicatedNodes, replace);
249 }
250 for (Map.Entry<ValueNode, PhiNode> phi : bottomPhis.entrySet()) {
251 phi.getValue().initializeValueAt(merge.forwardEndIndex(forwardEnd), (ValueNode) duplicates.get(phi.getKey()));
252 }
253 mergeAfter.addForwardEnd((EndNode) duplicates.get(endAfter));
254
255 // re-wire the duplicated ValueAnchorNode to the predecessor of the corresponding EndNode
256 FixedNode anchorDuplicate = (FixedNode) duplicates.get(anchor);
257 ((FixedWithNextNode) forwardEnd.predecessor()).setNext(anchorDuplicate);
258 // move dependencies on the ValueAnchorNode to the previous BeginNode
259 BeginNode prevBegin = BeginNode.prevBegin(anchorDuplicate);
260 anchorDuplicate.replaceAtUsages(prevBegin);
261
262 // re-wire the phi duplicates to the correct input
263 for (PhiNode phi : phiSnapshot) {
264 PhiNode phiDuplicate = (PhiNode) duplicates.get(phi);
265 for (Node usage : phiDuplicate.usages()) {
266 if (usage instanceof ValueNode) {
267 ((ValueNode) usage).dependencies().add(prevBegin);
268 }
269 }
270 phiDuplicate.replaceAtUsages(phi.valueAt(forwardEnd));
271 phiDuplicate.safeDelete();
272 }
273 endIndex++;
274 }
275 GraphUtil.killCFG(merge);
276 for (EndNode forwardEnd : endSnapshot) {
277 forwardEnd.safeDelete();
278 }
279 for (PhiNode phi : phiSnapshot) {
280 // these phis should go away, but they still need to be anchored to a merge to be valid...
281 if (phi.isAlive()) {
282 phi.setMerge(mergeAfter);
283 }
284 }
285 Debug.dump(graph, "After tail duplication at %s", merge);
286 }
287
288 /**
289 * Inserts a new ValueAnchorNode after the merge and transfers all dependency-usages (not phis) to this
290 * ValueAnchorNode.
291 *
292 * @return The new {@link ValueAnchorNode} that was created.
293 */
294 private ValueAnchorNode addValueAnchor() {
295 ValueAnchorNode anchor = graph.add(new ValueAnchorNode());
296 graph.addAfterFixed(merge, anchor);
297 for (Node usage : merge.usages().snapshot()) {
298 if (usage instanceof PhiNode && ((PhiNode) usage).merge() == merge) {
299 // nothing to do
300 } else {
301 usage.replaceFirstInput(merge, anchor);
302 }
303 }
304 return anchor;
305 }
306
307 /**
308 * Given a set of fixed nodes, this method determines the set of fixed and floating nodes that needs to be
309 * duplicated, i.e., all nodes that due to data flow and other dependencies needs to be duplicated.
310 *
311 * @param fixedNodes The set of fixed nodes that should be duplicated.
312 * @param stateAfter The frame state of the merge that follows the set of fixed nodes. All {@link ValueNode}s
313 * reachable from this state are considered to be reachable from within the duplicated set of nodes.
314 * @return The set of nodes that need to be duplicated.
315 */
316 private HashSet<Node> buildDuplicatedNodeSet(final ArrayList<FixedNode> fixedNodes, FrameState stateAfter) {
317 final NodeBitMap aboveBound = graph.createNodeBitMap();
318 final NodeBitMap belowBound = graph.createNodeBitMap();
319
320 final Deque<Node> worklist = new ArrayDeque<>();
321
322 // Build the set of nodes that have (transitive) usages within the duplicatedNodes.
323 // This is achieved by iterating all nodes that are reachable via inputs from the the fixed nodes.
324 aboveBound.markAll(fixedNodes);
325 worklist.addAll(fixedNodes);
326
327 // the phis at the original merge should always be duplicated
328 for (PhiNode phi : merge.phis()) {
329 aboveBound.mark(phi);
330 worklist.add(phi);
331 }
332
333 NodeClosure<Node> aboveClosure = new NodeClosure<Node>() {
334
335 @Override
336 public void apply(Node usage, Node node) {
337 if (node instanceof PhiNode && !fixedNodes.contains(((PhiNode) node).merge())) {
338 // stop iterating: phis belonging to outside merges are known to be outside.
339 } else if (node instanceof FixedNode) {
340 // stop iterating: fixed nodes within the given set are traversal roots anyway, and all other
341 // fixed nodes are known to be outside.
342 } else if (!aboveBound.isMarked(node)) {
343 worklist.add(node);
344 aboveBound.mark(node);
345 }
346 }
347 };
348
349 if (stateAfter != null) {
350 stateAfter.applyToNonVirtual(aboveClosure);
351 }
352 while (!worklist.isEmpty()) {
353 Node current = worklist.remove();
354 for (Node input : current.inputs()) {
355 aboveClosure.apply(current, input);
356 }
357 }
358
359 // Build the set of nodes that have (transitive) inputs within the duplicatedNodes.
360 // This is achieved by iterating all nodes that are reachable via usages from the fixed nodes.
361 belowBound.markAll(fixedNodes);
362 worklist.addAll(fixedNodes);
363
364 // the phis at the original merge should always be duplicated
365 for (PhiNode phi : merge.phis()) {
366 belowBound.mark(phi);
367 worklist.add(phi);
368 }
369
370 while (!worklist.isEmpty()) {
371 Node current = worklist.remove();
372 for (Node usage : current.usages()) {
373 if (usage instanceof PhiNode && !fixedNodes.contains(((PhiNode) usage).merge())) {
374 // stop iterating: phis belonging to outside merges are known to be outside.
375 } else if (usage instanceof FixedNode) {
376 // stop iterating: fixed nodes within the given set are traversal roots anyway, and all other
377 // fixed nodes are known to be outside.
378 } else if (!belowBound.isMarked(usage)) {
379 worklist.add(usage);
380 belowBound.mark(usage);
381 }
382 }
383 }
384
385 // build the intersection
386 belowBound.intersect(aboveBound);
387 HashSet<Node> result = new HashSet<>();
388 for (Node node : belowBound) {
389 result.add(node);
390 }
391 return result;
392 }
393
394 /**
395 * Creates a new merge and end node construct at the end of the duplicated area. While it is useless in itself
396 * (merge with only one end) it simplifies the later duplication step.
397 *
398 * @param successor The successor of the duplicated set of nodes, i.e., the first node that should not be
399 * duplicated.
400 * @param stateAfterMerge The frame state that should be used for the merge.
401 * @return The newly created end node.
402 */
403 private EndNode createNewMerge(FixedNode successor, FrameState stateAfterMerge) {
404 MergeNode newBottomMerge = graph.add(new MergeNode());
405 newBottomMerge.setProbability(successor.probability());
406 EndNode newBottomEnd = graph.add(new EndNode());
407 newBottomMerge.addForwardEnd(newBottomEnd);
408 newBottomMerge.setStateAfter(stateAfterMerge);
409 ((FixedWithNextNode) successor.predecessor()).setNext(newBottomEnd);
410 newBottomMerge.setNext(successor);
411 return newBottomEnd;
412 }
413
414 /**
415 * Expands the set of nodes to be duplicated by looking at a number of conditions:
416 * <ul>
417 * <li>{@link ValueNode}s that have usages on the outside need to be replaced with phis for the outside usages.</li>
418 * <li>Non-{@link ValueNode} nodes that have outside usages (frame states, virtual object states, ...) need to
419 * be cloned immediately for the outside usages.</li>
420 * <li>Nodes that have a {@link StampFactory#extension()} or {@link StampFactory#condition()} stamp need to be
421 * cloned immediately for the outside usages.</li>
422 * <li>Dependencies into the duplicated nodes will be replaced with dependencies on the merge.</li>
423 * <li>Outside non-{@link ValueNode}s with usages within the duplicated set of nodes need to also be duplicated.
424 * </li>
425 * <li>Outside {@link ValueNode}s with {@link StampFactory#extension()} or {@link StampFactory#condition()}
426 * stamps that have usages within the duplicated set of nodes need to also be duplicated.</li>
427 * </ul>
428 *
429 * @param duplicatedNodes The set of duplicated nodes that will be modified (expanded).
430 * @param newBottomMerge The merge that follows the duplicated set of nodes. It will be used for newly created
431 * phis and to as a target for dependencies that pointed into the duplicated set of nodes.
432 */
433 private void expandDuplicated(HashSet<Node> duplicatedNodes, MergeNode newBottomMerge) {
434 Deque<Node> worklist = new ArrayDeque<>(duplicatedNodes);
435
436 while (!worklist.isEmpty()) {
437 Node duplicated = worklist.remove();
438 if (hasUsagesOutside(duplicated, duplicatedNodes)) {
439 boolean cloneForOutsideUsages = false;
440 if (duplicated instanceof ValueNode) {
441 ValueNode node = (ValueNode) duplicated;
442 if (node.stamp() == StampFactory.dependency()) {
443 // re-route dependencies to the merge
444 replaceUsagesOutside(node, newBottomMerge, duplicatedNodes);
445 // TODO(ls) maybe introduce phis for dependencies
446 } else if (node.stamp() == StampFactory.extension() || node.stamp() == StampFactory.condition()) {
447 cloneForOutsideUsages = true;
448 } else {
449 // introduce a new phi
450 PhiNode newPhi = bottomPhis.get(node);
451 if (newPhi == null) {
452 newPhi = graph.add(new PhiNode(node.kind(), newBottomMerge));
453 bottomPhis.put(node, newPhi);
454 newPhi.addInput(node);
455 }
456 replaceUsagesOutside(node, newPhi, duplicatedNodes);
457 }
458 } else {
459 cloneForOutsideUsages = true;
460 }
461 if (cloneForOutsideUsages) {
462 // clone the offending node to the outside
463 Node newOutsideClone = duplicated.copyWithInputs();
464 replaceUsagesOutside(duplicated, newOutsideClone, duplicatedNodes);
465 // this might cause other nodes to have outside usages, we need to look at those as well
466 for (Node input : newOutsideClone.inputs()) {
467 if (duplicatedNodes.contains(input)) {
468 worklist.add(input);
469 }
470 }
471 }
472 }
473 // check if this node has an input that lies outside and cannot be shared
474 for (Node input : duplicated.inputs()) {
475 if (!duplicatedNodes.contains(input)) {
476 boolean duplicateInput = false;
477 if (input instanceof VirtualState) {
478 duplicateInput = true;
479 } else if (input instanceof ValueNode) {
480 Stamp inputStamp = ((ValueNode) input).stamp();
481 if (inputStamp == StampFactory.extension() || inputStamp == StampFactory.condition()) {
482 duplicateInput = true;
483 }
484 }
485 if (duplicateInput) {
486 duplicatedNodes.add(input);
487 worklist.add(input);
488 }
489 }
490 }
491 }
492 }
493
494 /**
495 * Moves all depdendencies that point outside the duplicated area to the supplied value anchor node.
496 *
497 * @param duplicatedNodes The set of duplicated nodes.
498 * @param anchor The node that will be the new target for all dependencies that point outside the duplicated set of nodes.
499 */
500 private static void retargetDependencies(HashSet<Node> duplicatedNodes, ValueAnchorNode anchor) {
501 for (Node node : duplicatedNodes) {
502 if (node instanceof ValueNode) {
503 NodeInputList<ValueNode> dependencies = ((ValueNode) node).dependencies();
504 for (int i = 0; i < dependencies.size(); i++) {
505 Node dependency = dependencies.get(i);
506 if (dependency != null && !duplicatedNodes.contains(dependency)) {
507 Debug.log("retargeting dependency %s to %s on %s", dependency, anchor, node);
508 dependencies.set(i, anchor);
509 }
510 }
511 }
512 }
513 }
514
515 /**
516 * Checks if the given node has usages that are not within the given set of nodes.
517 *
518 * @param node The node whose usages are checked.
519 * @param nodeSet The set of nodes that are considered to be "within".
520 * @return true if the given node has usages on the outside, false otherwise.
521 */
522 private static boolean hasUsagesOutside(Node node, HashSet<Node> nodeSet) {
523 for (Node usage : node.usages()) {
524 if (!nodeSet.contains(usage)) {
525 return true;
526 }
527 }
528 return false;
529 }
530
531 /**
532 * Replaces the given node with the given replacement at all usages that are not within the given set of nodes.
533 *
534 * @param node The node to be replaced at outside usages.
535 * @param replacement The node that replaced the given node at outside usages.
536 * @param nodeSet The set of nodes that are considered to be "within".
537 */
538 private static void replaceUsagesOutside(Node node, Node replacement, HashSet<Node> nodeSet) {
539 for (Node usage : node.usages().snapshot()) {
540 if (!nodeSet.contains(usage)) {
541 usage.replaceFirstInput(node, replacement);
542 }
543 }
544 }
545 }
546 }