summaryrefslogtreecommitdiff
path: root/test/527-checker-array-access-split/src/Main.java
blob: ead94464bf806bba34324149d5ba97b59210544d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {

  public static void assertIntEquals(int expected, int result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  /**
   * Test that HArrayGet with a constant index is not split.
   */

  /// CHECK-START-ARM64: int Main.constantIndexGet(int[]) instruction_simplifier_arm64 (before)
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:                                    ArrayGet [<<Array>>,<<Index>>]

  /// CHECK-START-ARM64: int Main.constantIndexGet(int[]) instruction_simplifier_arm64 (after)
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK-NOT:                                Arm64IntermediateAddress
  /// CHECK:                                    ArrayGet [<<Array>>,<<Index>>]

  public static int constantIndexGet(int array[]) {
    return array[1];
  }

  /**
   * Test that HArraySet with a constant index is not split.
   */

  /// CHECK-START-ARM64: void Main.constantIndexSet(int[]) instruction_simplifier_arm64 (before)
  /// CHECK:             <<Const2:i\d+>>        IntConstant 2
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:                                    ArraySet [<<Array>>,<<Index>>,<<Const2>>]

  /// CHECK-START-ARM64: void Main.constantIndexSet(int[]) instruction_simplifier_arm64 (after)
  /// CHECK:             <<Const2:i\d+>>        IntConstant 2
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK-NOT:                                Arm64IntermediateAddress
  /// CHECK:                                    ArraySet [<<Array>>,<<Index>>,<<Const2>>]


  public static void constantIndexSet(int array[]) {
    array[1] = 2;
  }

  /**
   * Test basic splitting of HArrayGet.
   */

  /// CHECK-START-ARM64: int Main.get(int[], int) instruction_simplifier_arm64 (before)
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:                                    ArrayGet [<<Array>>,<<Index>>]

  /// CHECK-START-ARM64: int Main.get(int[], int) instruction_simplifier_arm64 (after)
  /// CHECK:             <<DataOffset:i\d+>>    IntConstant
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:             <<Address:l\d+>>       Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-NEXT:                               ArrayGet [<<Address>>,<<Index>>]

  public static int get(int array[], int index) {
    return array[index];
  }

  /**
   * Test basic splitting of HArraySet.
   */

  /// CHECK-START-ARM64: void Main.set(int[], int, int) instruction_simplifier_arm64 (before)
  /// CHECK:                                    ParameterValue
  /// CHECK:                                    ParameterValue
  /// CHECK:             <<Arg:i\d+>>           ParameterValue
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:                                    ArraySet [<<Array>>,<<Index>>,<<Arg>>]

  /// CHECK-START-ARM64: void Main.set(int[], int, int) instruction_simplifier_arm64 (after)
  /// CHECK:                                    ParameterValue
  /// CHECK:                                    ParameterValue
  /// CHECK:             <<Arg:i\d+>>           ParameterValue
  /// CHECK:             <<DataOffset:i\d+>>    IntConstant
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:             <<Address:l\d+>>       Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-NEXT:                               ArraySet [<<Address>>,<<Index>>,<<Arg>>]

  public static void set(int array[], int index, int value) {
    array[index] = value;
  }

  /**
   * Check that the intermediate address can be shared after GVN.
   */

  /// CHECK-START-ARM64: void Main.getSet(int[], int) instruction_simplifier_arm64 (before)
  /// CHECK:             <<Const1:i\d+>>        IntConstant 1
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:             <<ArrayGet:i\d+>>      ArrayGet [<<Array>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK:                                    ArraySet [<<Array>>,<<Index>>,<<Add>>]

  /// CHECK-START-ARM64: void Main.getSet(int[], int) instruction_simplifier_arm64 (after)
  /// CHECK-DAG:         <<Const1:i\d+>>        IntConstant 1
  /// CHECK-DAG:         <<DataOffset:i\d+>>    IntConstant
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:             <<Address1:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-NEXT:        <<ArrayGet:i\d+>>      ArrayGet [<<Address1>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK:             <<Address2:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-NEXT:                               ArraySet [<<Address2>>,<<Index>>,<<Add>>]

  /// CHECK-START-ARM64: void Main.getSet(int[], int) GVN_after_arch (after)
  /// CHECK-DAG:         <<Const1:i\d+>>        IntConstant 1
  /// CHECK-DAG:         <<DataOffset:i\d+>>    IntConstant
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:             <<Address:l\d+>>       Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK:             <<ArrayGet:i\d+>>      ArrayGet [<<Address>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK-NOT:                                Arm64IntermediateAddress
  /// CHECK:                                    ArraySet [<<Address>>,<<Index>>,<<Add>>]

  public static void getSet(int array[], int index) {
    array[index] = array[index] + 1;
  }

  /**
   * Check that the intermediate address computation is not reordered or merged
   * across IRs that can trigger GC.
   */

  /// CHECK-START-ARM64: int[] Main.accrossGC(int[], int) instruction_simplifier_arm64 (before)
  /// CHECK:             <<Const1:i\d+>>        IntConstant 1
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:             <<ArrayGet:i\d+>>      ArrayGet [<<Array>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK:                                    NewArray
  /// CHECK:                                    ArraySet [<<Array>>,<<Index>>,<<Add>>]

  /// CHECK-START-ARM64: int[] Main.accrossGC(int[], int) instruction_simplifier_arm64 (after)
  /// CHECK-DAG:         <<Const1:i\d+>>        IntConstant 1
  /// CHECK-DAG:         <<DataOffset:i\d+>>    IntConstant
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:             <<Address1:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-NEXT:        <<ArrayGet:i\d+>>      ArrayGet [<<Address1>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK:                                    NewArray
  /// CHECK:             <<Address2:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-NEXT:                               ArraySet [<<Address2>>,<<Index>>,<<Add>>]

  /// CHECK-START-ARM64: int[] Main.accrossGC(int[], int) GVN_after_arch (after)
  /// CHECK-DAG:         <<Const1:i\d+>>        IntConstant 1
  /// CHECK-DAG:         <<DataOffset:i\d+>>    IntConstant
  /// CHECK:             <<Array:l\d+>>         NullCheck
  /// CHECK:             <<Index:i\d+>>         BoundsCheck
  /// CHECK:             <<Address1:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK:             <<ArrayGet:i\d+>>      ArrayGet [<<Address1>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK:                                    NewArray
  /// CHECK:             <<Address2:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK:                                    ArraySet [<<Address2>>,<<Index>>,<<Add>>]

  public static int[] accrossGC(int array[], int index) {
    int tmp = array[index] + 1;
    int[] new_array = new int[1];
    array[index] = tmp;
    return new_array;
  }

  /**
   * Test that the intermediate address is shared between array accesses after
   * the bounds check have been removed by BCE.
   */

  /// CHECK-START-ARM64: int Main.canMergeAfterBCE1() instruction_simplifier_arm64 (before)
  /// CHECK:             <<Const1:i\d+>>        IntConstant 1
  /// CHECK:             <<Array:l\d+>>         NewArray
  /// CHECK:             <<Index:i\d+>>         Phi
  /// CHECK:                                    If
  //  -------------- Loop
  /// CHECK:             <<ArrayGet:i\d+>>      ArrayGet [<<Array>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK:                                    ArraySet [<<Array>>,<<Index>>,<<Add>>]

  // By the time we reach the architecture-specific instruction simplifier, BCE
  // has removed the bounds checks in the loop.

  // Note that we do not care that the `DataOffset` is `12`. But if we do not
  // specify it and any other `IntConstant` appears before that instruction,
  // checker will match the previous `IntConstant`, and we will thus fail the
  // check.

  /// CHECK-START-ARM64: int Main.canMergeAfterBCE1() instruction_simplifier_arm64 (after)
  /// CHECK-DAG:         <<Const1:i\d+>>        IntConstant 1
  /// CHECK-DAG:         <<DataOffset:i\d+>>    IntConstant 12
  /// CHECK:             <<Array:l\d+>>         NewArray
  /// CHECK:             <<Index:i\d+>>         Phi
  /// CHECK:                                    If
  //  -------------- Loop
  /// CHECK:             <<Address1:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-NEXT:        <<ArrayGet:i\d+>>      ArrayGet [<<Address1>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK:             <<Address2:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-NEXT:                               ArraySet [<<Address2>>,<<Index>>,<<Add>>]

  /// CHECK-START-ARM64: int Main.canMergeAfterBCE1() GVN_after_arch (after)
  /// CHECK-DAG:         <<Const1:i\d+>>        IntConstant 1
  /// CHECK-DAG:         <<DataOffset:i\d+>>    IntConstant 12
  /// CHECK:             <<Array:l\d+>>         NewArray
  /// CHECK:             <<Index:i\d+>>         Phi
  /// CHECK:                                    If
  //  -------------- Loop
  /// CHECK:             <<Address:l\d+>>       Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK:             <<ArrayGet:i\d+>>      ArrayGet [<<Address>>,<<Index>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGet>>,<<Const1>>]
  /// CHECK-NOT:                                Arm64IntermediateAddress
  /// CHECK:                                    ArraySet [<<Address>>,<<Index>>,<<Add>>]

  public static int canMergeAfterBCE1() {
    int[] array = {0, 1, 2, 3};
    for (int i = 0; i < array.length; i++) {
      array[i] = array[i] + 1;
    }
    return array[array.length - 1];
  }

  /**
   * This test case is similar to `canMergeAfterBCE1`, but with different
   * indexes for the accesses.
   */

  /// CHECK-START-ARM64: int Main.canMergeAfterBCE2() instruction_simplifier_arm64 (before)
  /// CHECK:             <<Const1:i\d+>>        IntConstant 1
  /// CHECK:             <<Array:l\d+>>         NewArray
  /// CHECK:             <<Index:i\d+>>         Phi
  /// CHECK:                                    If
  //  -------------- Loop
  /// CHECK-DAG:         <<Index1:i\d+>>        Add [<<Index>>,<<Const1>>]
  /// CHECK-DAG:         <<ArrayGetI:i\d+>>     ArrayGet [<<Array>>,<<Index>>]
  /// CHECK-DAG:         <<ArrayGetI1:i\d+>>    ArrayGet [<<Array>>,<<Index1>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGetI>>,<<ArrayGetI1>>]
  /// CHECK:                                    ArraySet [<<Array>>,<<Index1>>,<<Add>>]

  // Note that we do not care that the `DataOffset` is `12`. But if we do not
  // specify it and any other `IntConstant` appears before that instruction,
  // checker will match the previous `IntConstant`, and we will thus fail the
  // check.

  /// CHECK-START-ARM64: int Main.canMergeAfterBCE2() instruction_simplifier_arm64 (after)
  /// CHECK-DAG:         <<Const1:i\d+>>        IntConstant 1
  /// CHECK-DAG:         <<DataOffset:i\d+>>    IntConstant 12
  /// CHECK:             <<Array:l\d+>>         NewArray
  /// CHECK:             <<Index:i\d+>>         Phi
  /// CHECK:                                    If
  //  -------------- Loop
  /// CHECK-DAG:         <<Index1:i\d+>>        Add [<<Index>>,<<Const1>>]
  /// CHECK-DAG:         <<Address1:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-DAG:         <<ArrayGetI:i\d+>>     ArrayGet [<<Address1>>,<<Index>>]
  /// CHECK-DAG:         <<Address2:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-DAG:         <<ArrayGetI1:i\d+>>    ArrayGet [<<Address2>>,<<Index1>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGetI>>,<<ArrayGetI1>>]
  /// CHECK:             <<Address3:l\d+>>      Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK:                                    ArraySet [<<Address3>>,<<Index1>>,<<Add>>]

  /// CHECK-START-ARM64: int Main.canMergeAfterBCE2() GVN_after_arch (after)
  /// CHECK-DAG:         <<Const1:i\d+>>        IntConstant 1
  /// CHECK-DAG:         <<DataOffset:i\d+>>    IntConstant 12
  /// CHECK:             <<Array:l\d+>>         NewArray
  /// CHECK:             <<Index:i\d+>>         Phi
  /// CHECK:                                    If
  //  -------------- Loop
  /// CHECK-DAG:         <<Index1:i\d+>>        Add [<<Index>>,<<Const1>>]
  /// CHECK-DAG:         <<Address:l\d+>>       Arm64IntermediateAddress [<<Array>>,<<DataOffset>>]
  /// CHECK-DAG:         <<ArrayGetI:i\d+>>     ArrayGet [<<Address>>,<<Index>>]
  /// CHECK-DAG:         <<ArrayGetI1:i\d+>>    ArrayGet [<<Address>>,<<Index1>>]
  /// CHECK:             <<Add:i\d+>>           Add [<<ArrayGetI>>,<<ArrayGetI1>>]
  /// CHECK:                                    ArraySet [<<Address>>,<<Index1>>,<<Add>>]

  // There should be only one intermediate address computation in the loop.

  /// CHECK-START-ARM64: int Main.canMergeAfterBCE2() GVN_after_arch (after)
  /// CHECK:                                    Arm64IntermediateAddress
  /// CHECK-NOT:                                Arm64IntermediateAddress

  public static int canMergeAfterBCE2() {
    int[] array = {0, 1, 2, 3};
    for (int i = 0; i < array.length - 1; i++) {
      array[i + 1] = array[i] + array[i + 1];
    }
    return array[array.length - 1];
  }


  public static void main(String[] args) {
    int[] array = {123, 456, 789};

    assertIntEquals(456, constantIndexGet(array));

    constantIndexSet(array);
    assertIntEquals(2, array[1]);

    assertIntEquals(789, get(array, 2));

    set(array, 1, 456);
    assertIntEquals(456, array[1]);

    getSet(array, 0);
    assertIntEquals(124, array[0]);

    accrossGC(array, 0);
    assertIntEquals(125, array[0]);

    assertIntEquals(4, canMergeAfterBCE1());
    assertIntEquals(6, canMergeAfterBCE2());
  }
}