1 /*
2  * Copyright (c) 2011, 2015, 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.deopt;
26 
27 import org.graalvm.compiler.api.directives.GraalDirectives;
28 import org.graalvm.compiler.core.test.GraalCompilerTest;
29 import org.graalvm.compiler.nodes.ConstantNode;
30 import org.graalvm.compiler.nodes.StructuredGraph;
31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
32 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
33 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
34 import org.graalvm.compiler.phases.tiers.PhaseContext;
35 import org.junit.Assert;
36 import org.junit.Test;
37 
38 import jdk.vm.ci.code.InstalledCode;
39 import jdk.vm.ci.code.InvalidInstalledCodeException;
40 import jdk.vm.ci.meta.JavaKind;
41 import jdk.vm.ci.meta.ResolvedJavaMethod;
42 
43 public class CompiledMethodTest extends GraalCompilerTest {
44 
testMethod(Object arg1, Object arg2, Object arg3)45     public static Object testMethod(Object arg1, Object arg2, Object arg3) {
46         String res = arg1 + " " + arg2 + " " + arg3;
47         return GraalDirectives.inCompiledCode() ? res : "interpreter";
48     }
49 
50     Object f1;
51 
testMethodVirtual(Object arg1, Object arg2, Object arg3)52     public Object testMethodVirtual(Object arg1, Object arg2, Object arg3) {
53         return f1 + " " + arg1 + " " + arg2 + " " + arg3;
54     }
55 
56     /**
57      * Usages of the constant {@code " "} are replaced with the constant {@code "-"} and it is
58      * verified that executing the compiled code produces a result that the preserves the node
59      * replacement unless deoptimization occurs (e.g., due to -Xcomp causing profiles to be
60      * missing).
61      */
62     @Test
test1()63     public void test1() {
64         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
65         final StructuredGraph graph = parseEager(javaMethod, AllowAssumptions.NO);
66         new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
67         new DeadCodeEliminationPhase().apply(graph);
68 
69         for (ConstantNode node : ConstantNode.getConstantNodes(graph)) {
70             if (node.getStackKind() == JavaKind.Object && " ".equals(getSnippetReflection().asObject(String.class, node.asJavaConstant()))) {
71                 node.replace(graph, ConstantNode.forConstant(getSnippetReflection().forObject("-"), getMetaAccess(), graph));
72             }
73         }
74 
75         InstalledCode compiledMethod = getCode(javaMethod, graph);
76         try {
77             Object result = compiledMethod.executeVarargs("1", "2", "3");
78             if (!"1-2-3".equals(result)) {
79                 // Deoptimization probably occurred
80                 Assert.assertEquals("interpreter", result);
81             }
82         } catch (InvalidInstalledCodeException t) {
83             Assert.fail("method invalidated");
84         }
85     }
86 
87     @Test
test3()88     public void test3() {
89         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
90         InstalledCode compiledMethod = getCode(javaMethod);
91         try {
92             Object result = compiledMethod.executeVarargs("1", "2", "3");
93             Assert.assertEquals("1 2 3", result);
94         } catch (InvalidInstalledCodeException t) {
95             Assert.fail("method invalidated");
96         }
97     }
98 
99     @Test
test4()100     public void test4() {
101         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethodVirtual");
102         InstalledCode compiledMethod = getCode(javaMethod);
103         try {
104             f1 = "0";
105             Object result = compiledMethod.executeVarargs(this, "1", "2", "3");
106             Assert.assertEquals("0 1 2 3", result);
107         } catch (InvalidInstalledCodeException t) {
108             Assert.fail("method invalidated");
109         }
110     }
111 }
112