1 /*
2  * Copyright (C) 2016 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 tests for BCE.
19  */
20 public class Main {
21 
22   static int[] array = new int[10];
23 
24   /// CHECK-START: int Main.doNotVisitAfterForwardBCE(int[]) BCE (before)
25   /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>> outer_loop:none
26   /// CHECK-DAG: BoundsCheck loop:<<Loop>>      outer_loop:none
27   //
28   /// CHECK-START: int Main.doNotVisitAfterForwardBCE(int[]) BCE (after)
29   /// CHECK-NOT: BoundsCheck
doNotVisitAfterForwardBCE(int[] a)30   static int doNotVisitAfterForwardBCE(int[] a) {
31     if (a == null) {
32       throw new Error("Null");
33     }
34     int k = 0;
35     int j = 0;
36     for (int i = 1; i < 10; i++) {
37       j = i - 1;
38       // b/32547652: after DCE, bounds checks become consecutive,
39       // and second should not be revisited after forward BCE.
40       k = a[i] + a[i - 1];
41     }
42     return j;
43   }
44 
$noinline$regressionTest123284765(String str)45   static public void $noinline$regressionTest123284765(String str) {
46     try {
47       int l = str.length();
48       if (l == 34) {
49         str.charAt(l);
50         fail();
51       }
52     } catch (StringIndexOutOfBoundsException expected) {
53       expectEquals(34, str.length());
54     }
55   }
56 
main(String[] args)57   public static void main(String[] args) {
58     expectEquals(8, doNotVisitAfterForwardBCE(array));
59     $noinline$regressionTest123284765("0123456789012345678901234567890123");
60     $noinline$regressionTest123284765("012345678901");
61     System.out.println("passed");
62   }
63 
expectEquals(int expected, int result)64   private static void expectEquals(int expected, int result) {
65     if (expected != result) {
66       throw new Error("Expected: " + expected + ", found: " + result);
67     }
68   }
69 
fail()70   private static void fail() {
71     throw new Error("FAIL");
72   }
73 }
74