1 /*
   2  * Copyright (c) 2011, 2018, 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 org.graalvm.compiler.loop.DefaultLoopPolicies;
  28 import org.graalvm.compiler.loop.phases.LoopFullUnrollPhase;
  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.tiers.HighTierContext;
  39 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  40 import org.junit.Test;
  41 
  42 import jdk.vm.ci.meta.ResolvedJavaMethod;
  43 
  44 public class LockEliminationTest extends GraalCompilerTest {
  45 
  46     static class A {
  47 
  48         int value;
  49 
  50         public synchronized int getValue() {
  51             return value;
  52         }
  53     }
  54 
  55     static int field1;
  56     static int field2;
  57 
  58     public static void testSynchronizedSnippet(A x, A y) {
  59         synchronized (x) {
  60             field1 = x.value;
  61         }
  62         synchronized (x) {
  63             field2 = y.value;
  64         }
  65     }
  66 
  67     @Test
  68     public void testLock() {
  69         test("testSynchronizedSnippet", new A(), new A());
  70 
  71         StructuredGraph graph = getGraph("testSynchronizedSnippet", false);
  72         new CanonicalizerPhase().apply(graph, getProviders());
  73         new LockEliminationPhase().apply(graph);
  74         assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count());
  75         assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count());
  76     }
  77 
  78     public static void testSynchronizedMethodSnippet(A x) {
  79         int value1 = x.getValue();
  80         int value2 = x.getValue();
  81         field1 = value1;
  82         field2 = value2;
  83     }
  84 
  85     @Test
  86     public void testSynchronizedMethod() {
  87         test("testSynchronizedMethodSnippet", new A());
  88 
  89         StructuredGraph graph = getGraph("testSynchronizedMethodSnippet", false);
  90         new CanonicalizerPhase().apply(graph, getProviders());
  91         new LockEliminationPhase().apply(graph);
  92         assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count());
  93         assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count());
  94     }
  95 
  96     public void testUnrolledSyncSnippet(Object a) {
  97         for (int i = 0; i < 3; i++) {
  98             synchronized (a) {
  99 
 100             }
 101         }
 102     }
 103 
 104     @Test
 105     public void testUnrolledSync() {
 106         StructuredGraph graph = getGraph("testUnrolledSyncSnippet", false);
 107         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 108         canonicalizer.apply(graph, getProviders());
 109         HighTierContext context = getDefaultHighTierContext();
 110         new LoopFullUnrollPhase(canonicalizer, new DefaultLoopPolicies()).apply(graph, context);
 111         new LockEliminationPhase().apply(graph);
 112         assertDeepEquals(1, graph.getNodes().filter(RawMonitorEnterNode.class).count());
 113         assertDeepEquals(1, graph.getNodes().filter(MonitorExitNode.class).count());
 114     }
 115 
 116     private StructuredGraph getGraph(String snippet, boolean doEscapeAnalysis) {
 117         ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
 118         StructuredGraph graph = parseEager(method, AllowAssumptions.YES);
 119         HighTierContext context = getDefaultHighTierContext();
 120         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 121         canonicalizer.apply(graph, context);
 122         createInliningPhase().apply(graph, context);
 123         new CanonicalizerPhase().apply(graph, context);
 124         new DeadCodeEliminationPhase().apply(graph);
 125         if (doEscapeAnalysis) {
 126             new PartialEscapePhase(true, canonicalizer, graph.getOptions()).apply(graph, context);
 127         }
 128         new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
 129         return graph;
 130     }
 131 
 132     public void testEscapeAnalysisSnippet(A a) {
 133         A newA = new A();
 134         synchronized (newA) {
 135             synchronized (a) {
 136                 field1 = a.value;
 137             }
 138         }
 139         /*
 140          * Escape analysis removes the synchronization on newA. But lock elimination still must not
 141          * combine the two synchronizations on the parameter a because they have a different lock
 142          * depth.
 143          */
 144         synchronized (a) {
 145             field2 = a.value;
 146         }
 147         /*
 148          * Lock elimination can combine these synchronizations, since they are both on parameter a
 149          * with the same lock depth.
 150          */
 151         synchronized (a) {
 152             field1 = a.value;
 153         }
 154     }
 155 
 156     @Test
 157     public void testEscapeAnalysis() {
 158         StructuredGraph graph = getGraph("testEscapeAnalysisSnippet", true);
 159 
 160         assertDeepEquals(3, graph.getNodes().filter(RawMonitorEnterNode.class).count());
 161         assertDeepEquals(3, graph.getNodes().filter(MonitorExitNode.class).count());
 162 
 163         new LockEliminationPhase().apply(graph);
 164 
 165         assertDeepEquals(2, graph.getNodes().filter(RawMonitorEnterNode.class).count());
 166         assertDeepEquals(2, graph.getNodes().filter(MonitorExitNode.class).count());
 167     }
 168 }