001/*
002 * Copyright (c) 2011, 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 static com.oracle.graal.graph.iterators.NodePredicates.*;
026
027import java.util.*;
028
029import org.junit.*;
030
031import com.oracle.graal.graph.*;
032import com.oracle.graal.graph.iterators.*;
033import com.oracle.graal.nodes.*;
034import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
035import com.oracle.graal.nodes.java.*;
036import com.oracle.graal.phases.common.*;
037import com.oracle.graal.phases.common.inlining.*;
038import com.oracle.graal.phases.tiers.*;
039
040/**
041 * In the following tests, the usages of local variable "a" are replaced with the integer constant
042 * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
043 * graph of the method that just has a "return 1" statement in it.
044 */
045public class MonitorGraphTest extends GraalCompilerTest {
046
047    private static final String REFERENCE_SNIPPET = "referenceSnippet";
048
049    @SuppressWarnings("all")
050    public static synchronized int referenceSnippet(int a) {
051        return 1;
052    }
053
054    public static int const1() {
055        return 1;
056    }
057
058    @Test
059    public void test1() {
060        test("test1Snippet");
061    }
062
063    @SuppressWarnings("all")
064    public static synchronized int test1Snippet(int a) {
065        return const1();
066    }
067
068    @Test
069    public void test2() {
070        StructuredGraph graph = parseAndProcess("test2Snippet");
071        NodeIterable<MonitorExitNode> monitors = graph.getNodes(MonitorExitNode.TYPE);
072        Assert.assertEquals(1, monitors.count());
073        Assert.assertEquals(monitors.first().stateAfter().bci, 3);
074    }
075
076    @SuppressWarnings("all")
077    public static int test2Snippet(int a) {
078        return const2();
079    }
080
081    public static synchronized int const2() {
082        return 1;
083    }
084
085    private StructuredGraph parseAndProcess(String snippet) {
086        StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
087        ParameterNode param = graph.getNodes(ParameterNode.TYPE).first();
088        if (param != null) {
089            ConstantNode constant = ConstantNode.forInt(0, graph);
090            for (Node n : param.usages().filter(isNotA(FrameState.class)).snapshot()) {
091                n.replaceFirstInput(param, constant);
092            }
093        }
094        Map<Invoke, Double> hints = new HashMap<>();
095        for (Invoke invoke : graph.getInvokes()) {
096            hints.put(invoke, 1000d);
097        }
098        HighTierContext context = getDefaultHighTierContext();
099        new InliningPhase(hints, new CanonicalizerPhase()).apply(graph, context);
100        new CanonicalizerPhase().apply(graph, context);
101        new DeadCodeEliminationPhase().apply(graph);
102        return graph;
103    }
104
105    private void test(String snippet) {
106        StructuredGraph graph = parseAndProcess(snippet);
107        StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.NO);
108        assertEquals(referenceGraph, graph);
109    }
110}