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