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