xref: /aosp_15_r20/art/test/2279-second-inner-loop-references-first/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2024 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 class Main {
main(String[] h)18     public static void main(String[] h) {
19         assertIntEquals(50, $noinline$SecondInnerLoopReferencesFirst());
20     }
21 
$noinline$SecondInnerLoopReferencesFirst()22     static int $noinline$SecondInnerLoopReferencesFirst() {
23         int f = 0;
24         for (int outer = 0; outer < 5; outer++) {
25             // This will create a Phi[const_3, const_1] that we eliminate in loop optimization
26             byte ab = 3;
27             // This loop will be eliminated but the next loop will be referencing this one as the
28             // `ab` is used in the addition below, which used to lead to a crash. The reason for the
29             // crash is that we are not updating the induction variables correctly and they are
30             // pointing to a deleted instruction.
31             for (int first_inner = 0; first_inner < 5; first_inner++) {
32                 ab = 1;
33             }
34             byte i = 0;
35             for (int second_inner = 0; second_inner < 10; second_inner++) {
36                 i += ab;
37             }
38             f += (int) i;
39         }
40         return f;
41     }
42 
assertIntEquals(int expected, int result)43     public static void assertIntEquals(int expected, int result) {
44         if (expected != result) {
45             throw new Error("Expected: " + expected + ", found: " + result);
46         }
47     }
48 }
49