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 
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.hotspot.HotSpotBackend;
  32 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  33 import org.graalvm.compiler.hotspot.nodes.GetObjectAddressNode;
  34 import org.graalvm.compiler.nodeinfo.InputType;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.ValueNode;
  38 import org.graalvm.compiler.nodes.calc.IntegerConvertNode;
  39 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  40 import org.graalvm.compiler.nodes.memory.AbstractMemoryCheckpoint;
  41 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  42 import org.graalvm.compiler.nodes.spi.Lowerable;
  43 import org.graalvm.compiler.nodes.spi.LoweringTool;
  44 import org.graalvm.word.LocationIdentity;
  45 
  46 import jdk.vm.ci.meta.JavaKind;
  47 
  48 @NodeInfo(allowedUsageTypes = {InputType.Memory, InputType.Value}, cycles = CYCLES_UNKNOWN, size = SIZE_UNKNOWN)
  49 public final class GenericArrayCopyCallNode extends AbstractMemoryCheckpoint implements Lowerable, MemoryCheckpoint.Single {
  50 
  51     public static final NodeClass<GenericArrayCopyCallNode> TYPE = NodeClass.create(GenericArrayCopyCallNode.class);
  52     @Input ValueNode src;
  53     @Input ValueNode srcPos;
  54     @Input ValueNode dest;
  55     @Input ValueNode destPos;
  56     @Input ValueNode length;
  57 
  58     protected final HotSpotGraalRuntimeProvider runtime;
  59 
  60     protected GenericArrayCopyCallNode(@InjectedNodeParameter HotSpotGraalRuntimeProvider runtime, ValueNode src, ValueNode srcPos, ValueNode dest, ValueNode destPos, ValueNode length) {
  61         super(TYPE, StampFactory.forKind(JavaKind.Int));
  62         this.src = src;
  63         this.srcPos = srcPos;
  64         this.dest = dest;
  65         this.destPos = destPos;
  66         this.length = length;
  67         this.runtime = runtime;
  68     }
  69 
  70     public ValueNode getSource() {
  71         return src;
  72     }
  73 
  74     public ValueNode getSourcePosition() {
  75         return srcPos;
  76     }
  77 
  78     public ValueNode getDestination() {
  79         return dest;
  80     }
  81 
  82     public ValueNode getDestinationPosition() {
  83         return destPos;
  84     }
  85 
  86     public ValueNode getLength() {
  87         return length;
  88     }
  89 
  90     @Override
  91     public void lower(LoweringTool tool) {
  92         if (graph().getGuardsStage().areFrameStatesAtDeopts()) {
  93             StructuredGraph graph = graph();
  94             ValueNode srcAddr = objectAddress(getSource());
  95             ValueNode destAddr = objectAddress(getDestination());
  96             ForeignCallNode call = graph.add(new ForeignCallNode(runtime.getHostBackend().getForeignCalls(), HotSpotBackend.GENERIC_ARRAYCOPY, srcAddr, srcPos, destAddr, destPos, length));
  97             call.setStateAfter(stateAfter());
  98             graph.replaceFixedWithFixed(this, call);
  99         }
 100     }
 101 
 102     private ValueNode objectAddress(ValueNode obj) {
 103         GetObjectAddressNode result = graph().add(new GetObjectAddressNode(obj));
 104         graph().addBeforeFixed(this, result);
 105         return result;
 106     }
 107 
 108     private ValueNode wordValue(ValueNode value) {
 109         if (value.stamp().getStackKind() != runtime.getTarget().wordJavaKind) {
 110             return IntegerConvertNode.convert(value, StampFactory.forKind(runtime.getTarget().wordJavaKind), graph());
 111         }
 112         return value;
 113     }
 114 
 115     @Override
 116     public LocationIdentity getLocationIdentity() {
 117         return LocationIdentity.any();
 118     }
 119 
 120     @NodeIntrinsic
 121     public static native int genericArraycopy(Object src, int srcPos, Object dest, int destPos, int length);
 122 }