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 package org.graalvm.compiler.core.test.deopt;
  24 
  25 import jdk.vm.ci.code.InstalledCode;
  26 import jdk.vm.ci.code.InvalidInstalledCodeException;
  27 import jdk.vm.ci.meta.JavaKind;
  28 import jdk.vm.ci.meta.ResolvedJavaMethod;
  29 
  30 import org.junit.Assert;
  31 import org.junit.Test;
  32 
  33 import org.graalvm.compiler.core.test.GraalCompilerTest;
  34 import org.graalvm.compiler.nodes.ConstantNode;
  35 import org.graalvm.compiler.nodes.StructuredGraph;
  36 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  37 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  38 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  39 import org.graalvm.compiler.phases.tiers.PhaseContext;
  40 
  41 /**
  42  * In the following tests, the usages of local variable "a" are replaced with the integer constant
  43  * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
  44  * graph of the method that just has a "return 1" statement in it.
  45  */
  46 public class CompiledMethodTest extends GraalCompilerTest {
  47 
  48     public static Object testMethod(Object arg1, Object arg2, Object arg3) {
  49         return arg1 + " " + arg2 + " " + arg3;
  50     }
  51 
  52     Object f1;
  53 
  54     public Object testMethodVirtual(Object arg1, Object arg2, Object arg3) {
  55         return f1 + " " + arg1 + " " + arg2 + " " + arg3;
  56     }
  57 
  58     @Test
  59     public void test1() {
  60         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
  61         final StructuredGraph graph = parseEager(javaMethod, AllowAssumptions.NO);
  62         new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
  63         new DeadCodeEliminationPhase().apply(graph);
  64 
  65         for (ConstantNode node : ConstantNode.getConstantNodes(graph)) {
  66             if (node.getStackKind() == JavaKind.Object && " ".equals(getSnippetReflection().asObject(String.class, node.asJavaConstant()))) {
  67                 node.replace(graph, ConstantNode.forConstant(getSnippetReflection().forObject("-"), getMetaAccess(), graph));
  68             }
  69         }
  70 
  71         InstalledCode compiledMethod = getCode(javaMethod, graph);
  72         try {
  73             Object result = compiledMethod.executeVarargs("1", "2", "3");
  74             Assert.assertEquals("1-2-3", result);
  75         } catch (InvalidInstalledCodeException t) {
  76             Assert.fail("method invalidated");
  77         }
  78     }
  79 
  80     @Test
  81     public void test3() {
  82         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
  83         InstalledCode compiledMethod = getCode(javaMethod);
  84         try {
  85             Object result = compiledMethod.executeVarargs("1", "2", "3");
  86             Assert.assertEquals("1 2 3", result);
  87         } catch (InvalidInstalledCodeException t) {
  88             Assert.fail("method invalidated");
  89         }
  90     }
  91 
  92     @Test
  93     public void test4() {
  94         final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethodVirtual");
  95         InstalledCode compiledMethod = getCode(javaMethod);
  96         try {
  97             f1 = "0";
  98             Object result = compiledMethod.executeVarargs(this, "1", "2", "3");
  99             Assert.assertEquals("0 1 2 3", result);
 100         } catch (InvalidInstalledCodeException t) {
 101             Assert.fail("method invalidated");
 102         }
 103     }
 104 }