001/*
002 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.test.deopt;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import org.junit.*;
029
030import com.oracle.graal.compiler.test.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
033import com.oracle.graal.phases.common.*;
034import com.oracle.graal.phases.tiers.*;
035
036/**
037 * In the following tests, the usages of local variable "a" are replaced with the integer constant
038 * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
039 * graph of the method that just has a "return 1" statement in it.
040 */
041public class CompiledMethodTest extends GraalCompilerTest {
042
043    public static Object testMethod(Object arg1, Object arg2, Object arg3) {
044        return arg1 + " " + arg2 + " " + arg3;
045    }
046
047    Object f1;
048
049    public Object testMethodVirtual(Object arg1, Object arg2, Object arg3) {
050        return f1 + " " + arg1 + " " + arg2 + " " + arg3;
051    }
052
053    @Test
054    public void test1() {
055        final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
056        final StructuredGraph graph = parseEager(javaMethod, AllowAssumptions.NO);
057        new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
058        new DeadCodeEliminationPhase().apply(graph);
059
060        for (ConstantNode node : ConstantNode.getConstantNodes(graph)) {
061            if (node.getKind() == Kind.Object && " ".equals(getSnippetReflection().asObject(String.class, node.asJavaConstant()))) {
062                node.replace(graph, ConstantNode.forConstant(getSnippetReflection().forObject("-"), getMetaAccess(), graph));
063            }
064        }
065
066        InstalledCode compiledMethod = getCode(javaMethod, graph);
067        try {
068            Object result = compiledMethod.executeVarargs("1", "2", "3");
069            Assert.assertEquals("1-2-3", result);
070        } catch (InvalidInstalledCodeException t) {
071            Assert.fail("method invalidated");
072        }
073    }
074
075    @Test
076    public void test3() {
077        final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethod");
078        InstalledCode compiledMethod = getCode(javaMethod);
079        try {
080            Object result = compiledMethod.executeVarargs("1", "2", "3");
081            Assert.assertEquals("1 2 3", result);
082        } catch (InvalidInstalledCodeException t) {
083            Assert.fail("method invalidated");
084        }
085    }
086
087    @Test
088    public void test4() {
089        final ResolvedJavaMethod javaMethod = getResolvedJavaMethod("testMethodVirtual");
090        InstalledCode compiledMethod = getCode(javaMethod);
091        try {
092            f1 = "0";
093            Object result = compiledMethod.executeVarargs(this, "1", "2", "3");
094            Assert.assertEquals("0 1 2 3", result);
095        } catch (InvalidInstalledCodeException t) {
096            Assert.fail("method invalidated");
097        }
098    }
099}