43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 2013, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.phases.common;
|
|
24 |
|
|
25 |
import java.util.LinkedList;
|
|
26 |
import java.util.List;
|
|
27 |
|
|
28 |
import org.graalvm.compiler.core.common.cfg.Loop;
|
|
29 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
50330
|
30 |
import org.graalvm.compiler.debug.DebugCloseable;
|
43972
|
31 |
import org.graalvm.compiler.nodes.AbstractDeoptimizeNode;
|
|
32 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
33 |
import org.graalvm.compiler.nodes.DynamicDeoptimizeNode;
|
|
34 |
import org.graalvm.compiler.nodes.EndNode;
|
|
35 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
36 |
import org.graalvm.compiler.nodes.FrameState;
|
|
37 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
|
38 |
import org.graalvm.compiler.nodes.LoopExitNode;
|
|
39 |
import org.graalvm.compiler.nodes.MergeNode;
|
|
40 |
import org.graalvm.compiler.nodes.PhiNode;
|
|
41 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
42 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
43 |
import org.graalvm.compiler.nodes.ValuePhiNode;
|
|
44 |
import org.graalvm.compiler.nodes.cfg.Block;
|
|
45 |
import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
|
|
46 |
import org.graalvm.compiler.phases.BasePhase;
|
|
47 |
import org.graalvm.compiler.phases.tiers.MidTierContext;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* This phase tries to find {@link AbstractDeoptimizeNode DeoptimizeNodes} which use the same
|
|
51 |
* {@link FrameState} and merges them together.
|
|
52 |
*/
|
|
53 |
public class DeoptimizationGroupingPhase extends BasePhase<MidTierContext> {
|
|
54 |
|
|
55 |
@Override
|
50330
|
56 |
@SuppressWarnings("try")
|
43972
|
57 |
protected void run(StructuredGraph graph, MidTierContext context) {
|
|
58 |
ControlFlowGraph cfg = null;
|
|
59 |
for (FrameState fs : graph.getNodes(FrameState.TYPE)) {
|
|
60 |
FixedNode target = null;
|
|
61 |
PhiNode reasonActionPhi = null;
|
|
62 |
PhiNode speculationPhi = null;
|
|
63 |
List<AbstractDeoptimizeNode> obsoletes = null;
|
|
64 |
for (AbstractDeoptimizeNode deopt : fs.usages().filter(AbstractDeoptimizeNode.class)) {
|
|
65 |
if (target == null) {
|
|
66 |
target = deopt;
|
|
67 |
} else {
|
|
68 |
if (cfg == null) {
|
|
69 |
cfg = ControlFlowGraph.compute(graph, true, true, false, false);
|
|
70 |
}
|
|
71 |
AbstractMergeNode merge;
|
|
72 |
if (target instanceof AbstractDeoptimizeNode) {
|
|
73 |
merge = graph.add(new MergeNode());
|
|
74 |
EndNode firstEnd = graph.add(new EndNode());
|
|
75 |
ValueNode actionAndReason = ((AbstractDeoptimizeNode) target).getActionAndReason(context.getMetaAccess());
|
|
76 |
ValueNode speculation = ((AbstractDeoptimizeNode) target).getSpeculation(context.getMetaAccess());
|
|
77 |
reasonActionPhi = graph.addWithoutUnique(new ValuePhiNode(StampFactory.forKind(actionAndReason.getStackKind()), merge));
|
|
78 |
speculationPhi = graph.addWithoutUnique(new ValuePhiNode(StampFactory.forKind(speculation.getStackKind()), merge));
|
|
79 |
merge.addForwardEnd(firstEnd);
|
|
80 |
reasonActionPhi.addInput(actionAndReason);
|
|
81 |
speculationPhi.addInput(speculation);
|
|
82 |
target.replaceAtPredecessor(firstEnd);
|
|
83 |
|
|
84 |
exitLoops((AbstractDeoptimizeNode) target, firstEnd, cfg);
|
50330
|
85 |
try (DebugCloseable position = target.withNodeSourcePosition()) {
|
|
86 |
merge.setNext(graph.add(new DynamicDeoptimizeNode(reasonActionPhi, speculationPhi)));
|
|
87 |
}
|
43972
|
88 |
obsoletes = new LinkedList<>();
|
|
89 |
obsoletes.add((AbstractDeoptimizeNode) target);
|
|
90 |
target = merge;
|
|
91 |
} else {
|
|
92 |
merge = (AbstractMergeNode) target;
|
|
93 |
}
|
|
94 |
EndNode newEnd = graph.add(new EndNode());
|
|
95 |
merge.addForwardEnd(newEnd);
|
|
96 |
reasonActionPhi.addInput(deopt.getActionAndReason(context.getMetaAccess()));
|
|
97 |
speculationPhi.addInput(deopt.getSpeculation(context.getMetaAccess()));
|
|
98 |
deopt.replaceAtPredecessor(newEnd);
|
|
99 |
exitLoops(deopt, newEnd, cfg);
|
|
100 |
obsoletes.add(deopt);
|
|
101 |
}
|
|
102 |
}
|
|
103 |
if (obsoletes != null) {
|
|
104 |
((DynamicDeoptimizeNode) ((AbstractMergeNode) target).next()).setStateBefore(fs);
|
|
105 |
for (AbstractDeoptimizeNode obsolete : obsoletes) {
|
|
106 |
obsolete.safeDelete();
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
private static void exitLoops(AbstractDeoptimizeNode deopt, EndNode end, ControlFlowGraph cfg) {
|
|
113 |
Block block = cfg.blockFor(deopt);
|
|
114 |
Loop<Block> loop = block.getLoop();
|
|
115 |
while (loop != null) {
|
|
116 |
end.graph().addBeforeFixed(end, end.graph().add(new LoopExitNode((LoopBeginNode) loop.getHeader().getBeginNode())));
|
|
117 |
loop = loop.getParent();
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
@Override
|
|
122 |
public float codeSizeIncrease() {
|
|
123 |
return 2.5f;
|
|
124 |
}
|
|
125 |
}
|