1 /*
2  * Copyright (C) 2017 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 public class Main {
18 
19   /// CHECK-START: int Main.bar(Main) inliner (before)
20   /// CHECK: InvokeVirtual
21   /// CHECK: InvokeVirtual
22 
23   /// CHECK-START: int Main.bar(Main) inliner (after)
24   /// CHECK-NOT: InvokeVirtual
bar(Main m)25   public static int bar(Main m) {
26     if (m.getClass() == Main.class) {
27       return m.foo();
28     }
29     return 4;
30   }
31 
foo()32   public int foo() {
33     return 42;
34   }
35 
36   /// CHECK-START: boolean Main.classEquality1() instruction_simplifier$after_inlining (before)
37   /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
38   /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
39   /// CHECK-DAG: <<Eq:z\d+>>     {{Equal|NotEqual}}
40   /// CHECK-DAG:                 If [<<Eq>>]
41   /// CHECK-DAG: <<Phi:i\d+>>    Phi [<<Const1>>,<<Const0>>]
42   /// CHECK-DAG:                 Return [<<Phi>>]
43 
44   /// CHECK-START: boolean Main.classEquality1() dead_code_elimination$after_inlining (after)
45   /// CHECK-DAG: <<Constant:i\d+>> IntConstant 1
46   /// CHECK-DAG:                   Return [<<Constant>>]
classEquality1()47   public static boolean classEquality1() {
48     return new Main().getClass() == Main.class;
49   }
50 
51   /// CHECK-START: boolean Main.classEquality2() instruction_simplifier$after_inlining (before)
52   /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
53   /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
54   /// CHECK-DAG: <<Eq:z\d+>>     {{Equal|NotEqual}}
55   /// CHECK-DAG:                 If [<<Eq>>]
56   /// CHECK-DAG: <<Phi:i\d+>>    Phi [<<Const1>>,<<Const0>>]
57   /// CHECK-DAG:                 Return [<<Phi>>]
58 
59   /// CHECK-START: boolean Main.classEquality2() dead_code_elimination$after_inlining (after)
60   /// CHECK-DAG: <<Constant:i\d+>> IntConstant 0
61   /// CHECK-DAG:                   Return [<<Constant>>]
classEquality2()62   public static boolean classEquality2() {
63     Object o = new SubMain();
64     return o.getClass() == Main.class;
65   }
66 
67   /// CHECK-START: boolean Main.classEquality3() instruction_simplifier$after_inlining (before)
68   /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
69   /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
70   /// CHECK-DAG: <<Eq:z\d+>>     {{Equal|NotEqual}}
71   /// CHECK-DAG:                 If [<<Eq>>]
72   /// CHECK-DAG: <<Phi:i\d+>>    Phi [<<Const1>>,<<Const0>>]
73   /// CHECK-DAG:                 Return [<<Phi>>]
74 
75   /// CHECK-START: boolean Main.classEquality3() dead_code_elimination$after_inlining (after)
76   /// CHECK-DAG: <<Constant:i\d+>> IntConstant 0
77   /// CHECK-DAG:                   Return [<<Constant>>]
classEquality3()78   public static boolean classEquality3() {
79     return new Main().getClass() != Main.class;
80   }
81 
82   /// CHECK-START: boolean Main.classEquality4() instruction_simplifier$after_inlining (before)
83   /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
84   /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
85   /// CHECK-DAG: <<Eq:z\d+>>     {{Equal|NotEqual}}
86   /// CHECK-DAG:                 If [<<Eq>>]
87   /// CHECK-DAG: <<Phi:i\d+>>    Phi [<<Const1>>,<<Const0>>]
88   /// CHECK-DAG:                 Return [<<Phi>>]
89 
90   /// CHECK-START: boolean Main.classEquality4() dead_code_elimination$after_inlining (after)
91   /// CHECK-DAG: <<Constant:i\d+>> IntConstant 1
92   /// CHECK-DAG:                   Return [<<Constant>>]
classEquality4()93   public static boolean classEquality4() {
94     Object o = new SubMain();
95     return o.getClass() != Main.class;
96   }
97 
main(String[] args)98   public static void main(String[] args) {
99     int actual = bar(new Main());
100     if (actual != 42) {
101       throw new Error("Expected 42, got " + actual);
102     }
103     actual = bar(new SubMain());
104     if (actual != 4) {
105       throw new Error("Expected 4, got " + actual);
106     }
107   }
108 }
109 
110 class SubMain extends Main {
111 }
112