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.Node;
  33 import org.graalvm.compiler.graph.NodeClass;
  34 import org.graalvm.compiler.graph.spi.Canonicalizable;
  35 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  36 import org.graalvm.compiler.nodeinfo.NodeCycles;
  37 import org.graalvm.compiler.nodeinfo.NodeInfo;
  38 import org.graalvm.compiler.nodeinfo.NodeSize;
  39 import org.graalvm.compiler.nodes.ConstantNode;
  40 import org.graalvm.compiler.nodes.FixedWithNextNode;
  41 import org.graalvm.compiler.nodes.NamedLocationIdentity;
  42 import org.graalvm.compiler.nodes.NodeView;
  43 import org.graalvm.compiler.nodes.ValueNode;
  44 import org.graalvm.compiler.nodes.ValueNodeUtil;
  45 import org.graalvm.compiler.nodes.memory.MemoryAccess;
  46 import org.graalvm.compiler.nodes.memory.MemoryNode;
  47 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  48 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  49 import org.graalvm.compiler.nodes.spi.Virtualizable;
  50 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  51 import org.graalvm.compiler.nodes.util.GraphUtil;
  52 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  53 import jdk.internal.vm.compiler.word.LocationIdentity;
  54 
  55 import jdk.vm.ci.meta.ConstantReflectionProvider;
  56 import jdk.vm.ci.meta.JavaConstant;
  57 import jdk.vm.ci.meta.JavaKind;
  58 import jdk.vm.ci.meta.Value;
  59 
  60 // JaCoCo Exclude
  61 
  62 /**
  63  * Compares two arrays with the same length.
  64  */
  65 @NodeInfo(cycles = NodeCycles.CYCLES_UNKNOWN, size = NodeSize.SIZE_128)
  66 public final class ArrayEqualsNode extends FixedWithNextNode implements LIRLowerable, Canonicalizable, Virtualizable, MemoryAccess {
  67 
  68     public static final NodeClass<ArrayEqualsNode> TYPE = NodeClass.create(ArrayEqualsNode.class);
  69     /** {@link JavaKind} of the arrays to compare. */
  70     protected final JavaKind kind;
  71 
  72     /** One array to be tested for equality. */
  73     @Input ValueNode array1;
  74 
  75     /** The other array to be tested for equality. */
  76     @Input ValueNode array2;
  77 
  78     /** Length of both arrays. */
  79     @Input ValueNode length;
  80 
  81     @OptionalInput(Memory) MemoryNode lastLocationAccess;
  82 
  83     public ArrayEqualsNode(ValueNode array1, ValueNode array2, ValueNode length, @ConstantNodeParameter JavaKind kind) {
  84         super(TYPE, StampFactory.forKind(JavaKind.Boolean));
  85         this.kind = kind;
  86         this.array1 = array1;
  87         this.array2 = array2;
  88         this.length = length;
  89     }
  90 
  91     private static boolean isNaNFloat(JavaConstant constant) {
  92         JavaKind kind = constant.getJavaKind();
  93         return (kind == JavaKind.Float && Float.isNaN(constant.asFloat())) || (kind == JavaKind.Double && Double.isNaN(constant.asDouble()));
  94     }
  95 
  96     private static boolean arrayEquals(ConstantReflectionProvider constantReflection, JavaConstant a, JavaConstant b, int len) {
  97         for (int i = 0; i < len; i++) {
  98             JavaConstant aElem = constantReflection.readArrayElement(a, i);
  99             JavaConstant bElem = constantReflection.readArrayElement(b, i);
 100             if (!constantReflection.constantEquals(aElem, bElem) && !(isNaNFloat(aElem) && isNaNFloat(bElem))) {
 101                 return false;
 102             }
 103         }
 104         return true;
 105     }
 106 
 107     @Override
 108     public Node canonical(CanonicalizerTool tool) {
 109         if (tool.allUsagesAvailable() && hasNoUsages()) {
 110             return null;
 111         }
 112         ValueNode a1 = GraphUtil.unproxify(array1);
 113         ValueNode a2 = GraphUtil.unproxify(array2);
 114         if (a1 == a2) {
 115             return ConstantNode.forBoolean(true);
 116         }
 117         if (a1.isConstant() && a2.isConstant() && length.isConstant()) {
 118             ConstantNode c1 = (ConstantNode) a1;
 119             ConstantNode c2 = (ConstantNode) a2;
 120             if (c1.getStableDimension() >= 1 && c2.getStableDimension() >= 1) {
 121                 boolean ret = arrayEquals(tool.getConstantReflection(), c1.asJavaConstant(), c2.asJavaConstant(), length.asJavaConstant().asInt());
 122                 return ConstantNode.forBoolean(ret);
 123             }
 124         }
 125         return this;
 126     }
 127 
 128     @Override
 129     public void virtualize(VirtualizerTool tool) {
 130         ValueNode alias1 = tool.getAlias(array1);
 131         ValueNode alias2 = tool.getAlias(array2);
 132         if (alias1 == alias2) {
 133             // the same virtual objects will always have the same contents
 134             tool.replaceWithValue(ConstantNode.forBoolean(true, graph()));
 135         } else if (alias1 instanceof VirtualObjectNode && alias2 instanceof VirtualObjectNode) {
 136             VirtualObjectNode virtual1 = (VirtualObjectNode) alias1;
 137             VirtualObjectNode virtual2 = (VirtualObjectNode) alias2;
 138 
 139             if (virtual1.entryCount() == virtual2.entryCount()) {
 140                 int entryCount = virtual1.entryCount();
 141                 boolean allEqual = true;
 142                 for (int i = 0; i < entryCount; i++) {
 143                     ValueNode entry1 = tool.getEntry(virtual1, i);
 144                     ValueNode entry2 = tool.getEntry(virtual2, i);
 145                     if (entry1 != entry2) {
 146                         if (entry1 instanceof ConstantNode && entry2 instanceof ConstantNode) {
 147                             // Float NaN constants are different constant nodes but treated as
 148                             // equal in Arrays.equals([F[F) or Arrays.equals([D[D).
 149                             if (entry1.getStackKind() == JavaKind.Float && entry2.getStackKind() == JavaKind.Float) {
 150                                 float value1 = ((JavaConstant) entry1.asConstant()).asFloat();
 151                                 float value2 = ((JavaConstant) entry2.asConstant()).asFloat();
 152                                 if (Float.floatToIntBits(value1) != Float.floatToIntBits(value2)) {
 153                                     allEqual = false;
 154                                 }
 155                             } else if (entry1.getStackKind() == JavaKind.Double && entry2.getStackKind() == JavaKind.Double) {
 156                                 double value1 = ((JavaConstant) entry1.asConstant()).asDouble();
 157                                 double value2 = ((JavaConstant) entry2.asConstant()).asDouble();
 158                                 if (Double.doubleToLongBits(value1) != Double.doubleToLongBits(value2)) {
 159                                     allEqual = false;
 160                                 }
 161                             } else {
 162                                 allEqual = false;
 163                             }
 164                         } else {
 165                             // the contents might be different
 166                             allEqual = false;
 167                         }
 168                     }
 169                     if (entry1.stamp(NodeView.DEFAULT).alwaysDistinct(entry2.stamp(NodeView.DEFAULT))) {
 170                         // the contents are different
 171                         tool.replaceWithValue(ConstantNode.forBoolean(false, graph()));
 172                         return;
 173                     }
 174                 }
 175                 if (allEqual) {
 176                     tool.replaceWithValue(ConstantNode.forBoolean(true, graph()));
 177                 }
 178             }
 179         }
 180     }
 181 
 182     @NodeIntrinsic
 183     public static native boolean equals(Object array1, Object array2, int length, @ConstantNodeParameter JavaKind kind);
 184 
 185     public static boolean equals(boolean[] array1, boolean[] array2, int length) {
 186         return equals(array1, array2, length, JavaKind.Boolean);
 187     }
 188 
 189     public static boolean equals(byte[] array1, byte[] array2, int length) {
 190         return equals(array1, array2, length, JavaKind.Byte);
 191     }
 192 
 193     public static boolean equals(char[] array1, char[] array2, int length) {
 194         return equals(array1, array2, length, JavaKind.Char);
 195     }
 196 
 197     public static boolean equals(short[] array1, short[] array2, int length) {
 198         return equals(array1, array2, length, JavaKind.Short);
 199     }
 200 
 201     public static boolean equals(int[] array1, int[] array2, int length) {
 202         return equals(array1, array2, length, JavaKind.Int);
 203     }
 204 
 205     public static boolean equals(long[] array1, long[] array2, int length) {
 206         return equals(array1, array2, length, JavaKind.Long);
 207     }
 208 
 209     public static boolean equals(float[] array1, float[] array2, int length) {
 210         return equals(array1, array2, length, JavaKind.Float);
 211     }
 212 
 213     public static boolean equals(double[] array1, double[] array2, int length) {
 214         return equals(array1, array2, length, JavaKind.Double);
 215     }
 216 
 217     public ValueNode getLength() {
 218         return length;
 219     }
 220 
 221     public JavaKind getKind() {
 222         return kind;
 223     }
 224 
 225     @Override
 226     public void generate(NodeLIRBuilderTool gen) {
 227         if (UseGraalStubs.getValue(graph().getOptions())) {
 228             ForeignCallLinkage linkage = gen.lookupGraalStub(this);
 229             if (linkage != null) {
 230                 Value result = gen.getLIRGeneratorTool().emitForeignCall(linkage, null, gen.operand(array1), gen.operand(array2), gen.operand(length));
 231                 gen.setResult(this, result);
 232                 return;
 233             }
 234         }
 235         Value result = gen.getLIRGeneratorTool().emitArrayEquals(kind, gen.operand(array1), gen.operand(array2), gen.operand(length), false);
 236         gen.setResult(this, result);
 237     }
 238 
 239     @Override
 240     public LocationIdentity getLocationIdentity() {
 241         return NamedLocationIdentity.getArrayLocation(kind);
 242     }
 243 
 244     @Override
 245     public MemoryNode getLastLocationAccess() {
 246         return lastLocationAccess;
 247     }
 248 
 249     @Override
 250     public void setLastLocationAccess(MemoryNode lla) {
 251         updateUsages(ValueNodeUtil.asNode(lastLocationAccess), ValueNodeUtil.asNode(lla));
 252         lastLocationAccess = lla;
 253     }
 254 }