1 /*
2  * Copyright (C) 2019 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  * Test corner cases for loop vectorizer.
19  */
20 public class Main {
21   /// CHECK-START: void Main.$noinline$testDivZeroCheck() loop_optimization (before)
22   /// CHECK: DivZeroCheck
23   /// CHECK-NOT: DivZeroCheck
24   /// CHECK-START: void Main.$noinline$testDivZeroCheck() loop_optimization (after)
25   /// CHECK: DivZeroCheck
$noinline$testDivZeroCheck()26   public static void $noinline$testDivZeroCheck() {
27     int[] a = new int[10];
28     for (int i = 0; i < a.length; ++i) {
29       int x = 42 / 0;  // unused but throwing
30       a[i] = 42;
31     }
32   }
33 
34   static class Base {}
35   static class Foo extends Base {}
36   static class Bar extends Base {}
37 
38   /// CHECK-START: void Main.$noinline$testCheckCast() loop_optimization (before)
39   /// CHECK: CheckCast
40   /// CHECK-NOT: CheckCast
41   /// CHECK-START: void Main.$noinline$testCheckCast() loop_optimization (after)
42   /// CHECK: CheckCast
$noinline$testCheckCast()43   public static void $noinline$testCheckCast() {
44     Base base = new Foo();
45     int[] a = new int[10];
46     for (int i = 0; i < a.length; ++i) {
47       Bar bar = (Bar) base;  // unused but throwing
48       a[i] = 42;
49     }
50   }
51 
52   /// CHECK-START: void Main.$noinline$testBoundsCheck() loop_optimization (before)
53   /// CHECK: BoundsCheck
54   /// CHECK-NOT: BoundsCheck
55   /// CHECK-START: void Main.$noinline$testBoundsCheck() loop_optimization (after)
56   /// CHECK: BoundsCheck
$noinline$testBoundsCheck()57   public static void $noinline$testBoundsCheck() {
58     int[] a = new int[10];
59     for (int i = 0; i < a.length; ++i) {
60       int x = a[11];  // unused but throwing
61       a[i] = 42;
62     }
63   }
64 
main(String[] args)65   public static void main(String[] args) {
66     // We must not optimize any of the exceptions away.
67     try {
68       $noinline$testDivZeroCheck();
69     } catch (java.lang.ArithmeticException e) {
70       System.out.println("DivZeroCheck");
71     }
72     try {
73       $noinline$testCheckCast();
74     } catch (java.lang.ClassCastException e) {
75       System.out.println("CheckCast");
76     }
77     try {
78       $noinline$testBoundsCheck();
79     } catch (java.lang.ArrayIndexOutOfBoundsException e) {
80       System.out.println("BoundsCheck");
81     }
82   }
83 }
84