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 
  24 
  25 package org.graalvm.compiler.replacements.nodes;
  26 
  27 import jdk.vm.ci.meta.JavaKind;
  28 import jdk.vm.ci.meta.Value;
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.graph.NodeClass;
  31 import org.graalvm.compiler.nodeinfo.NodeCycles;
  32 import org.graalvm.compiler.nodeinfo.NodeInfo;
  33 import org.graalvm.compiler.nodeinfo.NodeSize;
  34 import org.graalvm.compiler.nodes.FixedWithNextNode;
  35 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.ValueNodeUtil;
  38 import org.graalvm.compiler.nodes.memory.MemoryAccess;
  39 import org.graalvm.compiler.nodes.memory.MemoryNode;
  40 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  41 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  42 import jdk.internal.vm.compiler.word.LocationIdentity;
  43 import jdk.internal.vm.compiler.word.Pointer;
  44 
  45 import static org.graalvm.compiler.nodeinfo.InputType.Memory;
  46 
  47 // JaCoCo Exclude
  48 
  49 /**
  50  * Compares two array regions with a given length.
  51  */
  52 @NodeInfo(cycles = NodeCycles.CYCLES_UNKNOWN, size = NodeSize.SIZE_128)
  53 public final class ArrayRegionEqualsNode extends FixedWithNextNode implements LIRLowerable, MemoryAccess {
  54 
  55     public static final NodeClass<ArrayRegionEqualsNode> TYPE = NodeClass.create(ArrayRegionEqualsNode.class);
  56 
  57     /** {@link JavaKind} of the arrays to compare. */
  58     private final JavaKind kind;
  59 
  60     /** Pointer to first array region to be tested for equality. */
  61     @Input private ValueNode array1;
  62 
  63     /** Pointer to second array region to be tested for equality. */
  64     @Input private ValueNode array2;
  65 
  66     /** Length of the array region. */
  67     @Input private ValueNode length;
  68 
  69     @OptionalInput(Memory) private MemoryNode lastLocationAccess;
  70 
  71     public ArrayRegionEqualsNode(ValueNode array1, ValueNode array2, ValueNode length, @ConstantNodeParameter JavaKind kind) {
  72         super(TYPE, StampFactory.forKind(JavaKind.Boolean));
  73         this.kind = kind;
  74         this.array1 = array1;
  75         this.array2 = array2;
  76         this.length = length;
  77     }
  78 
  79     @NodeIntrinsic
  80     public static native boolean regionEquals(Pointer array1, Pointer array2, int length, @ConstantNodeParameter JavaKind kind);
  81 
  82     @Override
  83     public void generate(NodeLIRBuilderTool gen) {
  84         int constantLength = -1;
  85         if (length.isConstant()) {
  86             constantLength = length.asJavaConstant().asInt();
  87         }
  88         Value result = gen.getLIRGeneratorTool().emitArrayEquals(kind, gen.operand(array1), gen.operand(array2), gen.operand(length), constantLength, true);
  89         gen.setResult(this, result);
  90     }
  91 
  92     @Override
  93     public LocationIdentity getLocationIdentity() {
  94         return NamedLocationIdentity.getArrayLocation(kind);
  95     }
  96 
  97     @Override
  98     public MemoryNode getLastLocationAccess() {
  99         return lastLocationAccess;
 100     }
 101 
 102     @Override
 103     public void setLastLocationAccess(MemoryNode lla) {
 104         updateUsages(ValueNodeUtil.asNode(lastLocationAccess), ValueNodeUtil.asNode(lla));
 105         lastLocationAccess = lla;
 106     }
 107 }