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