1 /*
   2  * Copyright (c) 2013, 2019, 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         this.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         this(type, src, srcPos, dest, destPos, length, elementKind, BytecodeFrame.INVALID_FRAMESTATE_BCI);
  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         assert length >= 0;
 148         return position >= 0 && position <= virtualObject.entryCount() - length;
 149     }
 150 
 151     private static boolean checkEntryTypes(int srcPos, int length, VirtualObjectNode src, ResolvedJavaType destComponentType, VirtualizerTool tool) {
 152         if (destComponentType.getJavaKind() == JavaKind.Object && !destComponentType.isJavaLangObject()) {
 153             for (int i = 0; i < length; i++) {
 154                 ValueNode entry = tool.getEntry(src, srcPos + i);
 155                 ResolvedJavaType type = StampTool.typeOrNull(entry);
 156                 if (type == null || !destComponentType.isAssignableFrom(type)) {
 157                     return false;
 158                 }
 159             }
 160         }
 161         return true;
 162     }
 163 
 164     /*
 165      * Returns true if this copy doesn't require store checks. Trivially true for primitive arrays.
 166      */
 167     public boolean isExact() {
 168         ResolvedJavaType srcType = StampTool.typeOrNull(getSource().stamp(NodeView.DEFAULT));
 169         ResolvedJavaType destType = StampTool.typeOrNull(getDestination().stamp(NodeView.DEFAULT));
 170         if (srcType == null || !srcType.isArray() || destType == null || !destType.isArray()) {
 171             return false;
 172         }
 173         if ((srcType.getComponentType().getJavaKind().isPrimitive() && destType.getComponentType().equals(srcType.getComponentType())) || getSource() == getDestination()) {
 174             return true;
 175         }
 176 
 177         if (StampTool.isExactType(getDestination().stamp(NodeView.DEFAULT))) {
 178             if (destType != null && destType.isAssignableFrom(srcType)) {
 179                 return true;
 180             }
 181         }
 182         return false;
 183     }
 184 
 185     @Override
 186     public void virtualize(VirtualizerTool tool) {
 187         ValueNode sourcePosition = tool.getAlias(getSourcePosition());
 188         ValueNode destinationPosition = tool.getAlias(getDestinationPosition());
 189         ValueNode replacedLength = tool.getAlias(getLength());
 190 
 191         if (sourcePosition.isConstant() && destinationPosition.isConstant() && replacedLength.isConstant()) {
 192             int srcPosInt = sourcePosition.asJavaConstant().asInt();
 193             int destPosInt = destinationPosition.asJavaConstant().asInt();
 194             int len = replacedLength.asJavaConstant().asInt();
 195             ValueNode destAlias = tool.getAlias(getDestination());
 196 
 197             if (destAlias instanceof VirtualArrayNode) {
 198                 VirtualArrayNode destVirtual = (VirtualArrayNode) destAlias;
 199                 if (len < 0 || !checkBounds(destPosInt, len, destVirtual)) {
 200                     return;
 201                 }
 202                 ValueNode srcAlias = tool.getAlias(getSource());
 203 
 204                 if (srcAlias instanceof VirtualObjectNode) {
 205                     if (!(srcAlias instanceof VirtualArrayNode)) {
 206                         return;
 207                     }
 208                     VirtualArrayNode srcVirtual = (VirtualArrayNode) srcAlias;
 209                     if (destVirtual.componentType().getJavaKind() != srcVirtual.componentType().getJavaKind()) {
 210                         return;
 211                     }
 212                     if (!checkBounds(srcPosInt, len, srcVirtual)) {
 213                         return;
 214                     }
 215                     if (!checkEntryTypes(srcPosInt, len, srcVirtual, destVirtual.type().getComponentType(), tool)) {
 216                         return;
 217                     }
 218                     for (int i = 0; i < len; i++) {
 219                         tool.setVirtualEntry(destVirtual, destPosInt + i, tool.getEntry(srcVirtual, srcPosInt + i));
 220                     }
 221                     tool.delete();
 222                     DebugContext debug = getDebug();
 223                     if (debug.isLogEnabled()) {
 224                         debug.log("virtualized arraycopy(%s, %d, %s, %d, %d)", getSource(), srcPosInt, getDestination(), destPosInt, len);
 225                     }
 226                 } else {
 227                     ResolvedJavaType sourceType = StampTool.typeOrNull(srcAlias);
 228                     if (sourceType == null || !sourceType.isArray()) {
 229                         return;
 230                     }
 231                     ResolvedJavaType sourceComponentType = sourceType.getComponentType();
 232                     ResolvedJavaType destComponentType = destVirtual.type().getComponentType();
 233                     if (!sourceComponentType.equals(destComponentType)) {
 234                         return;
 235                     }
 236                     for (int i = 0; i < len; i++) {
 237                         LoadIndexedNode load = new LoadIndexedNode(graph().getAssumptions(), srcAlias, ConstantNode.forInt(i + srcPosInt, graph()), null, destComponentType.getJavaKind());
 238                         load.setNodeSourcePosition(getNodeSourcePosition());
 239                         tool.addNode(load);
 240                         tool.setVirtualEntry(destVirtual, destPosInt + i, load);
 241                     }
 242                     tool.delete();
 243                 }
 244             }
 245         }
 246     }
 247 
 248     @Override
 249     public boolean canDeoptimize() {
 250         return true;
 251     }
 252 
 253     @Override
 254     public FrameState stateDuring() {
 255         return stateDuring;
 256     }
 257 
 258     @Override
 259     public void setStateDuring(FrameState stateDuring) {
 260         updateUsages(this.stateDuring, stateDuring);
 261         this.stateDuring = stateDuring;
 262     }
 263 
 264     @Override
 265     public void computeStateDuring(FrameState currentStateAfter) {
 266         FrameState newStateDuring = currentStateAfter.duplicateModifiedDuringCall(getBci(), asNode().getStackKind());
 267         setStateDuring(newStateDuring);
 268     }
 269 }