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