1 /*
2  * Copyright (C) 2023 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 {
main(String[] args)18     public static void main(String[] args) throws Exception {
19         try {
20             $noinline$testInfiniteLoopUnlessItThrows();
21             throw new Exception("Unreachable");
22         } catch (Error expected) {
23         }
24     }
25 
26     /// CHECK-START: int Main.$noinline$testInfiniteLoopUnlessItThrows() code_sinking (before)
27     /// CHECK-NOT: Add loop:none
28 
29     /// CHECK-START: int Main.$noinline$testInfiniteLoopUnlessItThrows() code_sinking (before)
30     /// CHECK-DAG:   <<Const0:i\d+>> IntConstant 0
31     /// CHECK-DAG:   <<Add:i\d+>> Add loop:<<Loop:B\d+>> outer_loop:none
32     /// CHECK-DAG:   Phi [<<Const0>>,<<Add>>] loop:<<Loop>> outer_loop:none
33 
34     /// CHECK-START: int Main.$noinline$testInfiniteLoopUnlessItThrows() code_sinking (after)
35     /// CHECK-NOT: Add loop:none
36 
37     /// CHECK-START: int Main.$noinline$testInfiniteLoopUnlessItThrows() code_sinking (after)
38     /// CHECK-DAG:   <<Const0:i\d+>> IntConstant 0
39     /// CHECK-DAG:   <<Add:i\d+>> Add loop:<<Loop:B\d+>> outer_loop:none
40     /// CHECK-DAG:   Phi [<<Const0>>,<<Add>>] loop:<<Loop>> outer_loop:none
$noinline$testInfiniteLoopUnlessItThrows()41     private static int $noinline$testInfiniteLoopUnlessItThrows() {
42         int a = 0;
43         while (true) {
44             try {
45                 $noinline$throwOrReturn(a);
46                 throw new Exception();
47             } catch (Exception e) {
48                 a++;
49             }
50         }
51     }
52 
53     // Throws Error if `input` is 10. Otherwise it returns `input`.
$noinline$throwOrReturn(int input)54     private static int $noinline$throwOrReturn(int input) throws Error {
55         if (input == 10) {
56             throw new Error();
57         }
58         return input;
59     }
60 }
61