43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 2016, 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.List;
|
|
26 |
|
48861
|
27 |
import org.graalvm.collections.EconomicMap;
|
43972
|
28 |
import org.graalvm.compiler.debug.GraalError;
|
|
29 |
import org.graalvm.compiler.graph.Node;
|
|
30 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
31 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
32 |
import org.graalvm.compiler.nodes.DeoptimizingNode;
|
|
33 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
34 |
import org.graalvm.compiler.nodes.FrameState;
|
|
35 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
|
36 |
import org.graalvm.compiler.nodes.LoopExitNode;
|
|
37 |
import org.graalvm.compiler.nodes.StateSplit;
|
|
38 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
39 |
import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
|
|
40 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
|
41 |
import org.graalvm.compiler.phases.Phase;
|
|
42 |
import org.graalvm.compiler.phases.graph.ReentrantNodeIterator;
|
|
43 |
import org.graalvm.compiler.phases.graph.ReentrantNodeIterator.NodeIteratorClosure;
|
|
44 |
|
46807
|
45 |
import jdk.vm.ci.code.BytecodeFrame;
|
|
46 |
|
43972
|
47 |
/**
|
|
48 |
* This phase transfers {@link FrameState} nodes from {@link StateSplit} nodes to
|
|
49 |
* {@link DeoptimizingNode DeoptimizingNodes}.
|
|
50 |
*
|
|
51 |
* This allow to enter the {@link GuardsStage#AFTER_FSA AFTER_FSA} stage of the graph where no new
|
|
52 |
* node that may cause deoptimization can be introduced anymore.
|
|
53 |
* <p>
|
|
54 |
* This Phase processes the graph in post order, assigning the {@link FrameState} from the last
|
|
55 |
* {@link StateSplit} node to {@link DeoptimizingNode DeoptimizingNodes}.
|
|
56 |
*/
|
|
57 |
public class FrameStateAssignmentPhase extends Phase {
|
|
58 |
|
|
59 |
private static class FrameStateAssignmentClosure extends NodeIteratorClosure<FrameState> {
|
|
60 |
|
|
61 |
@Override
|
|
62 |
protected FrameState processNode(FixedNode node, FrameState previousState) {
|
|
63 |
FrameState currentState = previousState;
|
|
64 |
if (node instanceof DeoptimizingNode.DeoptBefore) {
|
|
65 |
DeoptimizingNode.DeoptBefore deopt = (DeoptimizingNode.DeoptBefore) node;
|
|
66 |
if (deopt.canDeoptimize() && deopt.stateBefore() == null) {
|
|
67 |
GraalError.guarantee(currentState != null, "no FrameState at DeoptimizingNode %s", deopt);
|
|
68 |
deopt.setStateBefore(currentState);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
if (node instanceof StateSplit) {
|
|
73 |
StateSplit stateSplit = (StateSplit) node;
|
|
74 |
FrameState stateAfter = stateSplit.stateAfter();
|
|
75 |
if (stateAfter != null) {
|
46807
|
76 |
if (stateAfter.bci == BytecodeFrame.INVALID_FRAMESTATE_BCI) {
|
|
77 |
currentState = null;
|
|
78 |
} else {
|
|
79 |
currentState = stateAfter;
|
|
80 |
}
|
43972
|
81 |
stateSplit.setStateAfter(null);
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
if (node instanceof DeoptimizingNode.DeoptDuring) {
|
|
86 |
DeoptimizingNode.DeoptDuring deopt = (DeoptimizingNode.DeoptDuring) node;
|
|
87 |
if (deopt.canDeoptimize()) {
|
|
88 |
GraalError.guarantee(currentState != null, "no FrameState at DeoptimizingNode %s", deopt);
|
|
89 |
deopt.computeStateDuring(currentState);
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
if (node instanceof DeoptimizingNode.DeoptAfter) {
|
|
94 |
DeoptimizingNode.DeoptAfter deopt = (DeoptimizingNode.DeoptAfter) node;
|
|
95 |
if (deopt.canDeoptimize() && deopt.stateAfter() == null) {
|
|
96 |
GraalError.guarantee(currentState != null, "no FrameState at DeoptimizingNode %s", deopt);
|
|
97 |
deopt.setStateAfter(currentState);
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
return currentState;
|
|
102 |
}
|
|
103 |
|
|
104 |
@Override
|
|
105 |
protected FrameState merge(AbstractMergeNode merge, List<FrameState> states) {
|
|
106 |
FrameState singleFrameState = singleFrameState(states);
|
|
107 |
return singleFrameState == null ? merge.stateAfter() : singleFrameState;
|
|
108 |
}
|
|
109 |
|
|
110 |
@Override
|
|
111 |
protected FrameState afterSplit(AbstractBeginNode node, FrameState oldState) {
|
|
112 |
return oldState;
|
|
113 |
}
|
|
114 |
|
|
115 |
@Override
|
46344
|
116 |
protected EconomicMap<LoopExitNode, FrameState> processLoop(LoopBeginNode loop, FrameState initialState) {
|
43972
|
117 |
return ReentrantNodeIterator.processLoop(this, loop, initialState).exitStates;
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
@Override
|
|
122 |
protected void run(StructuredGraph graph) {
|
|
123 |
assert !graph.getGuardsStage().allowsFloatingGuards() && !hasFloatingDeopts(graph);
|
|
124 |
if (graph.getGuardsStage().areFrameStatesAtSideEffects()) {
|
|
125 |
ReentrantNodeIterator.apply(new FrameStateAssignmentClosure(), graph.start(), null);
|
|
126 |
graph.setGuardsStage(GuardsStage.AFTER_FSA);
|
|
127 |
graph.getNodes(FrameState.TYPE).filter(state -> state.hasNoUsages()).forEach(GraphUtil::killWithUnusedFloatingInputs);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
private static boolean hasFloatingDeopts(StructuredGraph graph) {
|
|
132 |
for (Node n : graph.getNodes()) {
|
|
133 |
if (n instanceof DeoptimizingNode && GraphUtil.isFloatingNode(n)) {
|
|
134 |
DeoptimizingNode deoptimizingNode = (DeoptimizingNode) n;
|
|
135 |
if (deoptimizingNode.canDeoptimize()) {
|
|
136 |
return true;
|
|
137 |
}
|
|
138 |
}
|
|
139 |
}
|
|
140 |
return false;
|
|
141 |
}
|
|
142 |
|
|
143 |
private static FrameState singleFrameState(List<FrameState> states) {
|
|
144 |
FrameState singleState = states.get(0);
|
|
145 |
for (int i = 1; i < states.size(); ++i) {
|
|
146 |
if (states.get(i) != singleState) {
|
|
147 |
return null;
|
|
148 |
}
|
|
149 |
}
|
46807
|
150 |
if (singleState != null && singleState.bci != BytecodeFrame.INVALID_FRAMESTATE_BCI) {
|
|
151 |
return singleState;
|
|
152 |
}
|
|
153 |
return null;
|
43972
|
154 |
}
|
|
155 |
|
|
156 |
@Override
|
|
157 |
public boolean checkContract() {
|
|
158 |
// TODO GR-1409
|
|
159 |
return false;
|
|
160 |
}
|
|
161 |
}
|