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