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 jdk.internal.jvmci.meta.*; 026 027import org.junit.*; 028 029import com.oracle.graal.nodes.*; 030import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions; 031import com.oracle.graal.nodes.java.*; 032import com.oracle.graal.nodes.spi.*; 033import com.oracle.graal.phases.common.*; 034import com.oracle.graal.phases.common.inlining.*; 035import com.oracle.graal.phases.tiers.*; 036 037public class LockEliminationTest extends GraalCompilerTest { 038 039 static class A { 040 041 int value; 042 043 public synchronized int getValue() { 044 return value; 045 } 046 } 047 048 static int field1; 049 static int field2; 050 051 public static void testSynchronizedSnippet(A x, A y) { 052 synchronized (x) { 053 field1 = x.value; 054 } 055 synchronized (x) { 056 field2 = y.value; 057 } 058 } 059 060 @Test 061 public void testLock() { 062 test("testSynchronizedSnippet", new A(), new A()); 063 064 StructuredGraph graph = getGraph("testSynchronizedSnippet"); 065 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders())); 066 new LockEliminationPhase().apply(graph); 067 assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count()); 068 assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count()); 069 } 070 071 public static void testSynchronizedMethodSnippet(A x) { 072 int value1 = x.getValue(); 073 int value2 = x.getValue(); 074 field1 = value1; 075 field2 = value2; 076 } 077 078 @Test 079 public void testSynchronizedMethod() { 080 test("testSynchronizedMethodSnippet", new A()); 081 082 StructuredGraph graph = getGraph("testSynchronizedMethodSnippet"); 083 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders())); 084 new LockEliminationPhase().apply(graph); 085 assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count()); 086 assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count()); 087 } 088 089 private StructuredGraph getGraph(String snippet) { 090 ResolvedJavaMethod method = getResolvedJavaMethod(snippet); 091 StructuredGraph graph = parseEager(method, AllowAssumptions.YES); 092 HighTierContext context = getDefaultHighTierContext(); 093 new CanonicalizerPhase().apply(graph, context); 094 new InliningPhase(new CanonicalizerPhase()).apply(graph, context); 095 new CanonicalizerPhase().apply(graph, context); 096 new DeadCodeEliminationPhase().apply(graph); 097 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context); 098 new ValueAnchorCleanupPhase().apply(graph); 099 new LockEliminationPhase().apply(graph); 100 return graph; 101 } 102 103}