1 /*
   2  * Copyright (c) 2014, 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 //JaCoCo Exclude
  24 package org.graalvm.compiler.hotspot.replacements.arraycopy;
  25 
  26 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayBaseOffset;
  27 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayIndexScale;
  28 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN;
  29 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_UNKNOWN;
  30 
  31 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  32 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
  33 import org.graalvm.compiler.core.common.type.StampFactory;
  34 import org.graalvm.compiler.graph.NodeClass;
  35 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  36 import org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider;
  37 import org.graalvm.compiler.hotspot.nodes.GetObjectAddressNode;
  38 import org.graalvm.compiler.nodeinfo.InputType;
  39 import org.graalvm.compiler.nodeinfo.NodeInfo;
  40 import org.graalvm.compiler.nodes.ConstantNode;
  41 import org.graalvm.compiler.nodes.FixedWithNextNode;
  42 import org.graalvm.compiler.nodes.StructuredGraph;
  43 import org.graalvm.compiler.nodes.ValueNode;
  44 import org.graalvm.compiler.nodes.calc.AddNode;
  45 import org.graalvm.compiler.nodes.calc.IntegerConvertNode;
  46 import org.graalvm.compiler.nodes.calc.LeftShiftNode;
  47 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  48 import org.graalvm.compiler.nodes.memory.AbstractMemoryCheckpoint;
  49 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  50 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  51 import org.graalvm.compiler.nodes.spi.Lowerable;
  52 import org.graalvm.compiler.nodes.spi.LoweringTool;
  53 import org.graalvm.compiler.word.Word;
  54 import org.graalvm.word.LocationIdentity;
  55 
  56 import jdk.vm.ci.code.CodeUtil;
  57 import jdk.vm.ci.meta.JavaKind;
  58 
  59 @NodeInfo(allowedUsageTypes = {InputType.Memory, InputType.Value}, cycles = CYCLES_UNKNOWN, size = SIZE_UNKNOWN)
  60 public final class CheckcastArrayCopyCallNode extends AbstractMemoryCheckpoint implements Lowerable, MemoryCheckpoint.Single {
  61 
  62     public static final NodeClass<CheckcastArrayCopyCallNode> TYPE = NodeClass.create(CheckcastArrayCopyCallNode.class);
  63     @Input ValueNode src;
  64     @Input ValueNode srcPos;
  65     @Input ValueNode dest;
  66     @Input ValueNode destPos;
  67     @Input ValueNode length;
  68     @Input ValueNode destElemKlass;
  69     @Input ValueNode superCheckOffset;
  70 
  71     protected final boolean uninit;
  72 
  73     protected final HotSpotGraalRuntimeProvider runtime;
  74 
  75     protected CheckcastArrayCopyCallNode(@InjectedNodeParameter HotSpotGraalRuntimeProvider runtime, ValueNode src, ValueNode srcPos, ValueNode dest, ValueNode destPos, ValueNode length,
  76                     ValueNode superCheckOffset, ValueNode destElemKlass, boolean uninit) {
  77         super(TYPE, StampFactory.forKind(JavaKind.Int));
  78         this.src = src;
  79         this.srcPos = srcPos;
  80         this.dest = dest;
  81         this.destPos = destPos;
  82         this.length = length;
  83         this.superCheckOffset = superCheckOffset;
  84         this.destElemKlass = destElemKlass;
  85         this.uninit = uninit;
  86         this.runtime = runtime;
  87     }
  88 
  89     public ValueNode getSource() {
  90         return src;
  91     }
  92 
  93     public ValueNode getSourcePosition() {
  94         return srcPos;
  95     }
  96 
  97     public ValueNode getDestination() {
  98         return dest;
  99     }
 100 
 101     public ValueNode getDestinationPosition() {
 102         return destPos;
 103     }
 104 
 105     public ValueNode getLength() {
 106         return length;
 107     }
 108 
 109     public boolean isUninit() {
 110         return uninit;
 111     }
 112 
 113     private ValueNode computeBase(ValueNode base, ValueNode pos) {
 114         FixedWithNextNode basePtr = graph().add(new GetObjectAddressNode(base));
 115         graph().addBeforeFixed(this, basePtr);
 116 
 117         int shift = CodeUtil.log2(getArrayIndexScale(JavaKind.Object));
 118         ValueNode extendedPos = IntegerConvertNode.convert(pos, StampFactory.forKind(runtime.getTarget().wordJavaKind), graph());
 119         ValueNode scaledIndex = graph().unique(new LeftShiftNode(extendedPos, ConstantNode.forInt(shift, graph())));
 120         ValueNode offset = graph().unique(new AddNode(scaledIndex, ConstantNode.forIntegerBits(PrimitiveStamp.getBits(scaledIndex.stamp()), getArrayBaseOffset(JavaKind.Object), graph())));
 121         return graph().unique(new OffsetAddressNode(basePtr, offset));
 122     }
 123 
 124     @Override
 125     public void lower(LoweringTool tool) {
 126         if (graph().getGuardsStage().areFrameStatesAtDeopts()) {
 127             ForeignCallDescriptor desc = HotSpotHostForeignCallsProvider.lookupCheckcastArraycopyDescriptor(isUninit());
 128             StructuredGraph graph = graph();
 129             ValueNode srcAddr = computeBase(getSource(), getSourcePosition());
 130             ValueNode destAddr = computeBase(getDestination(), getDestinationPosition());
 131             ValueNode len = getLength();
 132             if (len.stamp().getStackKind() != runtime.getTarget().wordJavaKind) {
 133                 len = IntegerConvertNode.convert(len, StampFactory.forKind(runtime.getTarget().wordJavaKind), graph());
 134             }
 135             ForeignCallNode call = graph.add(new ForeignCallNode(runtime.getHostBackend().getForeignCalls(), desc, srcAddr, destAddr, len, superCheckOffset, destElemKlass));
 136             call.setStateAfter(stateAfter());
 137             graph.replaceFixedWithFixed(this, call);
 138         }
 139     }
 140 
 141     @Override
 142     public LocationIdentity getLocationIdentity() {
 143         /*
 144          * Because of restrictions that the memory graph of snippets matches the original node,
 145          * pretend that we kill any.
 146          */
 147         return LocationIdentity.any();
 148     }
 149 
 150     @NodeIntrinsic
 151     public static native int checkcastArraycopy(Object src, int srcPos, Object dest, int destPos, int length, Word superCheckOffset, Object destElemKlass, @ConstantNodeParameter boolean uninit);
 152 }