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.core.common.GraalOptions.UseGraalStubs;
  28 import static org.graalvm.compiler.nodeinfo.InputType.Memory;
  29 
  30 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
  31 import org.graalvm.compiler.core.common.type.StampFactory;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.nodeinfo.NodeCycles;
  34 import org.graalvm.compiler.nodeinfo.NodeInfo;
  35 import org.graalvm.compiler.nodeinfo.NodeSize;
  36 import org.graalvm.compiler.nodes.FixedWithNextNode;
  37 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  38 import org.graalvm.compiler.nodes.ValueNode;
  39 import org.graalvm.compiler.nodes.ValueNodeUtil;
  40 import org.graalvm.compiler.nodes.memory.MemoryAccess;
  41 import org.graalvm.compiler.nodes.memory.MemoryNode;
  42 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  43 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  44 import jdk.internal.vm.compiler.word.LocationIdentity;
  45 import jdk.internal.vm.compiler.word.Pointer;
  46 
  47 import jdk.vm.ci.meta.JavaKind;
  48 import jdk.vm.ci.meta.Value;
  49 
  50 // JaCoCo Exclude
  51 
  52 /**
  53  * Compares two array regions with a given length.
  54  */
  55 @NodeInfo(cycles = NodeCycles.CYCLES_UNKNOWN, size = NodeSize.SIZE_128)
  56 public final class ArrayRegionEqualsNode extends FixedWithNextNode implements LIRLowerable, MemoryAccess {
  57 
  58     public static final NodeClass<ArrayRegionEqualsNode> TYPE = NodeClass.create(ArrayRegionEqualsNode.class);
  59 
  60     /** {@link JavaKind} of the arrays to compare. */
  61     private final JavaKind kind1;
  62     private final JavaKind kind2;
  63 
  64     /** Pointer to first array region to be tested for equality. */
  65     @Input private ValueNode array1;
  66 
  67     /** Pointer to second array region to be tested for equality. */
  68     @Input private ValueNode array2;
  69 
  70     /** Length of the array region. */
  71     @Input private ValueNode length;
  72 
  73     @OptionalInput(Memory) private MemoryNode lastLocationAccess;
  74 
  75     public ArrayRegionEqualsNode(ValueNode array1, ValueNode array2, ValueNode length, @ConstantNodeParameter JavaKind kind1, @ConstantNodeParameter JavaKind kind2) {
  76         super(TYPE, StampFactory.forKind(JavaKind.Boolean));
  77         this.kind1 = kind1;
  78         this.kind2 = kind2;
  79         this.array1 = array1;
  80         this.array2 = array2;
  81         this.length = length;
  82     }
  83 
  84     public static boolean regionEquals(Pointer array1, Pointer array2, int length, @ConstantNodeParameter JavaKind kind) {
  85         return regionEquals(array1, array2, length, kind, kind);
  86     }
  87 
  88     @NodeIntrinsic
  89     public static native boolean regionEquals(Pointer array1, Pointer array2, int length, @ConstantNodeParameter JavaKind kind1, @ConstantNodeParameter JavaKind kind2);
  90 
  91     public JavaKind getKind1() {
  92         return kind1;
  93     }
  94 
  95     public JavaKind getKind2() {
  96         return kind2;
  97     }
  98 
  99     public ValueNode getLength() {
 100         return length;
 101     }
 102 
 103     @Override
 104     public void generate(NodeLIRBuilderTool gen) {
 105         if (UseGraalStubs.getValue(graph().getOptions())) {
 106             ForeignCallLinkage linkage = gen.lookupGraalStub(this);
 107             if (linkage != null) {
 108                 Value result = gen.getLIRGeneratorTool().emitForeignCall(linkage, null, gen.operand(array1), gen.operand(array2), gen.operand(length));
 109                 gen.setResult(this, result);
 110                 return;
 111             }
 112         }
 113         Value result;
 114         if (kind1 == kind2) {
 115             result = gen.getLIRGeneratorTool().emitArrayEquals(kind1, gen.operand(array1), gen.operand(array2), gen.operand(length), true);
 116         } else {
 117             result = gen.getLIRGeneratorTool().emitArrayEquals(kind1, kind2, gen.operand(array1), gen.operand(array2), gen.operand(length), true);
 118         }
 119         gen.setResult(this, result);
 120     }
 121 
 122     @Override
 123     public LocationIdentity getLocationIdentity() {
 124         return kind1 != kind2 ? LocationIdentity.ANY_LOCATION : NamedLocationIdentity.getArrayLocation(kind1);
 125     }
 126 
 127     @Override
 128     public MemoryNode getLastLocationAccess() {
 129         return lastLocationAccess;
 130     }
 131 
 132     @Override
 133     public void setLastLocationAccess(MemoryNode lla) {
 134         updateUsages(ValueNodeUtil.asNode(lastLocationAccess), ValueNodeUtil.asNode(lla));
 135         lastLocationAccess = lla;
 136     }
 137 }