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 package org.graalvm.compiler.core.test; 24 25 import jdk.vm.ci.meta.ResolvedJavaMethod; 26 27 import org.junit.Test; 28 29 import org.graalvm.compiler.nodes.StructuredGraph; 30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 31 import org.graalvm.compiler.nodes.java.MonitorExitNode; 32 import org.graalvm.compiler.nodes.java.RawMonitorEnterNode; 33 import org.graalvm.compiler.nodes.spi.LoweringTool; 34 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 35 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase; 36 import org.graalvm.compiler.phases.common.LockEliminationPhase; 37 import org.graalvm.compiler.phases.common.LoweringPhase; 38 import org.graalvm.compiler.phases.common.ValueAnchorCleanupPhase; 39 import org.graalvm.compiler.phases.common.inlining.InliningPhase; 40 import org.graalvm.compiler.phases.tiers.HighTierContext; 41 import org.graalvm.compiler.phases.tiers.PhaseContext; 42 43 public class LockEliminationTest extends GraalCompilerTest { 44 45 static class A { 46 47 int value; 48 49 public synchronized int getValue() { 50 return value; 51 } 52 } 53 54 static int field1; 55 static int field2; 56 57 public static void testSynchronizedSnippet(A x, A y) { 58 synchronized (x) { 59 field1 = x.value; 60 } 61 synchronized (x) { 62 field2 = y.value; 63 } 64 } 65 66 @Test 67 public void testLock() { 68 test("testSynchronizedSnippet", new A(), new A()); 69 70 StructuredGraph graph = getGraph("testSynchronizedSnippet"); 71 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders())); 72 new LockEliminationPhase().apply(graph); 73 assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count()); 74 assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count()); 75 } 76 77 public static void testSynchronizedMethodSnippet(A x) { 78 int value1 = x.getValue(); 79 int value2 = x.getValue(); 80 field1 = value1; 81 field2 = value2; 82 } 83 84 @Test 85 public void testSynchronizedMethod() { 86 test("testSynchronizedMethodSnippet", new A()); 87 88 StructuredGraph graph = getGraph("testSynchronizedMethodSnippet"); 89 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders())); 90 new LockEliminationPhase().apply(graph); 91 assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count()); 92 assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count()); 93 } 94 95 private StructuredGraph getGraph(String snippet) { 96 ResolvedJavaMethod method = getResolvedJavaMethod(snippet); 97 StructuredGraph graph = parseEager(method, AllowAssumptions.YES); 98 HighTierContext context = getDefaultHighTierContext(); 99 new CanonicalizerPhase().apply(graph, context); 100 new InliningPhase(new CanonicalizerPhase()).apply(graph, context); 101 new CanonicalizerPhase().apply(graph, context); 102 new DeadCodeEliminationPhase().apply(graph); 103 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); 104 new ValueAnchorCleanupPhase().apply(graph); 105 new LockEliminationPhase().apply(graph); 106 return graph; 107 } 108 109 }