BasicArrayCopyNode.java revision 13175:3df8ef613001
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 */
23package org.graalvm.compiler.replacements.nodes;
24
25import static org.graalvm.compiler.nodeinfo.InputType.Memory;
26import static org.graalvm.compiler.nodeinfo.InputType.State;
27import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_256;
28import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_64;
29import static org.graalvm.word.LocationIdentity.any;
30
31import org.graalvm.compiler.core.common.type.StampFactory;
32import org.graalvm.compiler.debug.Debug;
33import org.graalvm.compiler.graph.NodeClass;
34import org.graalvm.compiler.graph.NodeInputList;
35import org.graalvm.compiler.nodeinfo.NodeInfo;
36import org.graalvm.compiler.nodes.ConstantNode;
37import org.graalvm.compiler.nodes.DeoptimizingNode;
38import org.graalvm.compiler.nodes.FrameState;
39import org.graalvm.compiler.nodes.NamedLocationIdentity;
40import org.graalvm.compiler.nodes.ValueNode;
41import org.graalvm.compiler.nodes.java.LoadIndexedNode;
42import org.graalvm.compiler.nodes.memory.AbstractMemoryCheckpoint;
43import org.graalvm.compiler.nodes.memory.MemoryAccess;
44import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
45import org.graalvm.compiler.nodes.memory.MemoryNode;
46import org.graalvm.compiler.nodes.spi.Lowerable;
47import org.graalvm.compiler.nodes.spi.LoweringTool;
48import org.graalvm.compiler.nodes.spi.Virtualizable;
49import org.graalvm.compiler.nodes.spi.VirtualizerTool;
50import org.graalvm.compiler.nodes.type.StampTool;
51import org.graalvm.compiler.nodes.virtual.VirtualArrayNode;
52import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
53import org.graalvm.word.LocationIdentity;
54
55import jdk.vm.ci.code.BytecodeFrame;
56import jdk.vm.ci.meta.JavaKind;
57import jdk.vm.ci.meta.ResolvedJavaType;
58
59@NodeInfo(cycles = CYCLES_256, size = SIZE_64)
60public class BasicArrayCopyNode extends AbstractMemoryCheckpoint implements Virtualizable, MemoryCheckpoint.Single, MemoryAccess, Lowerable, DeoptimizingNode.DeoptDuring {
61
62    public static final NodeClass<BasicArrayCopyNode> TYPE = NodeClass.create(BasicArrayCopyNode.class);
63
64    static final int SRC_ARG = 0;
65    static final int SRC_POS_ARG = 1;
66    static final int DEST_ARG = 2;
67    static final int DEST_POS_ARG = 3;
68    static final int LENGTH_ARG = 4;
69
70    @Input NodeInputList<ValueNode> args;
71
72    @OptionalInput(State) FrameState stateDuring;
73
74    @OptionalInput(Memory) protected MemoryNode lastLocationAccess;
75
76    protected JavaKind elementKind;
77
78    protected int bci;
79
80    public BasicArrayCopyNode(NodeClass<? extends AbstractMemoryCheckpoint> type, ValueNode src, ValueNode srcPos, ValueNode dest, ValueNode destPos, ValueNode length, JavaKind elementKind, int bci) {
81        super(type, StampFactory.forKind(JavaKind.Void));
82        this.bci = bci;
83        args = new NodeInputList<>(this, new ValueNode[]{src, srcPos, dest, destPos, length});
84        this.elementKind = elementKind != JavaKind.Illegal ? elementKind : null;
85    }
86
87    public BasicArrayCopyNode(NodeClass<? extends AbstractMemoryCheckpoint> type, ValueNode src, ValueNode srcPos, ValueNode dest, ValueNode destPos, ValueNode length, JavaKind elementKind) {
88        super(type, StampFactory.forKind(JavaKind.Void));
89        this.bci = BytecodeFrame.INVALID_FRAMESTATE_BCI;
90        args = new NodeInputList<>(this, new ValueNode[]{src, srcPos, dest, destPos, length});
91        this.elementKind = elementKind != JavaKind.Illegal ? elementKind : null;
92    }
93
94    public ValueNode getSource() {
95        return args.get(SRC_ARG);
96    }
97
98    public ValueNode getSourcePosition() {
99        return args.get(SRC_POS_ARG);
100    }
101
102    public ValueNode getDestination() {
103        return args.get(DEST_ARG);
104    }
105
106    public ValueNode getDestinationPosition() {
107        return args.get(DEST_POS_ARG);
108    }
109
110    public ValueNode getLength() {
111        return args.get(LENGTH_ARG);
112    }
113
114    public int getBci() {
115        return bci;
116    }
117
118    public JavaKind getElementKind() {
119        return elementKind;
120    }
121
122    @Override
123    public LocationIdentity getLocationIdentity() {
124        if (elementKind != null) {
125            return NamedLocationIdentity.getArrayLocation(elementKind);
126        }
127        return any();
128    }
129
130    @Override
131    public MemoryNode getLastLocationAccess() {
132        return lastLocationAccess;
133    }
134
135    @Override
136    public void setLastLocationAccess(MemoryNode lla) {
137        updateUsagesInterface(lastLocationAccess, lla);
138        lastLocationAccess = lla;
139    }
140
141    @Override
142    public void lower(LoweringTool tool) {
143        tool.getLowerer().lower(this, tool);
144    }
145
146    private static boolean checkBounds(int position, int length, VirtualObjectNode virtualObject) {
147        return position >= 0 && position + length <= virtualObject.entryCount();
148    }
149
150    private static boolean checkEntryTypes(int srcPos, int length, VirtualObjectNode src, ResolvedJavaType destComponentType, VirtualizerTool tool) {
151        if (destComponentType.getJavaKind() == JavaKind.Object && !destComponentType.isJavaLangObject()) {
152            for (int i = 0; i < length; i++) {
153                ValueNode entry = tool.getEntry(src, srcPos + i);
154                ResolvedJavaType type = StampTool.typeOrNull(entry);
155                if (type == null || !destComponentType.isAssignableFrom(type)) {
156                    return false;
157                }
158            }
159        }
160        return true;
161    }
162
163    /*
164     * Returns true if this copy doesn't require store checks. Trivially true for primitive arrays.
165     */
166    public boolean isExact() {
167        ResolvedJavaType srcType = StampTool.typeOrNull(getSource().stamp());
168        ResolvedJavaType destType = StampTool.typeOrNull(getDestination().stamp());
169        if (srcType == null || !srcType.isArray() || destType == null || !destType.isArray()) {
170            return false;
171        }
172        if ((srcType.getComponentType().getJavaKind().isPrimitive() && destType.getComponentType().equals(srcType.getComponentType())) || getSource() == getDestination()) {
173            return true;
174        }
175
176        if (StampTool.isExactType(getDestination().stamp())) {
177            if (destType != null && destType.isAssignableFrom(srcType)) {
178                return true;
179            }
180        }
181        return false;
182    }
183
184    @Override
185    public void virtualize(VirtualizerTool tool) {
186        ValueNode sourcePosition = tool.getAlias(getSourcePosition());
187        ValueNode destinationPosition = tool.getAlias(getDestinationPosition());
188        ValueNode replacedLength = tool.getAlias(getLength());
189
190        if (sourcePosition.isConstant() && destinationPosition.isConstant() && replacedLength.isConstant()) {
191            int srcPosInt = sourcePosition.asJavaConstant().asInt();
192            int destPosInt = destinationPosition.asJavaConstant().asInt();
193            int len = replacedLength.asJavaConstant().asInt();
194            ValueNode destAlias = tool.getAlias(getDestination());
195
196            if (destAlias instanceof VirtualArrayNode) {
197                VirtualArrayNode destVirtual = (VirtualArrayNode) destAlias;
198                if (len < 0 || !checkBounds(destPosInt, len, destVirtual)) {
199                    return;
200                }
201                ValueNode srcAlias = tool.getAlias(getSource());
202
203                if (srcAlias instanceof VirtualObjectNode) {
204                    if (!(srcAlias instanceof VirtualArrayNode)) {
205                        return;
206                    }
207                    VirtualArrayNode srcVirtual = (VirtualArrayNode) srcAlias;
208                    if (destVirtual.componentType().getJavaKind() != srcVirtual.componentType().getJavaKind()) {
209                        return;
210                    }
211                    if (!checkBounds(srcPosInt, len, srcVirtual)) {
212                        return;
213                    }
214                    if (!checkEntryTypes(srcPosInt, len, srcVirtual, destVirtual.type().getComponentType(), tool)) {
215                        return;
216                    }
217                    for (int i = 0; i < len; i++) {
218                        tool.setVirtualEntry(destVirtual, destPosInt + i, tool.getEntry(srcVirtual, srcPosInt + i), false);
219                    }
220                    tool.delete();
221                    if (Debug.isLogEnabled()) {
222                        Debug.log("virtualized arraycopyf(%s, %d, %s, %d, %d)", getSource(), srcPosInt, getDestination(), destPosInt, len);
223                    }
224                } else {
225                    ResolvedJavaType sourceType = StampTool.typeOrNull(srcAlias);
226                    if (sourceType == null || !sourceType.isArray()) {
227                        return;
228                    }
229                    ResolvedJavaType sourceComponentType = sourceType.getComponentType();
230                    ResolvedJavaType destComponentType = destVirtual.type().getComponentType();
231                    if (!sourceComponentType.equals(destComponentType)) {
232                        return;
233                    }
234                    for (int i = 0; i < len; i++) {
235                        LoadIndexedNode load = new LoadIndexedNode(graph().getAssumptions(), srcAlias, ConstantNode.forInt(i + srcPosInt, graph()), destComponentType.getJavaKind());
236                        tool.addNode(load);
237                        tool.setVirtualEntry(destVirtual, destPosInt + i, load, false);
238                    }
239                    tool.delete();
240                }
241            }
242        }
243    }
244
245    @Override
246    public boolean canDeoptimize() {
247        return true;
248    }
249
250    @Override
251    public FrameState stateDuring() {
252        return stateDuring;
253    }
254
255    @Override
256    public void setStateDuring(FrameState stateDuring) {
257        updateUsages(this.stateDuring, stateDuring);
258        this.stateDuring = stateDuring;
259    }
260
261    @Override
262    public void computeStateDuring(FrameState currentStateAfter) {
263        FrameState newStateDuring = currentStateAfter.duplicateModifiedDuringCall(getBci(), asNode().getStackKind());
264        setStateDuring(newStateDuring);
265    }
266}
267