rev 52889 : 8214023: Update Graal

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