view graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/PushNodesThroughPiTest.java @ 11959:23ccaa863eda

made CodeCacheProvider independent of MetaAccessProvider (GRAAL-511)
author Doug Simon <doug.simon@oracle.com>
date Thu, 10 Oct 2013 16:14:55 +0200
parents 65cedae2647e
children 0fc653a9e019
line wrap: on
line source

/*
 * Copyright (c) 2013, 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.test;

import org.junit.*;

import com.oracle.graal.api.code.*;
import com.oracle.graal.api.meta.*;
import com.oracle.graal.debug.*;
import com.oracle.graal.nodes.*;
import com.oracle.graal.nodes.calc.*;
import com.oracle.graal.nodes.extended.*;
import com.oracle.graal.nodes.type.*;
import com.oracle.graal.phases.common.*;
import com.oracle.graal.phases.tiers.*;

public class PushNodesThroughPiTest extends GraalCompilerTest {

    public static class A {

        public long x = 20;
    }

    public static class B extends A {

        public long y = 10;
    }

    public static class C extends B {

        public long z = 5;
    }

    public static long test1Snippet(A a) {
        C c = (C) a;
        long ret = c.x; // this can be pushed before the checkcast
        ret += c.y; // not allowed to push
        ret += c.z; // not allowed to push
        // the null-check should be canonicalized with the null-check of the checkcast
        ret += c != null ? 100 : 200;
        return ret;
    }

    @Test
    public void test1() {
        final String snippet = "test1Snippet";
        Debug.scope("PushThroughPi", new DebugDumpScope(snippet), new Runnable() {

            public void run() {
                StructuredGraph graph = compileTestSnippet(snippet);

                for (ReadNode rn : graph.getNodes().filter(ReadNode.class)) {
                    if (rn.location() instanceof ConstantLocationNode && rn.object().stamp() instanceof ObjectStamp) {
                        long disp = ((ConstantLocationNode) rn.location()).getDisplacement();
                        ResolvedJavaType receiverType = ObjectStamp.typeOrNull(rn.object());
                        ResolvedJavaField field = receiverType.findInstanceFieldWithOffset(disp);

                        assert field != null : "Node " + rn + " tries to access a field which doesn't exists for this type";
                        if (field.getName().equals("x")) {
                            Assert.assertTrue(rn.object() instanceof LocalNode);
                        } else {
                            Assert.assertTrue(rn.object().toString(), rn.object() instanceof PiNode);
                        }
                    }
                }

                Assert.assertTrue(graph.getNodes().filter(IsNullNode.class).count() == 1);
            }
        });
    }

    private StructuredGraph compileTestSnippet(final String snippet) {
        StructuredGraph graph = parse(snippet);
        PhaseContext context = new PhaseContext(getMetaAccess(), getCodeCache(), new Assumptions(false), replacements);
        CanonicalizerPhase canonicalizer = new CanonicalizerPhase(true);
        new LoweringPhase(canonicalizer).apply(graph, context);
        canonicalizer.apply(graph, context);
        new PushThroughPiPhase().apply(graph);
        canonicalizer.apply(graph, context);

        return graph;
    }
}