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 public ValueNode getArray1() { 90 return array1; 91 } 92 93 public ValueNode getArray2() { 94 return array2; 95 } 96 97 public ValueNode getLength() { 98 return length; 99 } 100 101 private static boolean isNaNFloat(JavaConstant constant) { 102 JavaKind kind = constant.getJavaKind(); 103 return (kind == JavaKind.Float && Float.isNaN(constant.asFloat())) || (kind == JavaKind.Double && Double.isNaN(constant.asDouble())); 104 } 105 106 private static boolean arrayEquals(ConstantReflectionProvider constantReflection, JavaConstant a, JavaConstant b, int len) { 107 for (int i = 0; i < len; i++) { 108 JavaConstant aElem = constantReflection.readArrayElement(a, i); 109 JavaConstant bElem = constantReflection.readArrayElement(b, i); 110 if (!constantReflection.constantEquals(aElem, bElem) && !(isNaNFloat(aElem) && isNaNFloat(bElem))) { 111 return false; 112 } 113 } 114 return true; 115 } 116 117 @Override 118 public Node canonical(CanonicalizerTool tool) { 119 if (tool.allUsagesAvailable() && hasNoUsages()) { 120 return null; 121 } 122 ValueNode a1 = GraphUtil.unproxify(array1); 123 ValueNode a2 = GraphUtil.unproxify(array2); 124 if (a1 == a2) { 125 return ConstantNode.forBoolean(true); 126 } 127 if (a1.isConstant() && a2.isConstant() && length.isConstant()) { 128 ConstantNode c1 = (ConstantNode) a1; 129 ConstantNode c2 = (ConstantNode) a2; 130 if (c1.getStableDimension() >= 1 && c2.getStableDimension() >= 1) { 131 boolean ret = arrayEquals(tool.getConstantReflection(), c1.asJavaConstant(), c2.asJavaConstant(), length.asJavaConstant().asInt()); 132 return ConstantNode.forBoolean(ret); 133 } 134 } 135 return this; 136 } 137 138 @Override 139 public void virtualize(VirtualizerTool tool) { 140 ValueNode alias1 = tool.getAlias(array1); 141 ValueNode alias2 = tool.getAlias(array2); 142 if (alias1 == alias2) { 143 // the same virtual objects will always have the same contents 144 tool.replaceWithValue(ConstantNode.forBoolean(true, graph())); 145 } else if (alias1 instanceof VirtualObjectNode && alias2 instanceof VirtualObjectNode) { 146 VirtualObjectNode virtual1 = (VirtualObjectNode) alias1; 147 VirtualObjectNode virtual2 = (VirtualObjectNode) alias2; 148 149 if (virtual1.entryCount() == virtual2.entryCount()) { 150 int entryCount = virtual1.entryCount(); 151 boolean allEqual = true; 152 for (int i = 0; i < entryCount; i++) { 153 ValueNode entry1 = tool.getEntry(virtual1, i); 154 ValueNode entry2 = tool.getEntry(virtual2, i); 155 if (entry1 != entry2) { 156 if (entry1 instanceof ConstantNode && entry2 instanceof ConstantNode) { 157 // Float NaN constants are different constant nodes but treated as 158 // equal in Arrays.equals([F[F) or Arrays.equals([D[D). 159 if (entry1.getStackKind() == JavaKind.Float && entry2.getStackKind() == JavaKind.Float) { 160 float value1 = ((JavaConstant) ((ConstantNode) entry1).asConstant()).asFloat(); 161 float value2 = ((JavaConstant) ((ConstantNode) entry2).asConstant()).asFloat(); 162 if (Float.floatToIntBits(value1) != Float.floatToIntBits(value2)) { 163 allEqual = false; 164 } 165 } else if (entry1.getStackKind() == JavaKind.Double && entry2.getStackKind() == JavaKind.Double) { 166 double value1 = ((JavaConstant) ((ConstantNode) entry1).asConstant()).asDouble(); 167 double value2 = ((JavaConstant) ((ConstantNode) entry2).asConstant()).asDouble(); 168 if (Double.doubleToLongBits(value1) != Double.doubleToLongBits(value2)) { 169 allEqual = false; 170 } 171 } else { 172 allEqual = false; 173 } 174 } else { 175 // the contents might be different 176 allEqual = false; 177 } 178 } 179 if (entry1.stamp(NodeView.DEFAULT).alwaysDistinct(entry2.stamp(NodeView.DEFAULT))) { 180 // the contents are different 181 tool.replaceWithValue(ConstantNode.forBoolean(false, graph())); 182 return; 183 } 184 } 185 if (allEqual) { 186 tool.replaceWithValue(ConstantNode.forBoolean(true, graph())); 187 } 188 } 189 } 190 } 191 192 @NodeIntrinsic 193 static native boolean equals(Object array1, Object array2, int length, @ConstantNodeParameter JavaKind kind); 194 195 public static boolean equals(boolean[] array1, boolean[] array2, int length) { 196 return equals(array1, array2, length, JavaKind.Boolean); 197 } 198 199 public static boolean equals(byte[] array1, byte[] array2, int length) { 200 return equals(array1, array2, length, JavaKind.Byte); 201 } 202 203 public static boolean equals(char[] array1, char[] array2, int length) { 204 return equals(array1, array2, length, JavaKind.Char); 205 } 206 207 public static boolean equals(short[] array1, short[] array2, int length) { 208 return equals(array1, array2, length, JavaKind.Short); 209 } 210 211 public static boolean equals(int[] array1, int[] array2, int length) { 212 return equals(array1, array2, length, JavaKind.Int); 213 } 214 215 public static boolean equals(long[] array1, long[] array2, int length) { 216 return equals(array1, array2, length, JavaKind.Long); 217 } 218 219 public static boolean equals(float[] array1, float[] array2, int length) { 220 return equals(array1, array2, length, JavaKind.Float); 221 } 222 223 public static boolean equals(double[] array1, double[] array2, int length) { 224 return equals(array1, array2, length, JavaKind.Double); 225 } 226 227 @Override 228 public void generate(NodeLIRBuilderTool gen) { 229 Value result = gen.getLIRGeneratorTool().emitArrayEquals(kind, gen.operand(array1), gen.operand(array2), gen.operand(length)); 230 gen.setResult(this, result); 231 } 232 233 @Override 234 public LocationIdentity getLocationIdentity() { 235 return NamedLocationIdentity.getArrayLocation(kind); 236 } 237 238 @Override 239 public MemoryNode getLastLocationAccess() { 240 return lastLocationAccess; 241 } 242 243 @Override 244 public void setLastLocationAccess(MemoryNode lla) { 245 updateUsages(ValueNodeUtil.asNode(lastLocationAccess), ValueNodeUtil.asNode(lla)); 246 lastLocationAccess = lla; 247 } 248 }