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