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 org.junit.Assert;
  26 import org.junit.Test;
  27 
  28 import org.graalvm.compiler.core.phases.HighTier;
  29 import org.graalvm.compiler.core.phases.MidTier;
  30 import org.graalvm.compiler.nodes.InvokeNode;
  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.phases.OptimisticOptimizations;
  35 import org.graalvm.compiler.phases.tiers.MidTierContext;
  36 
  37 public class HashCodeTest extends GraalCompilerTest {
  38 
  39     static class OverrideHashCode {
  40         @Override
  41         public int hashCode() {
  42             return 42;
  43         }
  44     }
  45 
  46     static final class DontOverrideHashCode {
  47     }
  48 
  49     public static final Object NonOverridingConstant = new Object();
  50     public static final Object OverridingConstant = new OverrideHashCode();
  51 
  52     private static void initialize(Class<?> c) {
  53         try {
  54             Class.forName(c.getName(), true, c.getClassLoader());
  55         } catch (ClassNotFoundException e) {
  56             throw new AssertionError(e);
  57         }
  58     }
  59 
  60     public static final int hashCodeSnippet01(Object o) {
  61         return o.hashCode();
  62     }
  63 
  64     public static final int systemIdentityHashCodeSnippet01(Object o) {
  65         return System.identityHashCode(o);
  66     }
  67 
  68     public static final int hashCodeFoldSnippet01() {
  69         return NonOverridingConstant.hashCode();
  70     }
  71 
  72     public static final int identityHashCodeFoldSnippet01() {
  73         return System.identityHashCode(NonOverridingConstant);
  74     }
  75 
  76     public static final int hashCodeNoFoldOverridingSnippet01(Object o) {
  77         return o.hashCode();
  78     }
  79 
  80     public static final int identityHashCodeFoldOverridingSnippet01() {
  81         return System.identityHashCode(OverridingConstant);
  82     }
  83 
  84     public static final int dontOverrideHashCodeFinalClass(DontOverrideHashCode o) {
  85         return o.hashCode();
  86     }
  87 
  88     @Test
  89     public void test01() {
  90         test("hashCodeSnippet01", new Object());
  91     }
  92 
  93     @Test
  94     public void test02() {
  95         test("systemIdentityHashCodeSnippet01", new Object());
  96     }
  97 
  98     @Test
  99     public void test03() {
 100         StructuredGraph g = buildGraphAfterMidTier("hashCodeFoldSnippet01");
 101         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 102     }
 103 
 104     @Test
 105     public void test04() {
 106         StructuredGraph g = buildGraphAfterMidTier("identityHashCodeFoldSnippet01");
 107         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 108     }
 109 
 110     @Test
 111     public void test05() {
 112         checkForGuardedIntrinsicPattern("hashCodeNoFoldOverridingSnippet01");
 113 
 114         Object nullObject = null;
 115         test("hashCodeNoFoldOverridingSnippet01", nullObject);
 116         test("hashCodeNoFoldOverridingSnippet01", new Object());
 117         test("hashCodeNoFoldOverridingSnippet01", new DontOverrideHashCode());
 118     }
 119 
 120     @Test
 121     public void test06() {
 122         StructuredGraph g = buildGraphAfterMidTier("identityHashCodeFoldOverridingSnippet01");
 123         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 124     }
 125 
 126     @Test
 127     public void test07() {
 128         initialize(DontOverrideHashCode.class);
 129         StructuredGraph g = buildGraphAfterMidTier("dontOverrideHashCodeFinalClass");
 130         Assert.assertEquals(0, g.getNodes().filter(InvokeNode.class).count());
 131     }
 132 
 133     public static final int hashCodeInterface(Appendable o) {
 134         return o.hashCode();
 135     }
 136 
 137     @Test
 138     public void test08() {
 139         initialize(Appendable.class);
 140         checkForGuardedIntrinsicPattern("hashCodeInterface");
 141         checkForGuardedIntrinsicPattern("hashCodeSnippet01");
 142     }
 143 
 144     private void checkForGuardedIntrinsicPattern(String name) {
 145         StructuredGraph g = parseForCompile(getResolvedJavaMethod(name));
 146         Assert.assertEquals(1, g.getNodes().filter(InvokeNode.class).count());
 147         Assert.assertEquals(1, g.getNodes().filter(LoadHubNode.class).count());
 148         Assert.assertEquals(1, g.getNodes().filter(LoadMethodNode.class).count());
 149     }
 150 
 151     @SuppressWarnings("try")
 152     private StructuredGraph buildGraphAfterMidTier(String name) {
 153         StructuredGraph g = parseForCompile(getResolvedJavaMethod(name));
 154         new HighTier().apply(g, getDefaultHighTierContext());
 155         new MidTier().apply(g, new MidTierContext(getProviders(), getTargetProvider(), OptimisticOptimizations.ALL, g.getProfilingInfo()));
 156         return g;
 157     }
 158 
 159 }