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/**
18 * Regression test on duplicate removal of same bounds check.
19 */
20public class Main {
21
22  /// CHECK-START: void Main.doit1(int[]) BCE (before)
23  /// CHECK-DAG: BoundsCheck
24  /// CHECK-DAG: BoundsCheck
25  /// CHECK-DAG: BoundsCheck
26  /// CHECK-DAG: BoundsCheck
27  //
28  /// CHECK-START: void Main.doit1(int[]) BCE (after)
29  /// CHECK-DAG: BoundsCheck
30  /// CHECK-DAG: BoundsCheck
31  /// CHECK-DAG: BoundsCheck
32  /// CHECK-DAG: BoundsCheck
33  //
34  /// CHECK-START: void Main.doit1(int[]) BCE (after)
35  /// CHECK-NOT: Deoptimize
36  public static void doit1(int[] a) {
37    a[a.length-3] = 1;
38    a[a.length-2] = 2;
39    a[a.length-1] = 3;
40    // This introduces a problematic BoundsCheck(x,x) node
41    // (1) certain OOB, so should be rejected
42    // (2) exposed bug in removing same BC twice if (1) would not be done.
43    a[a.length-0] = 4;
44  }
45
46  /// CHECK-START: void Main.doit2(int[]) BCE (before)
47  /// CHECK-DAG: BoundsCheck
48  /// CHECK-DAG: BoundsCheck
49  /// CHECK-DAG: BoundsCheck
50  /// CHECK-DAG: BoundsCheck
51  //
52  /// CHECK-START: void Main.doit2(int[]) BCE (after)
53  /// CHECK-DAG: Deoptimize
54  /// CHECK-DAG: Deoptimize
55  //
56  /// CHECK-START: void Main.doit2(int[]) BCE (after)
57  /// CHECK-NOT: BoundsCheck
58  public static void doit2(int[] a) {
59    a[a.length-4] = -101;
60    a[a.length-3] = -102;
61    a[a.length-2] = -103;
62    a[a.length-1] = -104;
63  }
64
65  public static void main(String[] args) {
66    int[] a = new int[4];
67
68    int fail = 0;
69    try {
70      doit1(a);
71    } catch (ArrayIndexOutOfBoundsException e) {
72      fail++;
73    }
74    expectEquals(1, fail);
75    expectEquals(0, a[0]);
76    expectEquals(1, a[1]);
77    expectEquals(2, a[2]);
78    expectEquals(3, a[3]);
79
80    try {
81      doit2(a);
82    } catch (ArrayIndexOutOfBoundsException e) {
83      fail++;
84    }
85    expectEquals(1, fail);
86    expectEquals(-101, a[0]);
87    expectEquals(-102, a[1]);
88    expectEquals(-103, a[2]);
89    expectEquals(-104, a[3]);
90
91    System.out.println("passed");
92  }
93
94  private static void expectEquals(int expected, int result) {
95    if (expected != result) {
96      throw new Error("Expected: " + expected + ", found: " + result);
97    }
98  }
99}
100