1 /* 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.core.test; 26 27 import java.util.HashMap; 28 import java.util.Map; 29 30 import org.junit.Assert; 31 import org.junit.Test; 32 33 import org.graalvm.compiler.graph.Node; 34 import org.graalvm.compiler.graph.iterators.NodeIterable; 35 import org.graalvm.compiler.nodes.ConstantNode; 36 import org.graalvm.compiler.nodes.FrameState; 37 import org.graalvm.compiler.nodes.Invoke; 38 import org.graalvm.compiler.nodes.ParameterNode; 39 import org.graalvm.compiler.nodes.StructuredGraph; 40 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 41 import org.graalvm.compiler.nodes.java.MonitorExitNode; 42 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 43 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; 44 import org.graalvm.compiler.phases.common.inlining.InliningPhase; 45 import org.graalvm.compiler.phases.tiers.HighTierContext; 46 47 /** 48 * In the following tests, the usages of local variable "a" are replaced with the integer constant 49 * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the 50 * graph of the method that just has a "return 1" statement in it. 51 */ 52 public class MonitorGraphTest extends GraalCompilerTest { 53 54 private static final String REFERENCE_SNIPPET = "referenceSnippet"; 55 56 @SuppressWarnings("all") 57 public static synchronized int referenceSnippet(int a) { 58 return 1; 59 } 60 61 public static int const1() { 62 return 1; 63 } 64 65 @Test 66 public void test1() { 67 test("test1Snippet"); 68 } 69 70 @SuppressWarnings("all") 71 public static synchronized int test1Snippet(int a) { 72 return const1(); 73 } 74 75 @Test 76 public void test2() { 77 StructuredGraph graph = parseAndProcess("test2Snippet"); 78 NodeIterable<MonitorExitNode> monitors = graph.getNodes(MonitorExitNode.TYPE); 79 Assert.assertEquals(1, monitors.count()); 80 Assert.assertEquals(monitors.first().stateAfter().bci, 3); 81 } 82 83 @SuppressWarnings("all") 84 public static int test2Snippet(int a) { 85 return const2(); 86 } 87 88 public static synchronized int const2() { 89 return 1; 90 } 91 92 private StructuredGraph parseAndProcess(String snippet) { 93 StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO); 94 ParameterNode param = graph.getNodes(ParameterNode.TYPE).first(); 95 if (param != null) { 96 ConstantNode constant = ConstantNode.forInt(0, graph); 97 for (Node n : param.usages().snapshot()) { 98 if (!(n instanceof FrameState)) { 99 n.replaceFirstInput(param, constant); 100 } 101 } 102 } 103 Map<Invoke, Double> hints = new HashMap<>(); 104 for (Invoke invoke : graph.getInvokes()) { 105 hints.put(invoke, 1000d); 106 } 107 HighTierContext context = getDefaultHighTierContext(); 108 new InliningPhase(hints, new CanonicalizerPhase()).apply(graph, context); 109 new CanonicalizerPhase().apply(graph, context); 110 new DeadCodeEliminationPhase().apply(graph); 111 return graph; 112 } 113 114 private void test(String snippet) { 115 StructuredGraph graph = parseAndProcess(snippet); 116 StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.NO); 117 assertEquals(referenceGraph, graph); 118 } 119 }