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