view graal/com.oracle.graal.tests/src/com/oracle/graal/compiler/tests/EscapeAnalysisTest.java @ 5507:dc71b06d09f8

Moving classes from cri.ri to api.meta.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Thu, 07 Jun 2012 18:24:06 +0200
parents 13aee5aba8cc
children e18ba36bfebc
line wrap: on
line source

/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.graal.compiler.tests;

import junit.framework.Assert;

import org.junit.Test;

import com.oracle.graal.api.meta.*;
import com.oracle.graal.compiler.*;
import com.oracle.graal.compiler.phases.*;
import com.oracle.graal.debug.*;
import com.oracle.graal.nodes.*;
import com.oracle.graal.nodes.java.*;

/**
 * In these test cases the probability of all invokes is set to a high value, such that an InliningPhase should inline them all.
 * After that, the EscapeAnalysisPhase is expected to remove all allocations and return the correct values.
 */
public class EscapeAnalysisTest extends GraphTest {

    @Test
    public void test1() {
        test("test1Snippet", RiConstant.forInt(101));
    }

    @SuppressWarnings("all")
    public static int test1Snippet(int a) {
        Integer x = new Integer(101);
        return x.intValue();
    }

    @Test
    public void test2() {
        test("test2Snippet", RiConstant.forInt(0));
    }

    @SuppressWarnings("all")
    public static int test2Snippet(int a) {
        Integer[] x = new Integer[0];
        return x.length;
    }

    @Test
    public void test3() {
        test("test3Snippet", RiConstant.forObject(null));
    }

    @SuppressWarnings("all")
    public static Object test3Snippet(int a) {
        Integer[] x = new Integer[1];
        return x[0];
    }

    @Test
    public void testMonitor() {
        test("testMonitorSnippet", RiConstant.forInt(0));
    }

    private static native void notInlineable();

    @SuppressWarnings("all")
    public static int testMonitorSnippet(int a) {
        Integer x = new Integer(0);
        Integer[] y = new Integer[0];
        Integer[] z = new Integer[1];
        synchronized (x) {
            synchronized (y) {
                synchronized (z) {
                    notInlineable();
                }
            }
        }
        return x.intValue();
    }

    public void testMonitor2() {
        test("testMonitor2Snippet", RiConstant.forInt(0));
    }

    /**
     * This test case differs from the last one in that it requires inlining within a synchronized region.
     */
    @SuppressWarnings("all")
    public static int testMonitor2Snippet(int a) {
        Integer x = new Integer(0);
        Integer[] y = new Integer[0];
        Integer[] z = new Integer[1];
        synchronized (x) {
            synchronized (y) {
                synchronized (z) {
                    notInlineable();
                    return x.intValue();
                }
            }
        }
    }

    private void test(String snippet, RiConstant expectedResult) {
        StructuredGraph graph = parse(snippet);
        for (Invoke n : graph.getInvokes()) {
            n.node().setProbability(100000);
        }

        new InliningPhase(null, runtime(), null, null, null, getDefaultPhasePlan(), OptimisticOptimizations.ALL).apply(graph);
        new DeadCodeEliminationPhase().apply(graph);
        Debug.dump(graph, "Graph");
        new EscapeAnalysisPhase(null, runtime(), null, null, getDefaultPhasePlan(), OptimisticOptimizations.ALL).apply(graph);
        Debug.dump(graph, "Graph");
        int retCount = 0;
        for (ReturnNode ret : graph.getNodes(ReturnNode.class)) {
            Assert.assertTrue(ret.result().isConstant());
            Assert.assertEquals(ret.result().asConstant(), expectedResult);
            retCount++;
        }
        Assert.assertEquals(1, retCount);
        int newInstanceCount = 0;
        for (@SuppressWarnings("unused") NewInstanceNode n : graph.getNodes(NewInstanceNode.class)) {
            newInstanceCount++;
        }
        for (@SuppressWarnings("unused") NewObjectArrayNode n : graph.getNodes(NewObjectArrayNode.class)) {
            newInstanceCount++;
        }
        Assert.assertEquals(0, newInstanceCount);
    }
}