1 /*
  2  * Copyright (c) 2024, 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  * @test
 26  * @summary test lowering of synchronized blocks
 27  * @build TestSynchronized
 28  * @build CodeReflectionTester
 29  * @run main CodeReflectionTester TestSynchronized
 30  */
 31 
 32 import java.lang.runtime.CodeReflection;
 33 
 34 public class TestSynchronized {
 35 
 36     @CodeReflection
 37     @LoweredModel(value = """
 38             func @"test1" (%0 : java.lang.Object, %1 : int)int -> {
 39                 %2 : Var<java.lang.Object> = var %0 @"m";
 40                 %3 : Var<int> = var %1 @"i";
 41                 %4 : java.lang.Object = var.load %2;
 42                 branch ^block_1(%4);
 43 
 44               ^block_1(%5 : java.lang.Object):
 45                 monitor.enter %5;
 46                 %6 : java.lang.reflect.code.op.CoreOp$ExceptionRegion = exception.region.enter ^block_2 ^block_4;
 47 
 48               ^block_2:
 49                 %7 : int = var.load %3;
 50                 %8 : int = constant @"1";
 51                 %9 : int = add %7 %8;
 52                 var.store %3 %9;
 53                 monitor.exit %5;
 54                 exception.region.exit %6 ^block_3;
 55 
 56               ^block_3:
 57                 %10 : int = var.load %3;
 58                 return %10;
 59 
 60               ^block_4(%11 : java.lang.Throwable):
 61                 %12 : java.lang.reflect.code.op.CoreOp$ExceptionRegion = exception.region.enter ^block_5 ^block_4;
 62 
 63               ^block_5:
 64                 monitor.exit %5;
 65                 exception.region.exit %12 ^block_6;
 66 
 67               ^block_6:
 68                 throw %11;
 69             };
 70             """, ssa = false)
 71     static int test1(Object m, int i) {
 72         synchronized (m) {
 73             i++;
 74         }
 75         return i;
 76     }
 77 
 78 
 79     @CodeReflection
 80     @LoweredModel(value = """
 81             func @"test2" (%0 : java.lang.Object, %1 : int)int -> {
 82                 %2 : Var<java.lang.Object> = var %0 @"m";
 83                 %3 : Var<int> = var %1 @"i";
 84                 %4 : java.lang.Object = var.load %2;
 85                 branch ^block_1(%4);
 86 
 87               ^block_1(%5 : java.lang.Object):
 88                 monitor.enter %5;
 89                 %6 : java.lang.reflect.code.op.CoreOp$ExceptionRegion = exception.region.enter ^block_2 ^block_8;
 90 
 91               ^block_2:
 92                 %7 : int = var.load %3;
 93                 %8 : int = constant @"0";
 94                 %9 : boolean = gt %7 %8;
 95                 cbranch %9 ^block_3 ^block_5;
 96 
 97               ^block_3:
 98                 %10 : int = constant @"-1";
 99                 monitor.exit %5;
100                 exception.region.exit %6 ^block_4;
101 
102               ^block_4:
103                 return %10;
104 
105               ^block_5:
106                 branch ^block_6;
107 
108               ^block_6:
109                 %11 : int = var.load %3;
110                 %12 : int = constant @"1";
111                 %13 : int = add %11 %12;
112                 var.store %3 %13;
113                 monitor.exit %5;
114                 exception.region.exit %6 ^block_7;
115 
116               ^block_7:
117                 %14 : int = var.load %3;
118                 return %14;
119 
120               ^block_8(%15 : java.lang.Throwable):
121                 %16 : java.lang.reflect.code.op.CoreOp$ExceptionRegion = exception.region.enter ^block_9 ^block_8;
122 
123               ^block_9:
124                 monitor.exit %5;
125                 exception.region.exit %16 ^block_10;
126 
127               ^block_10:
128                 throw %15;
129             };
130             """, ssa = false)
131     static int test2(Object m, int i) {
132         synchronized (m) {
133             if (i > 0) {
134                 return -1;
135             }
136             i++;
137         }
138         return i;
139     }
140 
141 }