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 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         }
  47     }
  48 
  49     static final class DontOverrideHashCode {
  50     }
  51 
  52     public static final Object NonOverridingConstant = new Object();
  53     public static final Object OverridingConstant = new OverrideHashCode();
  54 
  55     private static void initialize(Class<?> c) {
  56         try {
  57             Class.forName(c.getName(), true, c.getClassLoader());
  58         } catch (ClassNotFoundException e) {
  59             throw new AssertionError(e);
  60         }
  61     }
  62 
  63     public static final int hashCodeSnippet01(Object o) {
  64         return o.hashCode();
  65     }
  66 
  67     public static final int systemIdentityHashCodeSnippet01(Object o) {
  68         return System.identityHashCode(o);
  69     }
  70 
  71     public static final int hashCodeFoldSnippet01() {
  72         return NonOverridingConstant.hashCode();
  73     }
  74 
  75     public static final int identityHashCodeFoldSnippet01() {
  76         return System.identityHashCode(NonOverridingConstant);
  77     }
  78 
  79     public static final int hashCodeNoFoldOverridingSnippet01(Object o) {
  80         return o.hashCode();
  81     }
  82 
  83     public static final int identityHashCodeFoldOverridingSnippet01() {
  84         return System.identityHashCode(OverridingConstant);
  85     }
  86 
  87     public static final int dontOverrideHashCodeFinalClass(DontOverrideHashCode o) {
  88         return o.hashCode();
  89     }
  90 
  91     @Test
  92     public void test01() {
  93         test("hashCodeSnippet01", new Object());
  94     }
  95 
  96     @Test
  97     public void test02() {
  98         test("systemIdentityHashCodeSnippet01", new Object());
  99     }
 100 
 101     @Test
 102     public void test03() {
 103         StructuredGraph g = buildGraphAfterMidTier("hashCodeFoldSnippet01");
 104         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 105     }
 106 
 107     @Test
 108     public void test04() {
 109         StructuredGraph g = buildGraphAfterMidTier("identityHashCodeFoldSnippet01");
 110         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 111     }
 112 
 113     @Test
 114     public void test05() {
 115         checkForGuardedIntrinsicPattern("hashCodeNoFoldOverridingSnippet01");
 116 
 117         Object nullObject = null;
 118         test("hashCodeNoFoldOverridingSnippet01", nullObject);
 119         test("hashCodeNoFoldOverridingSnippet01", new Object());
 120         test("hashCodeNoFoldOverridingSnippet01", new DontOverrideHashCode());
 121     }
 122 
 123     @Test
 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     }
 168 
 169 }