Mercurial > hg > truffle
annotate graal/com.oracle.graal.phases.common/src/com/oracle/graal/phases/common/TailDuplicationPhase.java @ 9238:8f01fe16e473
refactorings and cleanups for the removal of FixedNode.probability
author | Christian Haeubl <haeubl@ssw.jku.at> |
---|---|
date | Mon, 22 Apr 2013 17:49:13 +0200 |
parents | b9cf7d0b598e |
children | fa188fbfe3fe |
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.*; |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
39 |
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 * This class is a phase that looks for opportunities for tail duplication. The static method |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
42 * {@link #tailDuplicate(MergeNode, TailDuplicationDecision, List)} can also be used to drive tail |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
43 * duplication from other places, e.g., inlining. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
44 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
45 public class TailDuplicationPhase extends Phase { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
46 |
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 * 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
|
49 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
50 private static final DebugMetric metricDuplicationEnd = Debug.metric("DuplicationEnd"); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
51 private static final DebugMetric metricDuplicationEndPerformed = Debug.metric("DuplicationEndPerformed"); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
52 private static final DebugMetric metricDuplicationOther = Debug.metric("DuplicationOther"); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
53 private static final DebugMetric metricDuplicationOtherPerformed = Debug.metric("DuplicationOtherPerformed"); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
54 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
55 /** |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
56 * 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
|
57 * be performed. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
58 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
59 public interface TailDuplicationDecision { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
60 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
61 /** |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
62 * 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
|
63 * 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
|
64 * happened before. |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
65 * |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
66 * @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
|
67 * @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
|
68 * duplicated set of nodes. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
69 * @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
|
70 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
71 boolean doTransform(MergeNode merge, int fixedNodeCount); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
72 } |
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 /** |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
75 * 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
|
76 * 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
|
77 * fixed nodes. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
78 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
79 public static final TailDuplicationDecision DEFAULT_DECISION = new TailDuplicationDecision() { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
80 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
81 public boolean doTransform(MergeNode merge, int fixedNodeCount) { |
5792
fa6ed51ac198
more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents:
5786
diff
changeset
|
82 if (fixedNodeCount < GraalOptions.TailDuplicationTrivialSize) { |
fa6ed51ac198
more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents:
5786
diff
changeset
|
83 return true; |
fa6ed51ac198
more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents:
5786
diff
changeset
|
84 } |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
85 HashSet<PhiNode> improvements = new HashSet<>(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
86 for (PhiNode phi : merge.phis()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
87 Stamp phiStamp = phi.stamp(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
88 for (ValueNode input : phi.values()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
89 if (!input.stamp().equals(phiStamp)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
90 improvements.add(phi); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
91 break; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
92 } |
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 if (improvements.isEmpty()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
96 return false; |
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 FixedNode current = merge; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
99 int opportunities = 0; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
100 while (current instanceof FixedWithNextNode) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
101 current = ((FixedWithNextNode) current).next(); |
7536
1563a48b798d
don't tail duplicate allocations
Lukas Stadler <lukas.stadler@jku.at>
parents:
7255
diff
changeset
|
102 if (current instanceof VirtualizableAllocation) { |
1563a48b798d
don't tail duplicate allocations
Lukas Stadler <lukas.stadler@jku.at>
parents:
7255
diff
changeset
|
103 return false; |
1563a48b798d
don't tail duplicate allocations
Lukas Stadler <lukas.stadler@jku.at>
parents:
7255
diff
changeset
|
104 } |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
105 for (PhiNode phi : improvements) { |
6433
4bd8711d824a
small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents:
6411
diff
changeset
|
106 for (Node input : current.inputs()) { |
4bd8711d824a
small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents:
6411
diff
changeset
|
107 if (input == phi) { |
4bd8711d824a
small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents:
6411
diff
changeset
|
108 opportunities++; |
4bd8711d824a
small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents:
6411
diff
changeset
|
109 } |
4bd8711d824a
small fix to tail duplication heuristics
Lukas Stadler <lukas.stadler@jku.at>
parents:
6411
diff
changeset
|
110 if (input.inputs().contains(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 } |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
113 } |
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 return opportunities > 0; |
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 |
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 * A tail duplication decision closure that always returns true. |
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 public static final TailDuplicationDecision TRUE_DECISION = new TailDuplicationDecision() { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
124 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
125 @Override |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
126 public boolean doTransform(MergeNode merge, int fixedNodeCount) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
127 return true; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
128 } |
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 @Override |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
132 protected void run(StructuredGraph graph) { |
9238
8f01fe16e473
refactorings and cleanups for the removal of FixedNode.probability
Christian Haeubl <haeubl@ssw.jku.at>
parents:
9234
diff
changeset
|
133 NodesToDoubles nodeProbabilities = new ComputeProbabilityClosure(graph).apply(); |
9234
b9cf7d0b598e
removal of FixedNode.probability (draft)
Christian Haeubl <haeubl@ssw.jku.at>
parents:
9051
diff
changeset
|
134 |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
135 // 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
|
136 // duplication. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
137 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
|
138 if (!(merge instanceof LoopBeginNode) && nodeProbabilities.get(merge) >= GraalOptions.TailDuplicationProbability) { |
9051
fb2b0866e350
Remove unused fields.
Roland Schatz <roland.schatz@oracle.com>
parents:
9031
diff
changeset
|
139 tailDuplicate(merge, DEFAULT_DECISION, null); |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
140 } |
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 /** |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
145 * 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
|
146 * {@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
|
147 * 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
|
148 * {@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
|
149 * 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
|
150 * {@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
|
151 * |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
152 * @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
|
153 * @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
|
154 * or not. |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
155 * @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
|
156 * 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
|
157 * {@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
|
158 * {@link PiNode} in the duplicated branch that corresponds to the entry. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
159 */ |
8558
43ab11ee5524
counters for LoopFullUnrollPhase and TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents:
7539
diff
changeset
|
160 public static boolean tailDuplicate(MergeNode merge, TailDuplicationDecision decision, List<PiNode> replacements) { |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
161 assert !(merge instanceof LoopBeginNode); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
162 assert replacements == null || replacements.size() == merge.forwardEndCount(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
163 FixedNode fixed = merge; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
164 int fixedCount = 0; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
165 while (fixed instanceof FixedWithNextNode) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
166 fixed = ((FixedWithNextNode) fixed).next(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
167 fixedCount++; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
168 } |
8988
02f57662b6c4
remove monitor limitation form TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents:
8558
diff
changeset
|
169 if (fixedCount > 1) { |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
170 if (fixed instanceof EndNode && !(((EndNode) fixed).merge() instanceof LoopBeginNode)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
171 metricDuplicationEnd.increment(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
172 if (decision.doTransform(merge, fixedCount)) { |
5792
fa6ed51ac198
more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents:
5786
diff
changeset
|
173 metricDuplicationEndPerformed.increment(); |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
174 new DuplicationOperation(merge, replacements).duplicate(); |
8558
43ab11ee5524
counters for LoopFullUnrollPhase and TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents:
7539
diff
changeset
|
175 return true; |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
176 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
177 } else if (merge.stateAfter() != null) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
178 metricDuplicationOther.increment(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
179 if (decision.doTransform(merge, fixedCount)) { |
5792
fa6ed51ac198
more aggressive tail duplication
Lukas Stadler <lukas.stadler@jku.at>
parents:
5786
diff
changeset
|
180 metricDuplicationOtherPerformed.increment(); |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
181 new DuplicationOperation(merge, replacements).duplicate(); |
8558
43ab11ee5524
counters for LoopFullUnrollPhase and TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents:
7539
diff
changeset
|
182 return true; |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
183 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
184 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
185 } |
8558
43ab11ee5524
counters for LoopFullUnrollPhase and TailDuplicationPhase
Lukas Stadler <lukas.stadler@jku.at>
parents:
7539
diff
changeset
|
186 return false; |
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 /** |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
190 * 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
|
191 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
192 private static class DuplicationOperation { |
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 final MergeNode merge; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
195 private final StructuredGraph graph; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
196 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
197 private final HashMap<ValueNode, PhiNode> bottomPhis = new HashMap<>(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
198 private final List<PiNode> replacements; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
199 |
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 * 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
|
202 * |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
203 * @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
|
204 * @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
|
205 * 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
|
206 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
207 public DuplicationOperation(MergeNode merge, List<PiNode> replacements) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
208 this.merge = merge; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
209 this.replacements = replacements; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
210 this.graph = (StructuredGraph) merge.graph(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
211 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
212 |
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 * Performs the actual tail duplication: |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
215 * <ul> |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
216 * <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
|
217 * 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
|
218 * <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
|
219 * <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
|
220 * <li>Determines the complete set of duplicated nodes.</li> |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
221 * <li>Performs the actual duplication.</li> |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
222 * </ul> |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
223 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
224 private void duplicate() { |
9234
b9cf7d0b598e
removal of FixedNode.probability (draft)
Christian Haeubl <haeubl@ssw.jku.at>
parents:
9051
diff
changeset
|
225 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
|
226 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
227 ValueAnchorNode anchor = addValueAnchor(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
228 |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
229 // 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
|
230 // the first control |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
231 // split, end node, deopt or return. |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
232 ArrayList<FixedNode> fixedNodes = new ArrayList<>(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
233 FixedNode fixed = merge.next(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
234 FrameState stateAfter = merge.stateAfter(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
235 while (fixed instanceof FixedWithNextNode) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
236 fixedNodes.add(fixed); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
237 if (fixed instanceof StateSplit && ((StateSplit) fixed).stateAfter() != null) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
238 stateAfter = ((StateSplit) fixed).stateAfter(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
239 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
240 fixed = ((FixedWithNextNode) fixed).next(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
241 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
242 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
243 EndNode endAfter = createNewMerge(fixed, stateAfter); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
244 MergeNode mergeAfter = endAfter.merge(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
245 fixedNodes.add(endAfter); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
246 final HashSet<Node> duplicatedNodes = buildDuplicatedNodeSet(fixedNodes, stateAfter); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
247 mergeAfter.clearEnds(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
248 expandDuplicated(duplicatedNodes, mergeAfter); |
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 List<EndNode> endSnapshot = merge.forwardEnds().snapshot(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
251 List<PhiNode> phiSnapshot = merge.phis().snapshot(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
252 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
253 int endIndex = 0; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
254 for (final EndNode forwardEnd : merge.forwardEnds()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
255 Map<Node, Node> duplicates; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
256 if (replacements == null || replacements.get(endIndex) == null) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
257 duplicates = graph.addDuplicates(duplicatedNodes, (DuplicationReplacement) null); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
258 } else { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
259 HashMap<Node, Node> replace = new HashMap<>(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
260 replace.put(replacements.get(endIndex).object(), replacements.get(endIndex)); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
261 duplicates = graph.addDuplicates(duplicatedNodes, replace); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
262 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
263 for (Map.Entry<ValueNode, PhiNode> phi : bottomPhis.entrySet()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
264 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
|
265 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
266 mergeAfter.addForwardEnd((EndNode) duplicates.get(endAfter)); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
267 |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
268 // 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
|
269 // EndNode |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
270 FixedNode anchorDuplicate = (FixedNode) duplicates.get(anchor); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
271 ((FixedWithNextNode) forwardEnd.predecessor()).setNext(anchorDuplicate); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
272 // move dependencies on the ValueAnchorNode to the previous BeginNode |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
273 BeginNode prevBegin = BeginNode.prevBegin(anchorDuplicate); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
274 anchorDuplicate.replaceAtUsages(prevBegin); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
275 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
276 // re-wire the phi duplicates to the correct input |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
277 for (PhiNode phi : phiSnapshot) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
278 PhiNode phiDuplicate = (PhiNode) duplicates.get(phi); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
279 for (Node usage : phiDuplicate.usages()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
280 if (usage instanceof ValueNode) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
281 ((ValueNode) usage).dependencies().add(prevBegin); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
282 } |
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 phiDuplicate.replaceAtUsages(phi.valueAt(forwardEnd)); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
285 phiDuplicate.safeDelete(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
286 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
287 endIndex++; |
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 GraphUtil.killCFG(merge); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
290 for (EndNode forwardEnd : endSnapshot) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
291 forwardEnd.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 for (PhiNode phi : phiSnapshot) { |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
294 // 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
|
295 // valid... |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
296 if (phi.isAlive()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
297 phi.setMerge(mergeAfter); |
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 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
300 Debug.dump(graph, "After tail duplication at %s", merge); |
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 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
303 /** |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
304 * 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
|
305 * phis) to this ValueAnchorNode. |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
306 * |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
307 * @return The new {@link ValueAnchorNode} that was created. |
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 private ValueAnchorNode addValueAnchor() { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
310 ValueAnchorNode anchor = graph.add(new ValueAnchorNode()); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
311 graph.addAfterFixed(merge, anchor); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
312 for (Node usage : merge.usages().snapshot()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
313 if (usage instanceof PhiNode && ((PhiNode) usage).merge() == merge) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
314 // nothing to do |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
315 } else { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
316 usage.replaceFirstInput(merge, anchor); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
317 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
318 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
319 return anchor; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
320 } |
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 /** |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
323 * 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
|
324 * 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
|
325 * needs to be duplicated. |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
326 * |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
327 * @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
|
328 * @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
|
329 * {@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
|
330 * from within the duplicated set of nodes. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
331 * @return The set of nodes that need to be duplicated. |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
332 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
333 private HashSet<Node> buildDuplicatedNodeSet(final ArrayList<FixedNode> fixedNodes, FrameState stateAfter) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
334 final NodeBitMap aboveBound = graph.createNodeBitMap(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
335 final NodeBitMap belowBound = graph.createNodeBitMap(); |
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 final Deque<Node> worklist = new ArrayDeque<>(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
338 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
339 // 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
|
340 // 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
|
341 // fixed nodes. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
342 aboveBound.markAll(fixedNodes); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
343 worklist.addAll(fixedNodes); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
344 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
345 // the phis at the original merge should always be duplicated |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
346 for (PhiNode phi : merge.phis()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
347 aboveBound.mark(phi); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
348 worklist.add(phi); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
349 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
350 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
351 NodeClosure<Node> aboveClosure = new NodeClosure<Node>() { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
352 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
353 @Override |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
354 public void apply(Node usage, Node node) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
355 if (node instanceof PhiNode && !fixedNodes.contains(((PhiNode) node).merge())) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
356 // 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
|
357 } else if (node instanceof FixedNode) { |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
358 // 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
|
359 // anyway, and all other |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
360 // fixed nodes are known to be outside. |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
361 } else if (!aboveBound.isMarked(node)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
362 worklist.add(node); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
363 aboveBound.mark(node); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
364 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
365 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
366 }; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
367 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
368 if (stateAfter != null) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
369 stateAfter.applyToNonVirtual(aboveClosure); |
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 while (!worklist.isEmpty()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
372 Node current = worklist.remove(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
373 for (Node input : current.inputs()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
374 aboveClosure.apply(current, input); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
375 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
376 } |
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 // 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
|
379 // 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
|
380 // nodes. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
381 belowBound.markAll(fixedNodes); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
382 worklist.addAll(fixedNodes); |
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 // the phis at the original merge should always be duplicated |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
385 for (PhiNode phi : merge.phis()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
386 belowBound.mark(phi); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
387 worklist.add(phi); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
388 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
389 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
390 while (!worklist.isEmpty()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
391 Node current = worklist.remove(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
392 for (Node usage : current.usages()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
393 if (usage instanceof PhiNode && !fixedNodes.contains(((PhiNode) usage).merge())) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
394 // 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
|
395 } else if (usage instanceof FixedNode) { |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
396 // 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
|
397 // anyway, and all other |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
398 // fixed nodes are known to be outside. |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
399 } else if (!belowBound.isMarked(usage)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
400 worklist.add(usage); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
401 belowBound.mark(usage); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
402 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
403 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
404 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
405 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
406 // build the intersection |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
407 belowBound.intersect(aboveBound); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
408 HashSet<Node> result = new HashSet<>(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
409 for (Node node : belowBound) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
410 result.add(node); |
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 return result; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
413 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
414 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
415 /** |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
416 * 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
|
417 * 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
|
418 * |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
419 * @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
|
420 * should not be duplicated. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
421 * @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
|
422 * @return The newly created end node. |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
423 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
424 private EndNode createNewMerge(FixedNode successor, FrameState stateAfterMerge) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
425 MergeNode newBottomMerge = graph.add(new MergeNode()); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
426 EndNode newBottomEnd = graph.add(new EndNode()); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
427 newBottomMerge.addForwardEnd(newBottomEnd); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
428 newBottomMerge.setStateAfter(stateAfterMerge); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
429 ((FixedWithNextNode) successor.predecessor()).setNext(newBottomEnd); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
430 newBottomMerge.setNext(successor); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
431 return newBottomEnd; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
432 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
433 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
434 /** |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
435 * 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
|
436 * <ul> |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
437 * <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
|
438 * the outside usages.</li> |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
439 * <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
|
440 * 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
|
441 * <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
|
442 * {@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
|
443 * usages.</li> |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
444 * <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
|
445 * merge.</li> |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
446 * <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
|
447 * also be duplicated.</li> |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
448 * <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
|
449 * {@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
|
450 * nodes need to also be duplicated.</li> |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
451 * </ul> |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
452 * |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
453 * @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
|
454 * @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
|
455 * 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
|
456 * the duplicated set of nodes. |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
457 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
458 private void expandDuplicated(HashSet<Node> duplicatedNodes, MergeNode newBottomMerge) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
459 Deque<Node> worklist = new ArrayDeque<>(duplicatedNodes); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
460 |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
461 while (!worklist.isEmpty()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
462 Node duplicated = worklist.remove(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
463 if (hasUsagesOutside(duplicated, duplicatedNodes)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
464 boolean cloneForOutsideUsages = false; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
465 if (duplicated instanceof ValueNode) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
466 ValueNode node = (ValueNode) duplicated; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
467 if (node.stamp() == StampFactory.dependency()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
468 // re-route dependencies to the merge |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
469 replaceUsagesOutside(node, newBottomMerge, duplicatedNodes); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
470 // TODO(ls) maybe introduce phis for dependencies |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
471 } else if (node.stamp() == StampFactory.extension() || node.stamp() == StampFactory.condition()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
472 cloneForOutsideUsages = true; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
473 } else { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
474 // introduce a new phi |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
475 PhiNode newPhi = bottomPhis.get(node); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
476 if (newPhi == null) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
477 newPhi = graph.add(new PhiNode(node.kind(), newBottomMerge)); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
478 bottomPhis.put(node, newPhi); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
479 newPhi.addInput(node); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
480 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
481 replaceUsagesOutside(node, newPhi, duplicatedNodes); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
482 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
483 } else { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
484 cloneForOutsideUsages = true; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
485 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
486 if (cloneForOutsideUsages) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
487 // clone the offending node to the outside |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
488 Node newOutsideClone = duplicated.copyWithInputs(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
489 replaceUsagesOutside(duplicated, newOutsideClone, duplicatedNodes); |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
490 // 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
|
491 // those as well |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
492 for (Node input : newOutsideClone.inputs()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
493 if (duplicatedNodes.contains(input)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
494 worklist.add(input); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
495 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
496 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
497 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
498 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
499 // 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
|
500 for (Node input : duplicated.inputs()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
501 if (!duplicatedNodes.contains(input)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
502 boolean duplicateInput = false; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
503 if (input instanceof VirtualState) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
504 duplicateInput = true; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
505 } else if (input instanceof ValueNode) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
506 Stamp inputStamp = ((ValueNode) input).stamp(); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
507 if (inputStamp == StampFactory.extension() || inputStamp == StampFactory.condition()) { |
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 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
510 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
511 if (duplicateInput) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
512 duplicatedNodes.add(input); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
513 worklist.add(input); |
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 } |
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 } |
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 * 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
|
522 * |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
523 * @param node The node whose usages are checked. |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
524 * @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
|
525 * @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
|
526 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
527 private static boolean hasUsagesOutside(Node node, HashSet<Node> nodeSet) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
528 for (Node usage : node.usages()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
529 if (!nodeSet.contains(usage)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
530 return true; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
531 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
532 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
533 return false; |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
534 } |
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 /** |
7530
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
537 * 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
|
538 * given set of nodes. |
5e3d1a68664e
applied mx eclipseformat to all Java files
Doug Simon <doug.simon@oracle.com>
parents:
7255
diff
changeset
|
539 * |
5786
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
540 * @param node The node to be replaced at outside usages. |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
541 * @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
|
542 * @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
|
543 */ |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
544 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
|
545 for (Node usage : node.usages().snapshot()) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
546 if (!nodeSet.contains(usage)) { |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
547 usage.replaceFirstInput(node, replacement); |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
548 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
549 } |
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 } |
f69a406355b2
new tail duplication phase
Lukas Stadler <lukas.stadler@jku.at>
parents:
diff
changeset
|
552 } |