43972
|
1 |
/*
|
58533
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.nodes;
|
|
26 |
|
|
27 |
import java.util.Iterator;
|
|
28 |
import java.util.NoSuchElementException;
|
|
29 |
|
|
30 |
import org.graalvm.compiler.core.common.type.Stamp;
|
|
31 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
32 |
import org.graalvm.compiler.graph.IterableNodeType;
|
|
33 |
import org.graalvm.compiler.graph.Node;
|
|
34 |
import org.graalvm.compiler.graph.NodeClass;
|
58533
|
35 |
import org.graalvm.compiler.graph.Position;
|
43972
|
36 |
import org.graalvm.compiler.graph.iterators.NodeIterable;
|
|
37 |
import org.graalvm.compiler.nodeinfo.InputType;
|
|
38 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
39 |
import org.graalvm.compiler.nodes.extended.AnchoringNode;
|
|
40 |
import org.graalvm.compiler.nodes.extended.GuardingNode;
|
|
41 |
import org.graalvm.compiler.nodes.spi.LIRLowerable;
|
|
42 |
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
|
|
43 |
|
|
44 |
@NodeInfo(allowedUsageTypes = {InputType.Guard, InputType.Anchor})
|
|
45 |
public abstract class AbstractBeginNode extends FixedWithNextNode implements LIRLowerable, GuardingNode, AnchoringNode, IterableNodeType {
|
|
46 |
|
|
47 |
public static final NodeClass<AbstractBeginNode> TYPE = NodeClass.create(AbstractBeginNode.class);
|
|
48 |
|
52578
|
49 |
private boolean withSpeculationFence;
|
|
50 |
|
43972
|
51 |
protected AbstractBeginNode(NodeClass<? extends AbstractBeginNode> c) {
|
|
52 |
this(c, StampFactory.forVoid());
|
|
53 |
}
|
|
54 |
|
|
55 |
protected AbstractBeginNode(NodeClass<? extends AbstractBeginNode> c, Stamp stamp) {
|
|
56 |
super(c, stamp);
|
|
57 |
}
|
|
58 |
|
|
59 |
public static AbstractBeginNode prevBegin(FixedNode from) {
|
|
60 |
Node next = from;
|
|
61 |
while (next != null) {
|
|
62 |
if (next instanceof AbstractBeginNode) {
|
57537
|
63 |
return (AbstractBeginNode) next;
|
43972
|
64 |
}
|
|
65 |
next = next.predecessor();
|
|
66 |
}
|
|
67 |
return null;
|
|
68 |
}
|
|
69 |
|
57537
|
70 |
private void evacuateAnchored(FixedNode evacuateFrom) {
|
43972
|
71 |
if (!hasNoUsages()) {
|
|
72 |
AbstractBeginNode prevBegin = prevBegin(evacuateFrom);
|
|
73 |
assert prevBegin != null;
|
57537
|
74 |
replaceAtUsages(InputType.Anchor, prevBegin);
|
|
75 |
replaceAtUsages(InputType.Guard, prevBegin);
|
|
76 |
assert anchored().isEmpty() : anchored().snapshot();
|
43972
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
public void prepareDelete() {
|
|
81 |
prepareDelete((FixedNode) predecessor());
|
|
82 |
}
|
|
83 |
|
|
84 |
public void prepareDelete(FixedNode evacuateFrom) {
|
57537
|
85 |
evacuateAnchored(evacuateFrom);
|
43972
|
86 |
}
|
|
87 |
|
|
88 |
@Override
|
|
89 |
public boolean verify() {
|
|
90 |
assertTrue(predecessor() != null || this == graph().start() || this instanceof AbstractMergeNode, "begin nodes must be connected");
|
|
91 |
return super.verify();
|
|
92 |
}
|
|
93 |
|
|
94 |
@Override
|
|
95 |
public void generate(NodeLIRBuilderTool gen) {
|
52578
|
96 |
if (withSpeculationFence) {
|
|
97 |
gen.getLIRGeneratorTool().emitSpeculationFence();
|
|
98 |
}
|
43972
|
99 |
}
|
|
100 |
|
58533
|
101 |
public boolean isUsedAsGuardInput() {
|
|
102 |
if (this.hasUsages()) {
|
|
103 |
for (Node n : usages()) {
|
|
104 |
for (Position inputPosition : n.inputPositions()) {
|
|
105 |
if (inputPosition.getInputType() == InputType.Guard && inputPosition.get(n) == this) {
|
|
106 |
return true;
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|
|
111 |
return false;
|
|
112 |
}
|
|
113 |
|
43972
|
114 |
public NodeIterable<GuardNode> guards() {
|
|
115 |
return usages().filter(GuardNode.class);
|
|
116 |
}
|
|
117 |
|
|
118 |
public NodeIterable<Node> anchored() {
|
|
119 |
return usages();
|
|
120 |
}
|
|
121 |
|
58533
|
122 |
public boolean hasAnchored() {
|
|
123 |
return this.hasUsages();
|
|
124 |
}
|
|
125 |
|
43972
|
126 |
public NodeIterable<FixedNode> getBlockNodes() {
|
|
127 |
return new NodeIterable<FixedNode>() {
|
|
128 |
|
|
129 |
@Override
|
|
130 |
public Iterator<FixedNode> iterator() {
|
|
131 |
return new BlockNodeIterator(AbstractBeginNode.this);
|
|
132 |
}
|
|
133 |
};
|
|
134 |
}
|
|
135 |
|
52578
|
136 |
/**
|
|
137 |
* Set this begin node to be a speculation fence. This will prevent speculative execution of
|
|
138 |
* this block.
|
|
139 |
*/
|
|
140 |
public void setWithSpeculationFence() {
|
|
141 |
this.withSpeculationFence = true;
|
|
142 |
}
|
|
143 |
|
51436
|
144 |
private static class BlockNodeIterator implements Iterator<FixedNode> {
|
43972
|
145 |
|
|
146 |
private FixedNode current;
|
|
147 |
|
|
148 |
BlockNodeIterator(FixedNode next) {
|
|
149 |
this.current = next;
|
|
150 |
}
|
|
151 |
|
|
152 |
@Override
|
|
153 |
public boolean hasNext() {
|
|
154 |
return current != null;
|
|
155 |
}
|
|
156 |
|
|
157 |
@Override
|
|
158 |
public FixedNode next() {
|
|
159 |
FixedNode ret = current;
|
|
160 |
if (ret == null) {
|
|
161 |
throw new NoSuchElementException();
|
|
162 |
}
|
48861
|
163 |
if (current instanceof FixedWithNextNode) {
|
|
164 |
current = ((FixedWithNextNode) current).next();
|
|
165 |
if (current instanceof AbstractBeginNode) {
|
|
166 |
current = null;
|
|
167 |
}
|
|
168 |
} else {
|
43972
|
169 |
current = null;
|
|
170 |
}
|
|
171 |
return ret;
|
|
172 |
}
|
|
173 |
|
|
174 |
@Override
|
|
175 |
public void remove() {
|
|
176 |
throw new UnsupportedOperationException();
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|