1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 class OtherClass {
18   static {
19     a = 42;
20     b = 54;
21   }
22 
23   static int a;
24   static int b;
25 }
26 
27 public final class Main {
28 
29   /// CHECK-START: int Main.accessTwoStatics() GVN (before)
30   /// CHECK-DAG:     <<Class1:l\d+>>  LoadClass
31   /// CHECK-DAG:                      ClinitCheck [<<Class1>>]
32   /// CHECK-DAG:     <<Class2:l\d+>>  LoadClass
33   /// CHECK-DAG:                      ClinitCheck [<<Class2>>]
34 
35   /// CHECK-START: int Main.accessTwoStatics() GVN (after)
36   /// CHECK-DAG:     <<Class:l\d+>>   LoadClass
37   /// CHECK-DAG:                      ClinitCheck [<<Class>>]
38   /// CHECK-NOT:                      ClinitCheck
39 
accessTwoStatics()40   public static int accessTwoStatics() {
41     return OtherClass.b - OtherClass.a;
42   }
43 
44   /// CHECK-START: int Main.accessTwoStaticsCallInBetween() GVN (before)
45   /// CHECK-DAG:     <<Class1:l\d+>>  LoadClass
46   /// CHECK-DAG:                      ClinitCheck [<<Class1>>]
47   /// CHECK-DAG:     <<Class2:l\d+>>  LoadClass
48   /// CHECK-DAG:                      ClinitCheck [<<Class2>>]
49 
50   /// CHECK-START: int Main.accessTwoStaticsCallInBetween() GVN (after)
51   /// CHECK-DAG:     <<Class:l\d+>>   LoadClass
52   /// CHECK-DAG:                      ClinitCheck [<<Class>>]
53   /// CHECK-NOT:                      ClinitCheck
54 
accessTwoStaticsCallInBetween()55   public static int accessTwoStaticsCallInBetween() {
56     int b = OtherClass.b;
57     foo();
58     return b - OtherClass.a;
59   }
60 
foo()61   public static void foo() {
62     try {
63       Thread.sleep(0);
64     } catch (Exception e) {
65       throw new Error(e);
66     }
67   }
68 
main(String[] args)69   public static void main(String[] args) {
70     if (accessTwoStatics() != 12) {
71       throw new Error("Expected 12");
72     }
73 
74     if (accessTwoStaticsCallInBetween() != 12) {
75       throw new Error("Expected 12");
76     }
77   }
78 }
79