1 /*
   2  * Copyright (c) 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.core.test;
  26 
  27 import java.util.HashMap;
  28 import java.util.List;
  29 
  30 import org.graalvm.compiler.core.phases.HighTier;
  31 import org.graalvm.compiler.core.phases.MidTier;
  32 import org.graalvm.compiler.nodes.InvokeNode;
  33 import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.extended.LoadHubNode;
  36 import org.graalvm.compiler.nodes.extended.LoadMethodNode;
  37 import org.graalvm.compiler.options.OptionValues;
  38 import org.graalvm.compiler.phases.OptimisticOptimizations;
  39 import org.graalvm.compiler.phases.tiers.MidTierContext;
  40 import org.graalvm.compiler.test.SubprocessUtil;
  41 import org.junit.Assert;
  42 import org.junit.Assume;
  43 import org.junit.Test;
  44 
  45 public class HashCodeTest extends GraalCompilerTest {
  46 
  47     static class OverrideHashCode {
  48         @Override
  49         public int hashCode() {
  50             return 42;
  51         }
  52     }
  53 
  54     static final class DontOverrideHashCode {
  55     }
  56 
  57     public static final Object NonOverridingConstant = new Object();
  58     public static final Object OverridingConstant = new OverrideHashCode();
  59 
  60     private static void initialize(Class<?> c) {
  61         try {
  62             Class.forName(c.getName(), true, c.getClassLoader());
  63         } catch (ClassNotFoundException e) {
  64             throw new AssertionError(e);
  65         }
  66     }
  67 
  68     public static final int hashCodeSnippet01(Object o) {
  69         return o.hashCode();
  70     }
  71 
  72     public static final int systemIdentityHashCodeSnippet01(Object o) {
  73         return System.identityHashCode(o);
  74     }
  75 
  76     public static final int hashCodeFoldSnippet01() {
  77         return NonOverridingConstant.hashCode();
  78     }
  79 
  80     public static final int identityHashCodeFoldSnippet01() {
  81         return System.identityHashCode(NonOverridingConstant);
  82     }
  83 
  84     public static final int hashCodeNoFoldOverridingSnippet01(Object o) {
  85         return o.hashCode();
  86     }
  87 
  88     public static final int identityHashCodeFoldOverridingSnippet01() {
  89         return System.identityHashCode(OverridingConstant);
  90     }
  91 
  92     public static final int dontOverrideHashCodeFinalClass(DontOverrideHashCode o) {
  93         return o.hashCode();
  94     }
  95 
  96     @Test
  97     public void test01() {
  98         test("hashCodeSnippet01", new Object());
  99     }
 100 
 101     @Test
 102     public void test02() {
 103         test("systemIdentityHashCodeSnippet01", new Object());
 104     }
 105 
 106     @Test
 107     public void test03() {
 108         StructuredGraph g = buildGraphAfterMidTier("hashCodeFoldSnippet01");
 109         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 110     }
 111 
 112     @Test
 113     public void test04() {
 114         StructuredGraph g = buildGraphAfterMidTier("identityHashCodeFoldSnippet01");
 115         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 116     }
 117 
 118     @Test
 119     public void test05() {
 120         checkForGuardedIntrinsicPattern("hashCodeNoFoldOverridingSnippet01");
 121 
 122         Object nullObject = null;
 123         test("hashCodeNoFoldOverridingSnippet01", nullObject);
 124         test("hashCodeNoFoldOverridingSnippet01", new Object());
 125         test("hashCodeNoFoldOverridingSnippet01", new DontOverrideHashCode());
 126     }
 127 
 128     @Test
 129     public void test06() {
 130         StructuredGraph g = buildGraphAfterMidTier("identityHashCodeFoldOverridingSnippet01");
 131         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 132     }
 133 
 134     @Test
 135     public void test07() {
 136         initialize(DontOverrideHashCode.class);
 137         StructuredGraph g = buildGraphAfterMidTier("dontOverrideHashCodeFinalClass");
 138         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 139     }
 140 
 141     public static final int hashCodeInterface(Appendable o) {
 142         return o.hashCode();
 143     }
 144 
 145     @Test
 146     public void test08() {
 147         // This test requires profiling information which does not work reliable across platforms
 148         // when running with -Xcomp
 149         List<String> commandLine = SubprocessUtil.getVMCommandLine();
 150         Assume.assumeTrue(commandLine != null);
 151         Assume.assumeFalse(commandLine.contains("-Xcomp"));
 152         initialize(Appendable.class);
 153         checkForGuardedIntrinsicPattern("hashCodeInterface");
 154 
 155         // Ensure the profile for the dispatch in hashCodeSnippet01
 156         // has a receiver type that does not select Object.hashCode intrinsic
 157         hashCodeSnippet01(new HashMap<>());
 158         checkForGuardedIntrinsicPattern("hashCodeSnippet01");
 159     }
 160 
 161     private void checkForGuardedIntrinsicPattern(String name) {
 162         StructuredGraph g = parseForCompile(getResolvedJavaMethod(name));
 163         int invokeNodeCount = g.getNodes().filter(InvokeNode.class).count();
 164         int invokeWithExceptionNodeCount = g.getNodes().filter(InvokeWithExceptionNode.class).count();
 165         Assert.assertEquals(1, invokeNodeCount + invokeWithExceptionNodeCount);
 166         Assert.assertEquals(1, g.getNodes().filter(LoadHubNode.class).count());
 167         Assert.assertEquals(1, g.getNodes().filter(LoadMethodNode.class).count());
 168     }
 169 
 170     @SuppressWarnings("try")
 171     private StructuredGraph buildGraphAfterMidTier(String name) {
 172         StructuredGraph g = parseForCompile(getResolvedJavaMethod(name));
 173         OptionValues options = getInitialOptions();
 174         new HighTier(options).apply(g, getDefaultHighTierContext());
 175         new MidTier(options).apply(g, new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, g.getProfilingInfo()));
 176         return g;
 177     }
 178 
 179 }