43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2009, 2015, 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.nodes.java;
|
|
24 |
|
|
25 |
import java.util.Collections;
|
|
26 |
|
48861
|
27 |
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
|
46344
|
28 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
|
29 |
import org.graalvm.compiler.core.common.type.Stamp;
|
43972
|
30 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
|
31 |
import org.graalvm.compiler.core.common.type.TypeReference;
|
|
32 |
import org.graalvm.compiler.graph.NodeClass;
|
46344
|
33 |
import org.graalvm.compiler.graph.spi.Simplifiable;
|
|
34 |
import org.graalvm.compiler.graph.spi.SimplifierTool;
|
43972
|
35 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
36 |
import org.graalvm.compiler.nodes.ConstantNode;
|
46344
|
37 |
import org.graalvm.compiler.nodes.FixedGuardNode;
|
43972
|
38 |
import org.graalvm.compiler.nodes.FrameState;
|
46344
|
39 |
import org.graalvm.compiler.nodes.LogicNode;
|
48190
|
40 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
41 |
import org.graalvm.compiler.nodes.ValueNode;
|
46344
|
42 |
import org.graalvm.compiler.nodes.calc.CompareNode;
|
43972
|
43 |
import org.graalvm.compiler.nodes.spi.VirtualizableAllocation;
|
|
44 |
import org.graalvm.compiler.nodes.spi.VirtualizerTool;
|
46344
|
45 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
43972
|
46 |
import org.graalvm.compiler.nodes.virtual.VirtualArrayNode;
|
|
47 |
import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
|
|
48 |
|
46344
|
49 |
import jdk.vm.ci.meta.DeoptimizationAction;
|
|
50 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
43972
|
51 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* The {@code NewArrayNode} is used for all array allocations where the element type is know at
|
|
55 |
* compile time.
|
|
56 |
*/
|
|
57 |
// JaCoCo Exclude
|
|
58 |
@NodeInfo
|
46344
|
59 |
public class NewArrayNode extends AbstractNewArrayNode implements VirtualizableAllocation, Simplifiable {
|
43972
|
60 |
|
|
61 |
public static final NodeClass<NewArrayNode> TYPE = NodeClass.create(NewArrayNode.class);
|
|
62 |
private final ResolvedJavaType elementType;
|
|
63 |
|
|
64 |
public NewArrayNode(ResolvedJavaType elementType, ValueNode length, boolean fillContents) {
|
|
65 |
this(elementType, length, fillContents, null);
|
|
66 |
}
|
|
67 |
|
|
68 |
public NewArrayNode(ResolvedJavaType elementType, ValueNode length, boolean fillContents, FrameState stateBefore) {
|
|
69 |
this(TYPE, elementType, length, fillContents, stateBefore);
|
|
70 |
}
|
|
71 |
|
|
72 |
protected NewArrayNode(NodeClass<? extends NewArrayNode> c, ResolvedJavaType elementType, ValueNode length, boolean fillContents, FrameState stateBefore) {
|
|
73 |
super(c, StampFactory.objectNonNull(TypeReference.createExactTrusted(elementType.getArrayClass())), length, fillContents, stateBefore);
|
|
74 |
this.elementType = elementType;
|
|
75 |
}
|
|
76 |
|
|
77 |
@NodeIntrinsic
|
|
78 |
private static native Object newArray(@ConstantNodeParameter Class<?> elementType, int length, @ConstantNodeParameter boolean fillContents);
|
|
79 |
|
|
80 |
public static Object newUninitializedArray(Class<?> elementType, int length) {
|
|
81 |
return newArray(elementType, length, false);
|
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Gets the element type of the array.
|
|
86 |
*
|
|
87 |
* @return the element type of the array
|
|
88 |
*/
|
|
89 |
public ResolvedJavaType elementType() {
|
|
90 |
return elementType;
|
|
91 |
}
|
|
92 |
|
|
93 |
@Override
|
|
94 |
public void virtualize(VirtualizerTool tool) {
|
|
95 |
ValueNode lengthAlias = tool.getAlias(length());
|
|
96 |
if (lengthAlias.asConstant() != null) {
|
|
97 |
int constantLength = lengthAlias.asJavaConstant().asInt();
|
|
98 |
if (constantLength >= 0 && constantLength < tool.getMaximumEntryCount()) {
|
|
99 |
ValueNode[] state = new ValueNode[constantLength];
|
|
100 |
ConstantNode defaultForKind = constantLength == 0 ? null : defaultElementValue();
|
|
101 |
for (int i = 0; i < constantLength; i++) {
|
|
102 |
state[i] = defaultForKind;
|
|
103 |
}
|
|
104 |
VirtualObjectNode virtualObject = createVirtualArrayNode(constantLength);
|
|
105 |
tool.createVirtualObject(virtualObject, state, Collections.<MonitorIdNode> emptyList(), false);
|
|
106 |
tool.replaceWithVirtual(virtualObject);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
protected VirtualArrayNode createVirtualArrayNode(int constantLength) {
|
|
112 |
return new VirtualArrayNode(elementType(), constantLength);
|
|
113 |
}
|
|
114 |
|
|
115 |
/* Factored out in a separate method so that subclasses can override it. */
|
|
116 |
protected ConstantNode defaultElementValue() {
|
|
117 |
return ConstantNode.defaultForKind(elementType().getJavaKind(), graph());
|
|
118 |
}
|
46344
|
119 |
|
|
120 |
@Override
|
|
121 |
public void simplify(SimplifierTool tool) {
|
|
122 |
if (hasNoUsages()) {
|
48190
|
123 |
NodeView view = NodeView.from(tool);
|
|
124 |
Stamp lengthStamp = length().stamp(view);
|
46344
|
125 |
if (lengthStamp instanceof IntegerStamp) {
|
|
126 |
IntegerStamp lengthIntegerStamp = (IntegerStamp) lengthStamp;
|
|
127 |
if (lengthIntegerStamp.isPositive()) {
|
|
128 |
GraphUtil.removeFixedWithUnusedInputs(this);
|
|
129 |
return;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
// Should be areFrameStatesAtSideEffects but currently SVM will complain about
|
|
133 |
// RuntimeConstraint
|
|
134 |
if (graph().getGuardsStage().allowsFloatingGuards()) {
|
48861
|
135 |
LogicNode lengthNegativeCondition = CompareNode.createCompareNode(graph(), CanonicalCondition.LT, length(), ConstantNode.forInt(0, graph()), tool.getConstantReflection(), view);
|
46344
|
136 |
// we do not have a non-deopting path for that at the moment so action=None.
|
|
137 |
FixedGuardNode guard = graph().add(new FixedGuardNode(lengthNegativeCondition, DeoptimizationReason.RuntimeConstraint, DeoptimizationAction.None, true));
|
|
138 |
graph().replaceFixedWithFixed(this, guard);
|
|
139 |
}
|
|
140 |
}
|
|
141 |
}
|
43972
|
142 |
}
|