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