1 /* 2 * Copyright (c) 2014, 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.test; 26 27 import static org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 28 29 import java.util.Arrays; 30 31 import org.graalvm.compiler.nodes.ReturnNode; 32 import org.graalvm.compiler.nodes.StructuredGraph; 33 import org.graalvm.compiler.phases.OptimisticOptimizations; 34 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 35 import org.graalvm.compiler.phases.tiers.HighTierContext; 36 import org.graalvm.compiler.replacements.nodes.ArrayEqualsNode; 37 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase; 38 import org.junit.Assert; 39 import org.junit.Test; 40 41 public class ArraysSubstitutionsTest extends ArraysSubstitutionsTestBase { 42 43 private static final int N = 10; 44 45 private void testEquals(String methodName, Class<?>[] parameterTypes, ArrayBuilder builder) { 46 Object[] args1 = new Object[N]; 47 Object[] args2 = new Object[N]; 48 int n = 0; 49 // equal arrays 50 for (int i = 0; i < N / 2; i++, n++) { 51 args1[n] = builder.newArray(i, 0, 1); 52 args2[n] = builder.newArray(i, 0, 1); 53 } 54 // non-equal arrays 55 for (int i = 0; i < N / 2; i++, n++) { 56 args1[n] = builder.newArray(i, 0, 1); 57 args2[n] = builder.newArray(i, 1, 1); 58 } 59 testSubstitution(methodName, ArrayEqualsNode.class, Arrays.class, "equals", parameterTypes, false, false, args1, args2); 60 } 61 62 @Test 63 public void testEqualsBoolean() { 64 testEquals("arraysEqualsBoolean", new Class<?>[]{boolean[].class, boolean[].class}, ArraysSubstitutionsTestBase::booleanArray); 65 } 66 67 @Test 68 public void testEqualsByte() { 69 testEquals("arraysEqualsByte", new Class<?>[]{byte[].class, byte[].class}, ArraysSubstitutionsTestBase::byteArray); 70 } 71 72 @Test 73 public void testEqualsChar() { 74 testEquals("arraysEqualsChar", new Class<?>[]{char[].class, char[].class}, ArraysSubstitutionsTestBase::charArray); 75 } 76 77 @Test 78 public void testEqualsShort() { 79 testEquals("arraysEqualsShort", new Class<?>[]{short[].class, short[].class}, ArraysSubstitutionsTestBase::shortArray); 80 } 81 82 @Test 83 public void testEqualsInt() { 84 testEquals("arraysEqualsInt", new Class<?>[]{int[].class, int[].class}, ArraysSubstitutionsTestBase::intArray); 85 } 86 87 @Test 88 public void testEqualsLong() { 89 testEquals("arraysEqualsLong", new Class<?>[]{long[].class, long[].class}, ArraysSubstitutionsTestBase::longArray); 90 } 91 92 @Test 93 public void testEqualsNodeGVN() { 94 test("testEqualsNodeGVNSnippet", true); 95 } 96 97 public static int[] intArrayCompare = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; 98 public static int[] intArray; 99 100 public static boolean testEqualsNodeGVNSnippet(boolean b) { 101 int[] newIntArray = new int[]{0, 2, 3, 4, 5, 6, 7, 8, 9}; 102 intArray = newIntArray; 103 104 if (b) { 105 newIntArray[0] = 1; 106 return Arrays.equals(newIntArray, intArrayCompare); 107 } else { 108 newIntArray[0] = 1; 109 return Arrays.equals(newIntArray, intArrayCompare); 110 } 111 } 112 113 @Test 114 public void testConstants() { 115 testGraph("testConstantsSnippet"); 116 } 117 118 public static final int[] constantArray1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; 119 public static final int[] constantArray2 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; 120 121 public static boolean testConstantsSnippet() { 122 constantArray2[0] = 10; 123 try { 124 return Arrays.equals(constantArray1, constantArray2); 125 } finally { 126 constantArray2[0] = 1; 127 } 128 } 129 130 @Test 131 public void testCanonicalLength() { 132 StructuredGraph graph = parseEager("testCanonicalLengthSnippet", AllowAssumptions.NO); 133 HighTierContext context = new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL); 134 createInliningPhase().apply(graph, context); 135 new CanonicalizerPhase().apply(graph, getProviders()); 136 137 Assert.assertTrue(graph.getNodes(ReturnNode.TYPE).first().result().asJavaConstant().asLong() == 0); 138 } 139 140 public static final int[] constantArray3 = new int[]{1, 2, 3}; 141 142 public static boolean testCanonicalLengthSnippet() { 143 return Arrays.equals(constantArray1, constantArray3); 144 } 145 146 @Test 147 public void testCanonicalEqual() { 148 StructuredGraph graph = parseEager("testCanonicalEqualSnippet", AllowAssumptions.NO); 149 HighTierContext context = new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL); 150 createInliningPhase().apply(graph, context); 151 new CanonicalizerPhase().apply(graph, getProviders()); 152 153 Assert.assertTrue(graph.getNodes(ReturnNode.TYPE).first().result().asJavaConstant().asLong() == 1); 154 } 155 156 public static boolean testCanonicalEqualSnippet() { 157 return Arrays.equals(constantArray1, constantArray1); 158 } 159 160 @Test 161 public void testVirtualEqual() { 162 StructuredGraph graph = parseEager("testVirtualEqualSnippet", AllowAssumptions.NO); 163 HighTierContext context = new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL); 164 createInliningPhase().apply(graph, context); 165 new CanonicalizerPhase().apply(graph, getProviders()); 166 new PartialEscapePhase(false, new CanonicalizerPhase(), graph.getOptions()).apply(graph, context); 167 new CanonicalizerPhase().apply(graph, getProviders()); 168 169 Assert.assertTrue(graph.getNodes(ReturnNode.TYPE).first().result().asJavaConstant().asLong() == 1); 170 } 171 172 public static boolean testVirtualEqualSnippet() { 173 int[] array1 = new int[]{1, 2, 3, 4}; 174 int[] array2 = new int[]{1, 2, 3, 4}; 175 return Arrays.equals(array1, array2); 176 } 177 178 @Test 179 public void testVirtualNotEqual() { 180 StructuredGraph graph = parseEager("testVirtualNotEqualSnippet", AllowAssumptions.NO); 181 HighTierContext context = getDefaultHighTierContext(); 182 createInliningPhase().apply(graph, context); 183 new CanonicalizerPhase().apply(graph, getProviders()); 184 new PartialEscapePhase(false, new CanonicalizerPhase(), graph.getOptions()).apply(graph, context); 185 new CanonicalizerPhase().apply(graph, getProviders()); 186 187 Assert.assertTrue(graph.getNodes(ReturnNode.TYPE).first().result().asJavaConstant().asLong() == 0); 188 } 189 190 public static boolean testVirtualNotEqualSnippet(int x) { 191 int[] array1 = new int[]{1, 2, 100, x}; 192 int[] array2 = new int[]{1, 2, 3, 4}; 193 return Arrays.equals(array1, array2); 194 } 195 }