1 /*
2  * Copyright (C) 2017 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  * Regression test on ARM-scheduling/array-aliasing bug (b/64018485).
19  */
20 public class Main {
21 
22   //
23   // Mimic original bug.
24   //
25 
setFields(int[] fields)26   static void setFields(int[] fields) {
27     if (fields == null || fields.length < 6)
28       fields = new int[6];  // creates phi
29     fields[5] = 127;
30   }
31 
processFieldValues(int field0, int field1, int field2, int field3, int field4, int field5)32   static void processFieldValues(int field0, int field1, int field2,
33                                  int field3, int field4, int field5) {
34     if (field5 != 127) {
35       throw new Error("field = " + field5);
36     } else if (field0 != 0) {
37       processFieldValues(0, 0, 0, 0, 0, 0);  // disable inlining
38     }
39   }
40 
doit(int pass)41   static int doit(int pass) {
42     int[] fields = new int[6];
43     for (; ; pass++) {
44       setFields(fields);
45       processFieldValues(fields[0], fields[1], fields[2],
46                          fields[3], fields[4], fields[5]);
47       if (pass == 0)
48         break;
49     }
50     return fields[5];
51   }
52 
53   //
54   // Similar situation.
55   //
56 
aliasing(boolean f)57   private static int aliasing(boolean f) {
58     int[] array = new int[6];
59     int[] array2 = null;
60     int s = 0;
61     for (int i = 0; i < 1; i++) {
62       if (f) {
63         array2 = array;
64       }
65       array2[1] = 4;
66       s = array[1];
67     }
68     return s;
69   }
70 
71   //
72   // Main driver.
73   //
74 
main(String[] args)75   static public void main(String[] args) {
76     int r = doit(0);
77     int s = aliasing(true);
78     System.out.println("passed " + r + " " + s);
79   }
80 }
81