xref: /aosp_15_r20/art/test/499-bce-phi-array-length/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 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 {
foo(int start, int[] array)18*795d594fSAndroid Build Coastguard Worker     public static int foo(int start, int[] array) {
19*795d594fSAndroid Build Coastguard Worker         int result = 0;
20*795d594fSAndroid Build Coastguard Worker         // We will create HDeoptimize nodes for this first loop, and a phi
21*795d594fSAndroid Build Coastguard Worker         // for the array length which will only be used within the loop.
22*795d594fSAndroid Build Coastguard Worker         for (int i = start; i < 3; i++) {
23*795d594fSAndroid Build Coastguard Worker             result += array[i];
24*795d594fSAndroid Build Coastguard Worker             for (int j = 0; j < 2; ++j) {
25*795d594fSAndroid Build Coastguard Worker                 // The HBoundsCheck for this array access will be updated to access
26*795d594fSAndroid Build Coastguard Worker                 // the array length phi created for the deoptimization checks of the
27*795d594fSAndroid Build Coastguard Worker                 // first loop. This crashed the compiler which used to DCHECK an array
28*795d594fSAndroid Build Coastguard Worker                 // length in a bounds check cannot be a phi.
29*795d594fSAndroid Build Coastguard Worker                 result += array[j];
30*795d594fSAndroid Build Coastguard Worker             }
31*795d594fSAndroid Build Coastguard Worker         }
32*795d594fSAndroid Build Coastguard Worker         return result;
33*795d594fSAndroid Build Coastguard Worker     }
34*795d594fSAndroid Build Coastguard Worker 
bar(int start, int[] array)35*795d594fSAndroid Build Coastguard Worker     public static int bar(int start, int[] array) {
36*795d594fSAndroid Build Coastguard Worker         int result = 0;
37*795d594fSAndroid Build Coastguard Worker         for (int i = start; i < 3; i++) {
38*795d594fSAndroid Build Coastguard Worker             result += array[i];
39*795d594fSAndroid Build Coastguard Worker             for (int j = 0; j < 2; ++j) {
40*795d594fSAndroid Build Coastguard Worker                 result += array[j];
41*795d594fSAndroid Build Coastguard Worker                 // The following operations would lead to BCE wanting to add another
42*795d594fSAndroid Build Coastguard Worker                 // deoptimization, but it crashed assuming the input of a `HBoundsCheck`
43*795d594fSAndroid Build Coastguard Worker                 // must be a `HArrayLength`.
44*795d594fSAndroid Build Coastguard Worker                 result += array[0];
45*795d594fSAndroid Build Coastguard Worker                 result += array[1];
46*795d594fSAndroid Build Coastguard Worker                 result += array[2];
47*795d594fSAndroid Build Coastguard Worker             }
48*795d594fSAndroid Build Coastguard Worker         }
49*795d594fSAndroid Build Coastguard Worker         return result;
50*795d594fSAndroid Build Coastguard Worker     }
51*795d594fSAndroid Build Coastguard Worker 
main(String[] args)52*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) {
53*795d594fSAndroid Build Coastguard Worker         int[] a = new int[] { 1, 2, 3, 4, 5 };
54*795d594fSAndroid Build Coastguard Worker         int result = foo(1, a);
55*795d594fSAndroid Build Coastguard Worker         if (result != 11) {
56*795d594fSAndroid Build Coastguard Worker             throw new Error("Got " + result + ", expected " + 11);
57*795d594fSAndroid Build Coastguard Worker         }
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker         result = bar(1, a);
60*795d594fSAndroid Build Coastguard Worker         if (result != 35) {
61*795d594fSAndroid Build Coastguard Worker             throw new Error("Got " + result + ", expected " + 35);
62*795d594fSAndroid Build Coastguard Worker         }
63*795d594fSAndroid Build Coastguard Worker     }
64*795d594fSAndroid Build Coastguard Worker }
65