001/*
002 * Copyright (c) 2011, 2015, 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;
024
025import com.oracle.graal.debug.*;
026import com.oracle.graal.debug.Debug.*;
027
028import org.junit.*;
029
030import com.oracle.graal.graph.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
033import com.oracle.graal.nodes.extended.*;
034import com.oracle.graal.nodes.memory.*;
035import com.oracle.graal.nodes.spi.*;
036import com.oracle.graal.phases.common.*;
037import com.oracle.graal.phases.tiers.*;
038
039public class FloatingReadTest extends GraphScheduleTest {
040
041    public static class Container {
042
043        public int a;
044    }
045
046    public static void changeField(Container c) {
047        c.a = 0xcafebabe;
048    }
049
050    public static synchronized int test1Snippet() {
051        Container c = new Container();
052        return c.a;
053    }
054
055    @Test
056    public void test1() {
057        test("test1Snippet");
058    }
059
060    private void test(final String snippet) {
061        try (Scope s = Debug.scope("FloatingReadTest", new DebugDumpScope(snippet))) {
062
063            StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
064            PhaseContext context = new PhaseContext(getProviders());
065            new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
066            new FloatingReadPhase().apply(graph);
067
068            ReturnNode returnNode = null;
069            MonitorExit monitorexit = null;
070
071            for (Node n : graph.getNodes()) {
072                if (n instanceof ReturnNode) {
073                    assert returnNode == null;
074                    returnNode = (ReturnNode) n;
075                } else if (n instanceof MonitorExit) {
076                    monitorexit = (MonitorExit) n;
077                }
078            }
079
080            Debug.dump(graph, "After lowering");
081
082            Assert.assertNotNull(returnNode);
083            Assert.assertNotNull(monitorexit);
084            Assert.assertTrue(returnNode.result() instanceof FloatingReadNode);
085
086            FloatingReadNode read = (FloatingReadNode) returnNode.result();
087
088            assertOrderedAfterSchedule(graph, read, (Node) monitorexit);
089        } catch (Throwable e) {
090            throw Debug.handle(e);
091        }
092    }
093}