src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/LockEliminationTest.java
changeset 47216 71c04702a3d5
parent 46371 0337d0617e7b
child 50858 2d3e99a72541
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     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 import org.graalvm.compiler.loop.DefaultLoopPolicies;
       
    29 import org.graalvm.compiler.loop.phases.LoopFullUnrollPhase;
       
    30 import org.graalvm.compiler.nodes.StructuredGraph;
       
    31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
       
    32 import org.graalvm.compiler.nodes.java.MonitorExitNode;
       
    33 import org.graalvm.compiler.nodes.java.RawMonitorEnterNode;
       
    34 import org.graalvm.compiler.nodes.spi.LoweringTool;
       
    35 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
       
    36 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
       
    37 import org.graalvm.compiler.phases.common.LockEliminationPhase;
       
    38 import org.graalvm.compiler.phases.common.LoweringPhase;
       
    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     public void testUnrolledSyncSnippet(Object a) {
       
    96         for (int i = 0; i < 3; i++) {
       
    97             synchronized (a) {
       
    98 
       
    99             }
       
   100         }
       
   101     }
       
   102 
       
   103     @Test
       
   104     public void testUnrolledSync() {
       
   105         StructuredGraph graph = getGraph("testUnrolledSyncSnippet");
       
   106         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
       
   107         canonicalizer.apply(graph, new PhaseContext(getProviders()));
       
   108         HighTierContext context = getDefaultHighTierContext();
       
   109         new LoopFullUnrollPhase(canonicalizer, new DefaultLoopPolicies()).apply(graph, context);
       
   110         new LockEliminationPhase().apply(graph);
       
   111         assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count());
       
   112         assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count());
       
   113     }
       
   114 
       
   115     private StructuredGraph getGraph(String snippet) {
       
   116         ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
       
   117         StructuredGraph graph = parseEager(method, AllowAssumptions.YES);
       
   118         HighTierContext context = getDefaultHighTierContext();
       
   119         new CanonicalizerPhase().apply(graph, context);
       
   120         new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
       
   121         new CanonicalizerPhase().apply(graph, context);
       
   122         new DeadCodeEliminationPhase().apply(graph);
       
   123         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
       
   124         new LockEliminationPhase().apply(graph);
       
   125         return graph;
       
   126     }
       
   127 
       
   128 }