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 final class Final {
toString()18   public String toString() {
19     return "final";
20   }
21 }
22 
23 public class Main {
24   /// CHECK-START: Final Main.testKeepCheckCast(java.lang.Object, boolean) builder (after)
25   /// CHECK:    <<Phi:l\d+>>     Phi klass:java.lang.Object
26   /// CHECK:    <<Class:l\d+>>   LoadClass
27   /// CHECK:                     CheckCast [<<Phi>>,<<Class>>]
28   /// CHECK:    <<Ret:l\d+>>     BoundType [<<Phi>>] klass:Final
29   /// CHECK:                     Return [<<Ret>>]
30 
31   /// CHECK-START: Final Main.testKeepCheckCast(java.lang.Object, boolean) instruction_simplifier (after)
32   /// CHECK:    <<Phi:l\d+>>     Phi
33   /// CHECK:    <<Class:l\d+>>   LoadClass
34   /// CHECK:                     CheckCast [<<Phi>>,<<Class>>]
35   /// CHECK:    <<Ret:l\d+>>     BoundType [<<Phi>>]
36   /// CHECK:                     Return [<<Ret>>]
testKeepCheckCast(Object o, boolean cond)37   public static Final testKeepCheckCast(Object o, boolean cond) {
38     Object x = new Final();
39     while (cond) {
40       x = o;
41       cond = false;
42     }
43     return (Final) x;
44   }
45 
46   /// CHECK-START: void Main.testKeepInstanceOf(java.lang.Object, boolean) builder (after)
47   /// CHECK:    <<Phi:l\d+>>     Phi klass:java.lang.Object
48   /// CHECK:    <<Class:l\d+>>   LoadClass
49   /// CHECK:                     InstanceOf [<<Phi>>,<<Class>>]
50 
51   /// CHECK-START: void Main.testKeepInstanceOf(java.lang.Object, boolean) dead_code_elimination$initial (after)
52   /// CHECK:    <<Phi:l\d+>>     Phi
53   /// CHECK:    <<Class:l\d+>>   LoadClass
54   /// CHECK:                     InstanceOf [<<Phi>>,<<Class>>]
testKeepInstanceOf(Object o, boolean cond)55   public static void testKeepInstanceOf(Object o, boolean cond) {
56     Object x = new Final();
57     while (cond) {
58       x = o;
59       cond = false;
60     }
61     if (x instanceof Final) {
62       System.out.println("instanceof succeed");
63     } else {
64       System.out.println("instanceof failed");
65     }
66   }
67 
68   /// CHECK-START: java.lang.String Main.testNoInline(java.lang.Object, boolean) builder (after)
69   /// CHECK:    <<Phi:l\d+>>     Phi klass:java.lang.Object
70   /// CHECK:    <<NC:l\d+>>      NullCheck [<<Phi>>]
71   /// CHECK:    <<Ret:l\d+>>     InvokeVirtual [<<NC>>] method_name:java.lang.Object.toString
72   /// CHECK:                     Return [<<Ret>>]
73 
74   /// CHECK-START: java.lang.String Main.testNoInline(java.lang.Object, boolean) inliner (after)
75   /// CHECK:    <<Phi:l\d+>>     Phi
76   /// CHECK:    <<NC:l\d+>>      NullCheck [<<Phi>>]
77   /// CHECK:    <<Ret:l\d+>>     InvokeVirtual [<<NC>>] method_name:java.lang.Object.toString
78   /// CHECK:                     Return [<<Ret>>]
testNoInline(Object o, boolean cond)79   public static String testNoInline(Object o, boolean cond) {
80     Object x = new Final();
81     while (cond) {
82       x = o;
83       cond = false;
84     }
85     return x.toString();
86   }
87 
main(String[] args)88   public static void main(String[] args) {
89     try {
90       testKeepCheckCast(new Object(), true);
91       throw new Error("Expected check cast exception");
92     } catch (ClassCastException e) {
93       // expected
94     }
95 
96     testKeepInstanceOf(new Object(), true);
97 
98     if ("final".equals(testNoInline(new Object(), true))) {
99       throw new Error("Bad inlining");
100     }
101   }
102 }
103