xref: /aosp_15_r20/art/test/656-loop-deopt/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker public class Main {
main(String[] args)18*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) throws Exception {
19*795d594fSAndroid Build Coastguard Worker     System.loadLibrary(args[0]);
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker     $noinline$intUpdate(new Main());
22*795d594fSAndroid Build Coastguard Worker     ensureJitCompiled(Main.class, "$noinline$intUpdate");
23*795d594fSAndroid Build Coastguard Worker     $noinline$intUpdate(new SubMain());
24*795d594fSAndroid Build Coastguard Worker     if (myIntStatic != 5000) {
25*795d594fSAndroid Build Coastguard Worker       throw new Error("Expected 5000, got " + myIntStatic);
26*795d594fSAndroid Build Coastguard Worker     }
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker     $noinline$objectUpdate(new Main());
29*795d594fSAndroid Build Coastguard Worker     ensureJitCompiled(Main.class, "$noinline$objectUpdate");
30*795d594fSAndroid Build Coastguard Worker     $noinline$objectUpdate(new SubMain());
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker     $noinline$loopIncrement(new Main());
33*795d594fSAndroid Build Coastguard Worker     ensureJitCompiled(Main.class, "$noinline$loopIncrement");
34*795d594fSAndroid Build Coastguard Worker     $noinline$loopIncrement(new SubMain());
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker     $noinline$objectReturned(new Main());
37*795d594fSAndroid Build Coastguard Worker     ensureJitCompiled(Main.class, "$noinline$objectReturned");
38*795d594fSAndroid Build Coastguard Worker     Object o = $noinline$objectReturned(new SubMain());
39*795d594fSAndroid Build Coastguard Worker     // We used to get 0xebadde09 in 'o' here and therefore crash
40*795d594fSAndroid Build Coastguard Worker     // both interpreter and compiled code.
41*795d594fSAndroid Build Coastguard Worker     if (o instanceof Cloneable) {
42*795d594fSAndroid Build Coastguard Worker       System.out.println("Unexpected object type " + o.getClass());
43*795d594fSAndroid Build Coastguard Worker     }
44*795d594fSAndroid Build Coastguard Worker   }
45*795d594fSAndroid Build Coastguard Worker 
doCheck()46*795d594fSAndroid Build Coastguard Worker   public boolean doCheck() {
47*795d594fSAndroid Build Coastguard Worker     return false;
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker 
$noinline$intUpdate(Main m)50*795d594fSAndroid Build Coastguard Worker   public static void $noinline$intUpdate(Main m) {
51*795d594fSAndroid Build Coastguard Worker     int a = 0;
52*795d594fSAndroid Build Coastguard Worker     // We used to kill 'a' when the inline cache of 'doCheck' only
53*795d594fSAndroid Build Coastguard Worker     // contains 'Main' (which makes the only branch using 'a' dead).
54*795d594fSAndroid Build Coastguard Worker     // So the deoptimization at the inline cache was incorrectly assuming
55*795d594fSAndroid Build Coastguard Worker     // 'a' was dead.
56*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < 5000; i++) {
57*795d594fSAndroid Build Coastguard Worker       if (m.doCheck()) {
58*795d594fSAndroid Build Coastguard Worker         a++;
59*795d594fSAndroid Build Coastguard Worker         // We make this branch the only true user of the 'a' phi. All other uses
60*795d594fSAndroid Build Coastguard Worker         // of 'a' are phi updates.
61*795d594fSAndroid Build Coastguard Worker         myIntStatic = a;
62*795d594fSAndroid Build Coastguard Worker       } else if (myIntStatic == 42) {
63*795d594fSAndroid Build Coastguard Worker         a = 1;
64*795d594fSAndroid Build Coastguard Worker       }
65*795d594fSAndroid Build Coastguard Worker     }
66*795d594fSAndroid Build Coastguard Worker   }
67*795d594fSAndroid Build Coastguard Worker 
$noinline$objectUpdate(Main m)68*795d594fSAndroid Build Coastguard Worker   public static void $noinline$objectUpdate(Main m) {
69*795d594fSAndroid Build Coastguard Worker     Object o = new Object();
70*795d594fSAndroid Build Coastguard Worker     // We used to kill 'o' when the inline cache of 'doCheck' only
71*795d594fSAndroid Build Coastguard Worker     // contains 'Main' (which makes the only branch using 'o' dead).
72*795d594fSAndroid Build Coastguard Worker     // So the deoptimization at the inline cache was incorrectly assuming
73*795d594fSAndroid Build Coastguard Worker     // 'o' was dead.
74*795d594fSAndroid Build Coastguard Worker     // This lead to a NPE on the 'toString' call just after deoptimizing.
75*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < 5000; i++) {
76*795d594fSAndroid Build Coastguard Worker       if (m.doCheck()) {
77*795d594fSAndroid Build Coastguard Worker         // We make this branch the only true user of the 'o' phi. All other uses
78*795d594fSAndroid Build Coastguard Worker         // of 'o' are phi updates.
79*795d594fSAndroid Build Coastguard Worker         o.toString();
80*795d594fSAndroid Build Coastguard Worker       } else if (myIntStatic == 42) {
81*795d594fSAndroid Build Coastguard Worker         o = m;
82*795d594fSAndroid Build Coastguard Worker       }
83*795d594fSAndroid Build Coastguard Worker     }
84*795d594fSAndroid Build Coastguard Worker   }
85*795d594fSAndroid Build Coastguard Worker 
$noinline$loopIncrement(Main m)86*795d594fSAndroid Build Coastguard Worker   public static void $noinline$loopIncrement(Main m) {
87*795d594fSAndroid Build Coastguard Worker     int k = 0;
88*795d594fSAndroid Build Coastguard Worker     // We used to kill 'k' and replace it with 5000 when the inline cache
89*795d594fSAndroid Build Coastguard Worker     // of 'doCheck' only contains 'Main'.
90*795d594fSAndroid Build Coastguard Worker     // So the deoptimization at the inline cache was incorrectly assuming
91*795d594fSAndroid Build Coastguard Worker     // 'k' was 5000.
92*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < 5000; i++, k++) {
93*795d594fSAndroid Build Coastguard Worker       if (m.doCheck()) {
94*795d594fSAndroid Build Coastguard Worker         // We make this branch the only true user of the 'k' phi. All other uses
95*795d594fSAndroid Build Coastguard Worker         // of 'k' are phi updates.
96*795d594fSAndroid Build Coastguard Worker         myIntStatic = k;
97*795d594fSAndroid Build Coastguard Worker       }
98*795d594fSAndroid Build Coastguard Worker     }
99*795d594fSAndroid Build Coastguard Worker     if (k != 5000) {
100*795d594fSAndroid Build Coastguard Worker       throw new Error("Expected 5000, got " + k);
101*795d594fSAndroid Build Coastguard Worker     }
102*795d594fSAndroid Build Coastguard Worker   }
103*795d594fSAndroid Build Coastguard Worker 
$noinline$objectReturned(Main m)104*795d594fSAndroid Build Coastguard Worker   public static Object $noinline$objectReturned(Main m) {
105*795d594fSAndroid Build Coastguard Worker     Object o = new Object();
106*795d594fSAndroid Build Coastguard Worker     // We used to kill 'o' when the inline cache of 'doCheck' only
107*795d594fSAndroid Build Coastguard Worker     // contains 'Main' (which makes the only branch using 'o' dead).
108*795d594fSAndroid Build Coastguard Worker     // So the deoptimization at the inline cache was incorrectly assuming
109*795d594fSAndroid Build Coastguard Worker     // 'o' was dead.
110*795d594fSAndroid Build Coastguard Worker     // We also need to make 'o' escape through a return instruction, as mterp
111*795d594fSAndroid Build Coastguard Worker     // executes the same code for return and return-object, and the 0xebadde09
112*795d594fSAndroid Build Coastguard Worker     // sentinel for dead value is only pushed to non-object dex registers.
113*795d594fSAndroid Build Coastguard Worker     Object myReturnValue = null;
114*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < 5000; i++) {
115*795d594fSAndroid Build Coastguard Worker       if (m.doCheck()) {
116*795d594fSAndroid Build Coastguard Worker         // We make this branch the only true user of the 'o' phi. All other uses
117*795d594fSAndroid Build Coastguard Worker         // of 'o' are phi updates.
118*795d594fSAndroid Build Coastguard Worker         myReturnValue = o;
119*795d594fSAndroid Build Coastguard Worker       } else if (myIntStatic == 42) {
120*795d594fSAndroid Build Coastguard Worker         o = m;
121*795d594fSAndroid Build Coastguard Worker       }
122*795d594fSAndroid Build Coastguard Worker     }
123*795d594fSAndroid Build Coastguard Worker     return myReturnValue;
124*795d594fSAndroid Build Coastguard Worker   }
125*795d594fSAndroid Build Coastguard Worker 
126*795d594fSAndroid Build Coastguard Worker   public static int myIntStatic = 0;
127*795d594fSAndroid Build Coastguard Worker 
ensureJitCompiled(Class<?> itf, String name)128*795d594fSAndroid Build Coastguard Worker   public static native void ensureJitCompiled(Class<?> itf, String name);
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker class SubMain extends Main {
doCheck()132*795d594fSAndroid Build Coastguard Worker   public boolean doCheck() {
133*795d594fSAndroid Build Coastguard Worker     return true;
134*795d594fSAndroid Build Coastguard Worker   }
135*795d594fSAndroid Build Coastguard Worker }
136