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.core.test; 24 25 import org.graalvm.compiler.core.phases.HighTier; 26 import org.graalvm.compiler.core.phases.MidTier; 27 import org.graalvm.compiler.nodes.InvokeNode; 28 import org.graalvm.compiler.nodes.InvokeWithExceptionNode; 29 import org.graalvm.compiler.nodes.StructuredGraph; 30 import org.graalvm.compiler.nodes.extended.LoadHubNode; 31 import org.graalvm.compiler.nodes.extended.LoadMethodNode; 32 import org.graalvm.compiler.options.OptionValues; 33 import org.graalvm.compiler.phases.OptimisticOptimizations; 34 import org.graalvm.compiler.phases.tiers.MidTierContext; 35 import org.junit.Assert; 36 import org.junit.Test; 37 38 public class HashCodeTest extends GraalCompilerTest { 39 40 static class OverrideHashCode { 41 @Override 42 public int hashCode() { 43 return 42; 44 } 122 public void test06() { 123 StructuredGraph g = buildGraphAfterMidTier("identityHashCodeFoldOverridingSnippet01"); 124 Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count()); 125 } 126 127 @Test 128 public void test07() { 129 initialize(DontOverrideHashCode.class); 130 StructuredGraph g = buildGraphAfterMidTier("dontOverrideHashCodeFinalClass"); 131 Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count()); 132 } 133 134 public static final int hashCodeInterface(Appendable o) { 135 return o.hashCode(); 136 } 137 138 @Test 139 public void test08() { 140 initialize(Appendable.class); 141 checkForGuardedIntrinsicPattern("hashCodeInterface"); 142 checkForGuardedIntrinsicPattern("hashCodeSnippet01"); 143 } 144 145 private void checkForGuardedIntrinsicPattern(String name) { 146 StructuredGraph g = parseForCompile(getResolvedJavaMethod(name)); 147 int invokeNodeCount = g.getNodes().filter(InvokeNode.class).count(); 148 int invokeWithExceptionNodeCount = g.getNodes().filter(InvokeWithExceptionNode.class).count(); 149 Assert.assertEquals(1, invokeNodeCount + invokeWithExceptionNodeCount); 150 Assert.assertEquals(1, g.getNodes().filter(LoadHubNode.class).count()); 151 Assert.assertEquals(1, g.getNodes().filter(LoadMethodNode.class).count()); 152 } 153 154 @SuppressWarnings("try") 155 private StructuredGraph buildGraphAfterMidTier(String name) { 156 StructuredGraph g = parseForCompile(getResolvedJavaMethod(name)); 157 OptionValues options = getInitialOptions(); 158 new HighTier(options).apply(g, getDefaultHighTierContext()); 159 new MidTier(options).apply(g, new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, g.getProfilingInfo())); 160 return g; 161 } | 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.core.test; 24 25 import java.util.HashMap; 26 27 import org.graalvm.compiler.core.phases.HighTier; 28 import org.graalvm.compiler.core.phases.MidTier; 29 import org.graalvm.compiler.nodes.InvokeNode; 30 import org.graalvm.compiler.nodes.InvokeWithExceptionNode; 31 import org.graalvm.compiler.nodes.StructuredGraph; 32 import org.graalvm.compiler.nodes.extended.LoadHubNode; 33 import org.graalvm.compiler.nodes.extended.LoadMethodNode; 34 import org.graalvm.compiler.options.OptionValues; 35 import org.graalvm.compiler.phases.OptimisticOptimizations; 36 import org.graalvm.compiler.phases.tiers.MidTierContext; 37 import org.junit.Assert; 38 import org.junit.Test; 39 40 public class HashCodeTest extends GraalCompilerTest { 41 42 static class OverrideHashCode { 43 @Override 44 public int hashCode() { 45 return 42; 46 } 124 public void test06() { 125 StructuredGraph g = buildGraphAfterMidTier("identityHashCodeFoldOverridingSnippet01"); 126 Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count()); 127 } 128 129 @Test 130 public void test07() { 131 initialize(DontOverrideHashCode.class); 132 StructuredGraph g = buildGraphAfterMidTier("dontOverrideHashCodeFinalClass"); 133 Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count()); 134 } 135 136 public static final int hashCodeInterface(Appendable o) { 137 return o.hashCode(); 138 } 139 140 @Test 141 public void test08() { 142 initialize(Appendable.class); 143 checkForGuardedIntrinsicPattern("hashCodeInterface"); 144 145 // Ensure the profile for the dispatch in hashCodeSnippet01 146 // has a receiver type that does not select Object.hashCode intrinsic 147 hashCodeSnippet01(new HashMap<>()); 148 checkForGuardedIntrinsicPattern("hashCodeSnippet01"); 149 } 150 151 private void checkForGuardedIntrinsicPattern(String name) { 152 StructuredGraph g = parseForCompile(getResolvedJavaMethod(name)); 153 int invokeNodeCount = g.getNodes().filter(InvokeNode.class).count(); 154 int invokeWithExceptionNodeCount = g.getNodes().filter(InvokeWithExceptionNode.class).count(); 155 Assert.assertEquals(1, invokeNodeCount + invokeWithExceptionNodeCount); 156 Assert.assertEquals(1, g.getNodes().filter(LoadHubNode.class).count()); 157 Assert.assertEquals(1, g.getNodes().filter(LoadMethodNode.class).count()); 158 } 159 160 @SuppressWarnings("try") 161 private StructuredGraph buildGraphAfterMidTier(String name) { 162 StructuredGraph g = parseForCompile(getResolvedJavaMethod(name)); 163 OptionValues options = getInitialOptions(); 164 new HighTier(options).apply(g, getDefaultHighTierContext()); 165 new MidTier(options).apply(g, new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, g.getProfilingInfo())); 166 return g; 167 } |