annotate graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/TailDuplicationPhase.java @ 9329:fa188fbfe3fe

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