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