1 /*
2  * Copyright (C) 2022 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 public class Main {
18 
19   static final int ITERATIONS = 16;
20 
21   // Test 1: This test checks whether the SuspendCheck is removed from the
22   // header.
23 
24   /// CHECK-START-ARM64: void Main.$noinline$testRemoveSuspendCheck(int[]) disassembly (after)
25   /// CHECK:        SuspendCheck         loop:<<LoopId:B\d+>>
26   /// CHECK-NEXT:   dex_pc:{{.*}}
27   /// CHECK:        Goto                 loop:<<LoopId>>
28   /// CHECK-NEXT:   b
29 
$noinline$testRemoveSuspendCheck(int[] a)30   public static void $noinline$testRemoveSuspendCheck(int[] a) {
31     for (int i = 0; i < ITERATIONS; i++) {
32       a[i++] = i;
33     }
34   }
35 
36   // Test 2: This test checks that the SuspendCheck is not removed from the
37   // header because it contains a call to another function.
38 
39   /// CHECK-START-ARM64: void Main.testRemoveSuspendCheckWithCall(int[]) disassembly (after)
40   /// CHECK:        SuspendCheck         loop:<<LoopId:B\d+>>
41   /// CHECK:        Goto                 loop:<<LoopId>>
42   /// CHECK-NEXT:   ldr
43 
testRemoveSuspendCheckWithCall(int[] a)44   public static void testRemoveSuspendCheckWithCall(int[] a) {
45     for (int i = 0; i < ITERATIONS; i++) {
46       a[i++] = i;
47       $noinline$testRemoveSuspendCheck(a);
48     }
49   }
50 
51   // Test 3:  This test checks that the SuspendCheck is not removed from the
52   // header because INSTR_COUNT * TRIP_COUNT exceeds the defined heuristic.
53 
54   /// CHECK-START-ARM64: void Main.testRemoveSuspendCheckAboveHeuristic(int[]) disassembly (after)
55   /// CHECK:        SuspendCheck         loop:<<LoopId:B\d+>>
56   /// CHECK:        Goto                 loop:<<LoopId>>
57   /// CHECK-NEXT:   ldr
58 
testRemoveSuspendCheckAboveHeuristic(int[] a)59   public static void testRemoveSuspendCheckAboveHeuristic(int[] a) {
60     for (int i = 0; i < ITERATIONS * 6; i++) {
61       a[i++] = i;
62     }
63   }
64 
65   // Test 4:  This test checks that the SuspendCheck is not removed from the
66   // header because the trip count is not known at compile time.
67 
68   /// CHECK-START-ARM64: void Main.testRemoveSuspendCheckUnknownCount(int[], int) disassembly (after)
69   /// CHECK:        SuspendCheck         loop:<<LoopId:B\d+>>
70   /// CHECK:        Goto                 loop:<<LoopId>>
71   /// CHECK-NEXT:   ldr
72 
testRemoveSuspendCheckUnknownCount(int[] a, int n)73   public static void testRemoveSuspendCheckUnknownCount(int[] a, int n) {
74     for (int i = 0; i < n; i++) {
75       a[i++] = i;
76     }
77   }
78 
main(String[] args)79   public static void main(String[] args) {
80     int[] a = new int[100];
81     $noinline$testRemoveSuspendCheck(a);
82     testRemoveSuspendCheckWithCall(a);
83     testRemoveSuspendCheckAboveHeuristic(a);
84     testRemoveSuspendCheckUnknownCount(a, 4);
85   }
86 }
87