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