xref: /aosp_15_r20/art/test/2233-checker-remove-loop-suspend-check/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker public class Main {
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker   static final int ITERATIONS = 16;
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker   // Test 1: This test checks whether the SuspendCheck is removed from the
22*795d594fSAndroid Build Coastguard Worker   // header.
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker   /// CHECK-START-ARM64: void Main.$noinline$testRemoveSuspendCheck(int[]) disassembly (after)
25*795d594fSAndroid Build Coastguard Worker   /// CHECK:        SuspendCheck         loop:<<LoopId:B\d+>>
26*795d594fSAndroid Build Coastguard Worker   /// CHECK-NEXT:   dex_pc:{{.*}}
27*795d594fSAndroid Build Coastguard Worker   /// CHECK:        Goto                 loop:<<LoopId>>
28*795d594fSAndroid Build Coastguard Worker   /// CHECK-NEXT:   b
29*795d594fSAndroid Build Coastguard Worker 
$noinline$testRemoveSuspendCheck(int[] a)30*795d594fSAndroid Build Coastguard Worker   public static void $noinline$testRemoveSuspendCheck(int[] a) {
31*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < ITERATIONS; i++) {
32*795d594fSAndroid Build Coastguard Worker       a[i++] = i;
33*795d594fSAndroid Build Coastguard Worker     }
34*795d594fSAndroid Build Coastguard Worker   }
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker   // Test 2: This test checks that the SuspendCheck is not removed from the
37*795d594fSAndroid Build Coastguard Worker   // header because it contains a call to another function.
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker   /// CHECK-START-ARM64: void Main.testRemoveSuspendCheckWithCall(int[]) disassembly (after)
40*795d594fSAndroid Build Coastguard Worker   /// CHECK:        SuspendCheck         loop:<<LoopId:B\d+>>
41*795d594fSAndroid Build Coastguard Worker   /// CHECK:        Goto                 loop:<<LoopId>>
42*795d594fSAndroid Build Coastguard Worker   /// CHECK-NEXT:   ldr
43*795d594fSAndroid Build Coastguard Worker 
testRemoveSuspendCheckWithCall(int[] a)44*795d594fSAndroid Build Coastguard Worker   public static void testRemoveSuspendCheckWithCall(int[] a) {
45*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < ITERATIONS; i++) {
46*795d594fSAndroid Build Coastguard Worker       a[i++] = i;
47*795d594fSAndroid Build Coastguard Worker       $noinline$testRemoveSuspendCheck(a);
48*795d594fSAndroid Build Coastguard Worker     }
49*795d594fSAndroid Build Coastguard Worker   }
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker   // Test 3:  This test checks that the SuspendCheck is not removed from the
52*795d594fSAndroid Build Coastguard Worker   // header because INSTR_COUNT * TRIP_COUNT exceeds the defined heuristic.
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   /// CHECK-START-ARM64: void Main.testRemoveSuspendCheckAboveHeuristic(int[]) disassembly (after)
55*795d594fSAndroid Build Coastguard Worker   /// CHECK:        SuspendCheck         loop:<<LoopId:B\d+>>
56*795d594fSAndroid Build Coastguard Worker   /// CHECK:        Goto                 loop:<<LoopId>>
57*795d594fSAndroid Build Coastguard Worker   /// CHECK-NEXT:   ldr
58*795d594fSAndroid Build Coastguard Worker 
testRemoveSuspendCheckAboveHeuristic(int[] a)59*795d594fSAndroid Build Coastguard Worker   public static void testRemoveSuspendCheckAboveHeuristic(int[] a) {
60*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < ITERATIONS * 6; i++) {
61*795d594fSAndroid Build Coastguard Worker       a[i++] = i;
62*795d594fSAndroid Build Coastguard Worker     }
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker   // Test 4:  This test checks that the SuspendCheck is not removed from the
66*795d594fSAndroid Build Coastguard Worker   // header because the trip count is not known at compile time.
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker   /// CHECK-START-ARM64: void Main.testRemoveSuspendCheckUnknownCount(int[], int) disassembly (after)
69*795d594fSAndroid Build Coastguard Worker   /// CHECK:        SuspendCheck         loop:<<LoopId:B\d+>>
70*795d594fSAndroid Build Coastguard Worker   /// CHECK:        Goto                 loop:<<LoopId>>
71*795d594fSAndroid Build Coastguard Worker   /// CHECK-NEXT:   ldr
72*795d594fSAndroid Build Coastguard Worker 
testRemoveSuspendCheckUnknownCount(int[] a, int n)73*795d594fSAndroid Build Coastguard Worker   public static void testRemoveSuspendCheckUnknownCount(int[] a, int n) {
74*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < n; i++) {
75*795d594fSAndroid Build Coastguard Worker       a[i++] = i;
76*795d594fSAndroid Build Coastguard Worker     }
77*795d594fSAndroid Build Coastguard Worker   }
78*795d594fSAndroid Build Coastguard Worker 
main(String[] args)79*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
80*795d594fSAndroid Build Coastguard Worker     int[] a = new int[100];
81*795d594fSAndroid Build Coastguard Worker     $noinline$testRemoveSuspendCheck(a);
82*795d594fSAndroid Build Coastguard Worker     testRemoveSuspendCheckWithCall(a);
83*795d594fSAndroid Build Coastguard Worker     testRemoveSuspendCheckAboveHeuristic(a);
84*795d594fSAndroid Build Coastguard Worker     testRemoveSuspendCheckUnknownCount(a, 4);
85*795d594fSAndroid Build Coastguard Worker   }
86*795d594fSAndroid Build Coastguard Worker }
87