annotate graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/TailDuplicationPhase.java @ 10009:b5c87b5c6e9c

add option to enable ahead of time compilation for hotspot (GRAAL-290)
author Bernhard Urban <bernhard.urban@jku.at>
date Wed, 12 Jun 2013 16:04:42 +0200
parents 4158612eca60
children 2faa1e7ef4f3
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
9864
063a712fe8d8 converted remaining options in GraalOptions to new system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents: 9643
diff changeset
25 import static com.oracle.graal.phases.GraalOptions.*;
063a712fe8d8 converted remaining options in GraalOptions to new system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents: 9643
diff changeset
26
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
27 import java.util.*;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
28
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
29 import com.oracle.graal.debug.*;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
30 import com.oracle.graal.graph.Graph.DuplicationReplacement;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
31 import com.oracle.graal.graph.*;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
32 import com.oracle.graal.nodes.*;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
33 import com.oracle.graal.nodes.VirtualState.NodeClosure;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
34 import com.oracle.graal.nodes.extended.*;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
35 import com.oracle.graal.nodes.java.*;
7536
1563a48b798d don't tail duplicate allocations
Lukas Stadler <lukas.stadler@jku.at>
parents: 7255
diff changeset
36 import com.oracle.graal.nodes.spi.*;
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
37 import com.oracle.graal.nodes.type.*;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
38 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
39 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
40 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
41 import com.oracle.graal.phases.tiers.*;
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
42
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
43 /**
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
44 * 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
45 * {@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
46 * 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
47 */
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
48 public class TailDuplicationPhase extends BasePhase<PhaseContext> {
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
49
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 * 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
52 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
53 private static final DebugMetric metricDuplicationEnd = Debug.metric("DuplicationEnd");
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
54 private static final DebugMetric metricDuplicationEndPerformed = Debug.metric("DuplicationEndPerformed");
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
55 private static final DebugMetric metricDuplicationOther = Debug.metric("DuplicationOther");
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
56 private static final DebugMetric metricDuplicationOtherPerformed = Debug.metric("DuplicationOtherPerformed");
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
57
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
58 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
59 * 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
60 * be performed.
5786
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 public interface TailDuplicationDecision {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
63
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
64 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
65 * 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
66 * 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
67 * happened before.
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
68 *
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
69 * @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
70 * @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
71 * duplicated set of nodes.
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
72 * @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
73 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
74 boolean doTransform(MergeNode merge, int fixedNodeCount);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
75 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
76
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
77 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
78 * 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
79 * 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
80 * fixed nodes.
5786
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 static final TailDuplicationDecision DEFAULT_DECISION = new TailDuplicationDecision() {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
83
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
84 public boolean doTransform(MergeNode merge, int fixedNodeCount) {
9864
063a712fe8d8 converted remaining options in GraalOptions to new system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents: 9643
diff changeset
85 if (fixedNodeCount < TailDuplicationTrivialSize.getValue()) {
5792
fa6ed51ac198 more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents: 5786
diff changeset
86 return true;
fa6ed51ac198 more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents: 5786
diff changeset
87 }
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
88 HashSet<PhiNode> improvements = new HashSet<>();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
89 for (PhiNode phi : merge.phis()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
90 Stamp phiStamp = phi.stamp();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
91 for (ValueNode input : phi.values()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
92 if (!input.stamp().equals(phiStamp)) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
93 improvements.add(phi);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
94 break;
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 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
97 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
98 if (improvements.isEmpty()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
99 return false;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
100 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
101 FixedNode current = merge;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
102 int opportunities = 0;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
103 while (current instanceof FixedWithNextNode) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
104 current = ((FixedWithNextNode) current).next();
7536
1563a48b798d don't tail duplicate allocations
Lukas Stadler <lukas.stadler@jku.at>
parents: 7255
diff changeset
105 if (current instanceof VirtualizableAllocation) {
1563a48b798d don't tail duplicate allocations
Lukas Stadler <lukas.stadler@jku.at>
parents: 7255
diff changeset
106 return false;
1563a48b798d don't tail duplicate allocations
Lukas Stadler <lukas.stadler@jku.at>
parents: 7255
diff changeset
107 }
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
108 for (PhiNode phi : improvements) {
6433
4bd8711d824a small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents: 6411
diff changeset
109 for (Node input : current.inputs()) {
4bd8711d824a small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents: 6411
diff changeset
110 if (input == phi) {
4bd8711d824a small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents: 6411
diff changeset
111 opportunities++;
4bd8711d824a small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents: 6411
diff changeset
112 }
4bd8711d824a small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents: 6411
diff changeset
113 if (input.inputs().contains(phi)) {
4bd8711d824a small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents: 6411
diff changeset
114 opportunities++;
4bd8711d824a small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents: 6411
diff changeset
115 }
5786
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 }
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 return opportunities > 0;
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
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 * A tail duplication decision closure that always returns true.
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 public static final TailDuplicationDecision TRUE_DECISION = new TailDuplicationDecision() {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
127
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
128 @Override
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
129 public boolean doTransform(MergeNode merge, int fixedNodeCount) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
130 return true;
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 };
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
133
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
134 @Override
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
135 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
136 NodesToDoubles nodeProbabilities = new ComputeProbabilityClosure(graph).apply();
9234
b9cf7d0b598e removal of FixedNode.probability (draft)
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9051
diff changeset
137
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
138 // 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
139 // duplication.
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
140 for (MergeNode merge : graph.getNodes(MergeNode.class).snapshot()) {
9864
063a712fe8d8 converted remaining options in GraalOptions to new system (GRAAL-27)
Doug Simon <doug.simon@oracle.com>
parents: 9643
diff changeset
141 if (!(merge instanceof LoopBeginNode) && nodeProbabilities.get(merge) >= TailDuplicationProbability.getValue()) {
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
142 tailDuplicate(merge, DEFAULT_DECISION, null, phaseContext);
5786
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 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
146
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
147 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
148 * 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
149 * {@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
150 * 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
151 * {@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
152 * 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
153 * {@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
154 *
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
155 * @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
156 * @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
157 * or not.
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
158 * @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
159 * 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
160 * {@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
161 * {@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
162 * @param phaseContext
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
163 */
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
164 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
165 assert !(merge instanceof LoopBeginNode);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
166 assert replacements == null || replacements.size() == merge.forwardEndCount();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
167 FixedNode fixed = merge;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
168 int fixedCount = 0;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
169 while (fixed instanceof FixedWithNextNode) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
170 fixed = ((FixedWithNextNode) fixed).next();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
171 fixedCount++;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
172 }
8988
02f57662b6c4 remove monitor limitation form TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents: 8558
diff changeset
173 if (fixedCount > 1) {
9434
50f02c7d1cec Rename EndNode => AbstractEndNode and make abstract class. Introduce concrete EndNode subclass.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9329
diff changeset
174 if (fixed instanceof AbstractEndNode && !(((AbstractEndNode) fixed).merge() instanceof LoopBeginNode)) {
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
175 metricDuplicationEnd.increment();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
176 if (decision.doTransform(merge, fixedCount)) {
5792
fa6ed51ac198 more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents: 5786
diff changeset
177 metricDuplicationEndPerformed.increment();
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
178 new DuplicationOperation(merge, replacements).duplicate(phaseContext);
8558
43ab11ee5524 counters for LoopFullUnrollPhase and TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents: 7539
diff changeset
179 return true;
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
180 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
181 } else if (merge.stateAfter() != null) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
182 metricDuplicationOther.increment();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
183 if (decision.doTransform(merge, fixedCount)) {
5792
fa6ed51ac198 more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents: 5786
diff changeset
184 metricDuplicationOtherPerformed.increment();
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
185 new DuplicationOperation(merge, replacements).duplicate(phaseContext);
8558
43ab11ee5524 counters for LoopFullUnrollPhase and TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents: 7539
diff changeset
186 return true;
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
187 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
188 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
189 }
8558
43ab11ee5524 counters for LoopFullUnrollPhase and TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents: 7539
diff changeset
190 return false;
5786
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
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 * 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
195 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
196 private static class DuplicationOperation {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
197
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
198 private final MergeNode merge;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
199 private final StructuredGraph graph;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
200
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
201 private final HashMap<ValueNode, PhiNode> bottomPhis = new HashMap<>();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
202 private final List<PiNode> replacements;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
203
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
204 /**
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
205 * 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
206 *
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
207 * @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
208 * @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
209 * 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
210 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
211 public DuplicationOperation(MergeNode merge, List<PiNode> replacements) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
212 this.merge = merge;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
213 this.replacements = replacements;
9602
19c5a07c7843 Introduce a graph() method that returns a StructuredGraph, to make many explicit casts unnecessary
Christian Wimmer <christian.wimmer@oracle.com>
parents: 9436
diff changeset
214 this.graph = merge.graph();
5786
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
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
217 /**
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
218 * Performs the actual tail duplication:
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
219 * <ul>
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
220 * <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
221 * 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
222 * <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
223 * <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
224 * <li>Determines the complete set of duplicated nodes.</li>
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
225 * <li>Performs the actual duplication.</li>
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
226 * </ul>
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
227 *
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
228 * @param phaseContext
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
229 */
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
230 private void duplicate(PhaseContext phaseContext) {
9234
b9cf7d0b598e removal of FixedNode.probability (draft)
Christian Haeubl <haeubl@ssw.jku.at>
parents: 9051
diff changeset
231 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
232
9329
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
233 int startMark = graph.getMark();
fa188fbfe3fe Perform only targeted canonicalization after tail duplication.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9238
diff changeset
234
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
235 ValueAnchorNode anchor = addValueAnchor();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
236
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
237 // 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
238 // the first control
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
239 // split, end node, deopt or return.
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
240 ArrayList<FixedNode> fixedNodes = new ArrayList<>();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
241 FixedNode fixed = merge.next();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
242 FrameState stateAfter = merge.stateAfter();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
243 while (fixed instanceof FixedWithNextNode) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
244 fixedNodes.add(fixed);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
245 if (fixed instanceof StateSplit && ((StateSplit) fixed).stateAfter() != null) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
246 stateAfter = ((StateSplit) fixed).stateAfter();
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 fixed = ((FixedWithNextNode) fixed).next();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
249 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
250
9434
50f02c7d1cec Rename EndNode => AbstractEndNode and make abstract class. Introduce concrete EndNode subclass.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9329
diff changeset
251 AbstractEndNode endAfter = createNewMerge(fixed, stateAfter);
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
252 MergeNode mergeAfter = endAfter.merge();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
253 fixedNodes.add(endAfter);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
254 final HashSet<Node> duplicatedNodes = buildDuplicatedNodeSet(fixedNodes, stateAfter);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
255 mergeAfter.clearEnds();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
256 expandDuplicated(duplicatedNodes, mergeAfter);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
257
9434
50f02c7d1cec Rename EndNode => AbstractEndNode and make abstract class. Introduce concrete EndNode subclass.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9329
diff changeset
258 List<AbstractEndNode> endSnapshot = merge.forwardEnds().snapshot();
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
259 List<PhiNode> phiSnapshot = merge.phis().snapshot();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
260
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
261 int endIndex = 0;
9434
50f02c7d1cec Rename EndNode => AbstractEndNode and make abstract class. Introduce concrete EndNode subclass.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9329
diff changeset
262 for (final AbstractEndNode forwardEnd : merge.forwardEnds()) {
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
263 Map<Node, Node> duplicates;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
264 if (replacements == null || replacements.get(endIndex) == null) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
265 duplicates = graph.addDuplicates(duplicatedNodes, (DuplicationReplacement) null);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
266 } else {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
267 HashMap<Node, Node> replace = new HashMap<>();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
268 replace.put(replacements.get(endIndex).object(), replacements.get(endIndex));
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
269 duplicates = graph.addDuplicates(duplicatedNodes, replace);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
270 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
271 for (Map.Entry<ValueNode, PhiNode> phi : bottomPhis.entrySet()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
272 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
273 }
9434
50f02c7d1cec Rename EndNode => AbstractEndNode and make abstract class. Introduce concrete EndNode subclass.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9329
diff changeset
274 mergeAfter.addForwardEnd((AbstractEndNode) duplicates.get(endAfter));
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
275
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
276 // 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
277 // EndNode
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
278 FixedNode anchorDuplicate = (FixedNode) duplicates.get(anchor);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
279 ((FixedWithNextNode) forwardEnd.predecessor()).setNext(anchorDuplicate);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
280 // move dependencies on the ValueAnchorNode to the previous BeginNode
9436
ae815a4c112a Rename BeginNode => AbstractBeginNode and make abstract. Introduce concrete subclass BeginNode.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9434
diff changeset
281 AbstractBeginNode prevBegin = AbstractBeginNode.prevBegin(anchorDuplicate);
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
282 anchorDuplicate.replaceAtUsages(prevBegin);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
283
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
284 // re-wire the phi duplicates to the correct input
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
285 for (PhiNode phi : phiSnapshot) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
286 PhiNode phiDuplicate = (PhiNode) duplicates.get(phi);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
287 phiDuplicate.replaceAtUsages(phi.valueAt(forwardEnd));
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
288 phiDuplicate.safeDelete();
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 endIndex++;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
291 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
292 GraphUtil.killCFG(merge);
9434
50f02c7d1cec Rename EndNode => AbstractEndNode and make abstract class. Introduce concrete EndNode subclass.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9329
diff changeset
293 for (AbstractEndNode forwardEnd : endSnapshot) {
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
294 forwardEnd.safeDelete();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
295 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
296 for (PhiNode phi : phiSnapshot) {
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
297 // 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
298 // valid...
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
299 if (phi.isAlive()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
300 phi.setMerge(mergeAfter);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
301 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
302 }
10009
b5c87b5c6e9c add option to enable ahead of time compilation for hotspot (GRAAL-290)
Bernhard Urban <bernhard.urban@jku.at>
parents: 9966
diff changeset
303 new CanonicalizerPhase.Instance(phaseContext.getRuntime(), phaseContext.getAssumptions(), !AOTCompilation.getValue(), graph.getNewNodes(startMark), null).apply(graph);
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
304 Debug.dump(graph, "After tail duplication at %s", merge);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
305 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
306
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
307 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
308 * 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
309 * phis) to this ValueAnchorNode.
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
310 *
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
311 * @return The new {@link ValueAnchorNode} that was created.
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
312 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
313 private ValueAnchorNode addValueAnchor() {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
314 ValueAnchorNode anchor = graph.add(new ValueAnchorNode());
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
315 graph.addAfterFixed(merge, anchor);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
316 for (Node usage : merge.usages().snapshot()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
317 if (usage instanceof PhiNode && ((PhiNode) usage).merge() == merge) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
318 // nothing to do
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
319 } else {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
320 usage.replaceFirstInput(merge, anchor);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
321 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
322 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
323 return 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 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
327 * 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
328 * 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
329 * needs to be duplicated.
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
330 *
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
331 * @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
332 * @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
333 * {@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
334 * from within the duplicated set of nodes.
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
335 * @return The set of nodes that need to be duplicated.
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
336 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
337 private HashSet<Node> buildDuplicatedNodeSet(final ArrayList<FixedNode> fixedNodes, FrameState stateAfter) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
338 final NodeBitMap aboveBound = graph.createNodeBitMap();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
339 final NodeBitMap belowBound = graph.createNodeBitMap();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
340
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
341 final Deque<Node> worklist = new ArrayDeque<>();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
342
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
343 // 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
344 // 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
345 // fixed nodes.
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
346 aboveBound.markAll(fixedNodes);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
347 worklist.addAll(fixedNodes);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
348
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
349 // the phis at the original merge should always be duplicated
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
350 for (PhiNode phi : merge.phis()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
351 aboveBound.mark(phi);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
352 worklist.add(phi);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
353 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
354
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
355 NodeClosure<Node> aboveClosure = new NodeClosure<Node>() {
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 @Override
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
358 public void apply(Node usage, Node node) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
359 if (node instanceof PhiNode && !fixedNodes.contains(((PhiNode) node).merge())) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
360 // 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
361 } else if (node instanceof FixedNode) {
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
362 // 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
363 // anyway, and all other
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
364 // fixed nodes are known to be outside.
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
365 } else if (!aboveBound.isMarked(node)) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
366 worklist.add(node);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
367 aboveBound.mark(node);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
368 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
369 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
370 };
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 if (stateAfter != null) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
373 stateAfter.applyToNonVirtual(aboveClosure);
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 while (!worklist.isEmpty()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
376 Node current = worklist.remove();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
377 for (Node input : current.inputs()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
378 aboveClosure.apply(current, input);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
379 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
380 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
381
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
382 // 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
383 // 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
384 // nodes.
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
385 belowBound.markAll(fixedNodes);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
386 worklist.addAll(fixedNodes);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
387
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
388 // the phis at the original merge should always be duplicated
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
389 for (PhiNode phi : merge.phis()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
390 belowBound.mark(phi);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
391 worklist.add(phi);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
392 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
393
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
394 while (!worklist.isEmpty()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
395 Node current = worklist.remove();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
396 for (Node usage : current.usages()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
397 if (usage instanceof PhiNode && !fixedNodes.contains(((PhiNode) usage).merge())) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
398 // 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
399 } else if (usage instanceof FixedNode) {
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
400 // 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
401 // anyway, and all other
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
402 // fixed nodes are known to be outside.
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
403 } else if (!belowBound.isMarked(usage)) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
404 worklist.add(usage);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
405 belowBound.mark(usage);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
406 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
407 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
408 }
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 // build the intersection
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
411 belowBound.intersect(aboveBound);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
412 HashSet<Node> result = new HashSet<>();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
413 for (Node node : belowBound) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
414 result.add(node);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
415 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
416 return result;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
417 }
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 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
420 * 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
421 * 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
422 *
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
423 * @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
424 * should not be duplicated.
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
425 * @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
426 * @return The newly created end node.
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
427 */
9434
50f02c7d1cec Rename EndNode => AbstractEndNode and make abstract class. Introduce concrete EndNode subclass.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9329
diff changeset
428 private AbstractEndNode createNewMerge(FixedNode successor, FrameState stateAfterMerge) {
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
429 MergeNode newBottomMerge = graph.add(new MergeNode());
9434
50f02c7d1cec Rename EndNode => AbstractEndNode and make abstract class. Introduce concrete EndNode subclass.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9329
diff changeset
430 AbstractEndNode newBottomEnd = graph.add(new EndNode());
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
431 newBottomMerge.addForwardEnd(newBottomEnd);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
432 newBottomMerge.setStateAfter(stateAfterMerge);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
433 ((FixedWithNextNode) successor.predecessor()).setNext(newBottomEnd);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
434 newBottomMerge.setNext(successor);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
435 return newBottomEnd;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
436 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
437
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
438 /**
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
439 * 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
440 * <ul>
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
441 * <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
442 * the outside usages.</li>
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
443 * <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
444 * 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
445 * <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
446 * {@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
447 * usages.</li>
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
448 * <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
449 * merge.</li>
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
450 * <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
451 * also be duplicated.</li>
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
452 * <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
453 * {@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
454 * nodes need to also be duplicated.</li>
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
455 * </ul>
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
456 *
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
457 * @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
458 * @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
459 * 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
460 * the duplicated set of nodes.
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
461 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
462 private void expandDuplicated(HashSet<Node> duplicatedNodes, MergeNode newBottomMerge) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
463 Deque<Node> worklist = new ArrayDeque<>(duplicatedNodes);
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 while (!worklist.isEmpty()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
466 Node duplicated = worklist.remove();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
467 if (hasUsagesOutside(duplicated, duplicatedNodes)) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
468 boolean cloneForOutsideUsages = false;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
469 if (duplicated instanceof ValueNode) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
470 ValueNode node = (ValueNode) duplicated;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
471 if (node.stamp() == StampFactory.dependency()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
472 // re-route dependencies to the merge
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
473 replaceUsagesOutside(node, newBottomMerge, duplicatedNodes);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
474 // TODO(ls) maybe introduce phis for dependencies
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
475 } else if (node.stamp() == StampFactory.extension() || node.stamp() == StampFactory.condition()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
476 cloneForOutsideUsages = true;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
477 } else {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
478 // introduce a new phi
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
479 PhiNode newPhi = bottomPhis.get(node);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
480 if (newPhi == null) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
481 newPhi = graph.add(new PhiNode(node.kind(), newBottomMerge));
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
482 bottomPhis.put(node, newPhi);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
483 newPhi.addInput(node);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
484 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
485 replaceUsagesOutside(node, newPhi, duplicatedNodes);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
486 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
487 } else {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
488 cloneForOutsideUsages = true;
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 if (cloneForOutsideUsages) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
491 // clone the offending node to the outside
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
492 Node newOutsideClone = duplicated.copyWithInputs();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
493 replaceUsagesOutside(duplicated, newOutsideClone, duplicatedNodes);
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
494 // 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
495 // those as well
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
496 for (Node input : newOutsideClone.inputs()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
497 if (duplicatedNodes.contains(input)) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
498 worklist.add(input);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
499 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
500 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
501 }
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 // 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
504 for (Node input : duplicated.inputs()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
505 if (!duplicatedNodes.contains(input)) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
506 boolean duplicateInput = false;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
507 if (input instanceof VirtualState) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
508 duplicateInput = true;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
509 } else if (input instanceof ValueNode) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
510 Stamp inputStamp = ((ValueNode) input).stamp();
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
511 if (inputStamp == StampFactory.extension() || inputStamp == StampFactory.condition()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
512 duplicateInput = true;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
513 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
514 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
515 if (duplicateInput) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
516 duplicatedNodes.add(input);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
517 worklist.add(input);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
518 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
519 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
520 }
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 * 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
526 *
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
527 * @param node The node whose usages are checked.
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
528 * @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
529 * @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
530 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
531 private static boolean hasUsagesOutside(Node node, HashSet<Node> nodeSet) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
532 for (Node usage : node.usages()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
533 if (!nodeSet.contains(usage)) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
534 return true;
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
535 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
536 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
537 return false;
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 /**
7530
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
541 * 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
542 * given set of nodes.
5e3d1a68664e applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents: 7255
diff changeset
543 *
5786
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
544 * @param node The node to be replaced at outside usages.
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
545 * @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
546 * @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
547 */
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
548 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
549 for (Node usage : node.usages().snapshot()) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
550 if (!nodeSet.contains(usage)) {
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
551 usage.replaceFirstInput(node, replacement);
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
552 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
553 }
f69a406355b2 new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff changeset
554 }
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 }