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