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.List;
  26 
  27 import org.graalvm.compiler.core.common.CompilationIdentifier;
  28 import org.graalvm.compiler.graph.Node;
  29 import org.graalvm.compiler.nodes.ConstantNode;
  30 import org.graalvm.compiler.nodes.Invoke;
  31 import org.graalvm.compiler.nodes.InvokeNode;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 import org.graalvm.compiler.nodes.ValueNode;
  35 import org.graalvm.compiler.nodes.debug.OpaqueNode;
  36 import org.graalvm.compiler.nodes.extended.LoadHubNode;
  37 import org.graalvm.compiler.nodes.extended.LoadMethodNode;
  38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  39 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
  40 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver;
  41 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
  42 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration;
  43 import org.graalvm.compiler.options.OptionValues;
  44 import org.junit.Assert;
  45 import org.junit.BeforeClass;
  46 import org.junit.Test;
  47 
  48 import jdk.vm.ci.meta.JavaKind;
  49 import jdk.vm.ci.meta.ResolvedJavaMethod;
  50 
  51 /**
  52  * Tests use of an intrinsic for virtual methods where the call site is indirect. A prime example is
  53  * an intrinsic for {@link Object#hashCode()}. The intrinsic can only be used if the call site would
  54  * actually dispatch to {@link Object#hashCode()} and not a method that overrides it.
  55  */
  56 public class GuardedIntrinsicTest extends GraalCompilerTest {
  57 
  58     static class Super {
  59         int getAge() {
  60             return 11;
  61         }
  62     }
  63 
  64     public static class Person extends Super {
  65         int age;
  66 
  67         public Person(int age) {
  68             this.age = age;
  69         }
  70 
  71         @Override
  72         public int getAge() {
  73             return age;
  74         }
  75     }
  76 
  77     @BytecodeParserForceInline
  78     public static final Super createSuper() {
  79         return new Super();
  80     }
  81 
  82     @BytecodeParserNeverInline
  83     public static final Super createPerson() {
  84         return new Person(42);
  85     }
  86 
  87     public static int getSuperAge(Super s) {
  88         return s.getAge();
  89     }
  90 
  91     public static int getPersonAge(Person p) {
  92         return p.getAge();
  93     }
  94 
  95     public static int makeSuperAge() {
  96         return createSuper().getAge();
  97     }
  98 
  99     public static int makePersonAge() {
 100         return createPerson().getAge();
 101     }
 102 
 103     @BeforeClass
 104     public static void init() {
 105         // Ensure classes are initialized
 106         new Person(0).toString();
 107     }
 108 
 109     private StructuredGraph graph;
 110     private StructuredGraph parsedForCompile;
 111 
 112     @Override
 113     protected StructuredGraph parseForCompile(ResolvedJavaMethod method, CompilationIdentifier compilationId, OptionValues options) {
 114         graph = super.parseForCompile(method, compilationId, options);
 115         parsedForCompile = (StructuredGraph) graph.copy(graph.getDebug());
 116         return graph;
 117     }
 118 
 119     @Override
 120     protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
 121         Registration r = new Registration(invocationPlugins, Super.class);
 122 
 123         r.register1("getAge", Receiver.class, new InvocationPlugin() {
 124             @Override
 125             public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
 126                 ConstantNode res = b.add(ConstantNode.forInt(new Super().getAge()));
 127                 b.add(new OpaqueNode(res));
 128                 b.push(JavaKind.Int, res);
 129                 return true;
 130             }
 131         });
 132         super.registerInvocationPlugins(invocationPlugins);
 133     }
 134 
 135     public static int referenceMakeSuperAge() {
 136         return 11;
 137     }
 138 
 139     public static int referenceMakePersonAge() {
 140         return 42;
 141     }
 142 
 143     @Test
 144     public void test01() {
 145         Super inheritsHC = new Super();
 146         Person overridesHC = new Person(0);
 147 
 148         // Ensure the profile for getSuperAge includes both receiver types
 149         getSuperAge(inheritsHC);
 150         getSuperAge(overridesHC);
 151 
 152         test("getSuperAge", inheritsHC);
 153         test("getSuperAge", overridesHC);
 154 
 155         // Check that the virtual dispatch test exists after bytecode parsing
 156         Assert.assertEquals(1, parsedForCompile.getNodes().filter(LoadMethodNode.class).count());
 157         Assert.assertEquals(1, parsedForCompile.getNodes().filter(LoadHubNode.class).count());
 158 
 159         // Check for the marker node indicating the intrinsic was applied
 160         Assert.assertEquals(1, parsedForCompile.getNodes().filter(OpaqueNode.class).count());
 161 
 162         // Final graph should have a single invoke
 163         List<Node> invokes = graph.getNodes().filter(n -> n instanceof Invoke).snapshot();
 164         Assert.assertEquals(invokes.toString(), 1, invokes.size());
 165     }
 166 
 167     @Test
 168     public void test02() {
 169         test("getPersonAge", new Person(0));
 170 
 171         // Check that the virtual dispatch test does not exist after bytecode parsing
 172         Assert.assertEquals(0, parsedForCompile.getNodes().filter(LoadMethodNode.class).count());
 173         Assert.assertEquals(0, parsedForCompile.getNodes().filter(LoadHubNode.class).count());
 174 
 175         Assert.assertEquals(0, parsedForCompile.getNodes().filter(InvokeNode.class).count());
 176     }
 177 
 178     @Test
 179     public void test03() {
 180         test("makeSuperAge");
 181 
 182         // Check that the virtual dispatch test does not exist after bytecode parsing
 183         Assert.assertEquals(0, parsedForCompile.getNodes().filter(LoadMethodNode.class).count());
 184         Assert.assertEquals(0, parsedForCompile.getNodes().filter(LoadHubNode.class).count());
 185 
 186         StructuredGraph referenceGraph = parseEager("referenceMakeSuperAge", AllowAssumptions.NO);
 187         assertEquals(referenceGraph, graph, true, true);
 188     }
 189 
 190     @Test
 191     public void test04() {
 192         test("makePersonAge");
 193 
 194         // Check that the virtual dispatch test exists after bytecode parsing
 195         Assert.assertEquals(1, parsedForCompile.getNodes().filter(LoadMethodNode.class).count());
 196         Assert.assertEquals(1, parsedForCompile.getNodes().filter(LoadHubNode.class).count());
 197 
 198         StructuredGraph referenceGraph = parseEager("referenceMakePersonAge", AllowAssumptions.NO);
 199         assertEquals(referenceGraph, graph, true, true);
 200     }
 201 
 202     static final class ReadCacheEntry {
 203 
 204         public final Object identity;
 205         public final ValueNode object;
 206 
 207         ReadCacheEntry(Object identity, ValueNode object) {
 208             this.identity = identity;
 209             this.object = object;
 210         }
 211 
 212         @Override
 213         public int hashCode() {
 214             int result = ((identity == null) ? 0 : identity.hashCode());
 215             return result + System.identityHashCode(object);
 216         }
 217     }
 218 
 219     public static int getHashCode(ReadCacheEntry obj) {
 220         return obj.hashCode();
 221     }
 222 
 223     @Test
 224     public void test05() {
 225         ReadCacheEntry val1 = new ReadCacheEntry("identity", ConstantNode.forBoolean(false));
 226         ReadCacheEntry val2 = new ReadCacheEntry(Integer.valueOf(34), ConstantNode.forInt(42));
 227         for (int i = 0; i < 10000; i++) {
 228             getHashCode(val2);
 229         }
 230         test("getHashCode", val1);
 231     }
 232 }