blob: 209786a113278eba0401058344ef313060299f84 [file] [log] [blame]
Aart Bik67def592016-07-14 17:19:43 -07001/*
2 * Copyright (C) 2016 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// Test on loop optimizations, in particular dynamic BCE. In all cases,
19// bounds check on a[] is resolved statically. Bounds checks on b[]
20// exercise various different scenarios. In all cases, loop-based
21// dynamic BCE is better than the dominator-based BCE, since it
22// generates the test outside the loop.
23//
24public class Main {
25
26 /// CHECK-START: void Main.oneConstantIndex(int[], int[]) BCE (before)
27 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
28 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
29 //
30 /// CHECK-START: void Main.oneConstantIndex(int[], int[]) BCE (after)
31 /// CHECK-DAG: Deoptimize loop:none
32 /// CHECK-DAG: Deoptimize loop:none
33 /// CHECK-NOT: Deoptimize
34 //
35 /// CHECK-START: void Main.oneConstantIndex(int[], int[]) BCE (after)
36 /// CHECK-NOT: BoundsCheck
37 public static void oneConstantIndex(int[] a, int[] b) {
38 // Dynamic bce on b requires two deopts: one null and one bound.
39 for (int i = 0; i < a.length; i++) {
40 a[i] = b[1];
41 }
42 }
43
44 /// CHECK-START: void Main.multipleConstantIndices(int[], int[]) BCE (before)
45 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
46 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
47 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
48 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
49 //
50 /// CHECK-START: void Main.multipleConstantIndices(int[], int[]) BCE (after)
51 /// CHECK-DAG: Deoptimize loop:none
52 /// CHECK-DAG: Deoptimize loop:none
53 /// CHECK-NOT: Deoptimize
54 //
55 /// CHECK-START: void Main.multipleConstantIndices(int[], int[]) BCE (after)
56 /// CHECK-NOT: BoundsCheck
57 public static void multipleConstantIndices(int[] a, int[] b) {
58 // Dynamic bce on b requires two deopts: one null and one bound.
59 for (int i = 0; i < a.length; i++) {
60 a[i] = b[0] + b[1] + b[2];
61 }
62 }
63
64 /// CHECK-START: void Main.oneInvariantIndex(int[], int[], int) BCE (before)
65 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
66 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
67 //
68 /// CHECK-START: void Main.oneInvariantIndex(int[], int[], int) BCE (after)
69 /// CHECK-DAG: Deoptimize loop:none
70 /// CHECK-DAG: Deoptimize loop:none
71 /// CHECK-NOT: Deoptimize
72 //
73 /// CHECK-START: void Main.oneInvariantIndex(int[], int[], int) BCE (after)
74 /// CHECK-NOT: BoundsCheck
75 public static void oneInvariantIndex(int[] a, int[] b, int c) {
76 // Dynamic bce on b requires two deopts: one null and one bound.
77 for (int i = 0; i < a.length; i++) {
78 a[i] = b[c];
79 }
80 }
81
82 /// CHECK-START: void Main.multipleInvariantIndices(int[], int[], int) BCE (before)
83 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
84 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
85 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
86 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
87 //
88 /// CHECK-START: void Main.multipleInvariantIndices(int[], int[], int) BCE (after)
89 /// CHECK-DAG: Deoptimize loop:none
90 /// CHECK-DAG: Deoptimize loop:none
91 /// CHECK-DAG: Deoptimize loop:none
92 /// CHECK-NOT: Deoptimize
93 //
94 /// CHECK-START: void Main.multipleInvariantIndices(int[], int[], int) BCE (after)
95 /// CHECK-NOT: BoundsCheck
96 public static void multipleInvariantIndices(int[] a, int[] b, int c) {
97 // Dynamic bce on b requires three deopts: one null and two bounds.
98 for (int i = 0; i < a.length; i++) {
99 a[i] = b[c-1] + b[c] + b[c+1];
100 }
101 }
102
103 /// CHECK-START: void Main.oneUnitStride(int[], int[]) BCE (before)
104 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
105 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
106 //
107 /// CHECK-START: void Main.oneUnitStride(int[], int[]) BCE (after)
108 /// CHECK-DAG: Deoptimize loop:none
109 /// CHECK-DAG: Deoptimize loop:none
110 /// CHECK-DAG: Deoptimize loop:none
111 /// CHECK-NOT: Deoptimize
112 //
113 /// CHECK-START: void Main.oneUnitStride(int[], int[]) BCE (after)
114 /// CHECK-NOT: BoundsCheck
115 public static void oneUnitStride(int[] a, int[] b) {
116 // Dynamic bce on b requires three deopts: one null and two bounds.
117 for (int i = 0; i < a.length; i++) {
118 a[i] = b[i];
119 }
120 }
121
122 /// CHECK-START: void Main.multipleUnitStrides(int[], int[]) BCE (before)
123 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
124 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
125 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
126 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
127 //
128 /// CHECK-START: void Main.multipleUnitStrides(int[], int[]) BCE (after)
129 /// CHECK-DAG: Deoptimize loop:none
130 /// CHECK-DAG: Deoptimize loop:none
131 /// CHECK-DAG: Deoptimize loop:none
132 /// CHECK-DAG: Deoptimize loop:none
133 /// CHECK-NOT: Deoptimize
134 //
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700135 /// CHECK-START: void Main.multipleUnitStrides(int[], int[]) instruction_simplifier$after_bce (after)
Aart Bik67def592016-07-14 17:19:43 -0700136 /// CHECK-DAG: Deoptimize loop:none
137 /// CHECK-DAG: Deoptimize loop:none
138 /// CHECK-DAG: Deoptimize loop:none
139 /// CHECK-NOT: Deoptimize
140 //
141 /// CHECK-START: void Main.multipleUnitStrides(int[], int[]) BCE (after)
142 /// CHECK-NOT: BoundsCheck
143 public static void multipleUnitStrides(int[] a, int[] b) {
144 // Dynamic bce on b requires four deopts: one null and three bounds.
145 // One redundant deopt is removed by simplifier.
146 // TODO: range information could remove another
147 for (int i = 1; i < a.length - 1; i++) {
148 a[i] = b[i-1] + b[i] + b[i+1];
149 }
150 }
151
152 /// CHECK-START: void Main.multipleUnitStridesConditional(int[], int[]) BCE (before)
153 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
154 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
155 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
156 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
157 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
158 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
159 //
160 /// CHECK-START: void Main.multipleUnitStridesConditional(int[], int[]) BCE (after)
161 /// CHECK-DAG: Deoptimize loop:none
162 /// CHECK-DAG: Deoptimize loop:none
163 /// CHECK-DAG: Deoptimize loop:none
164 /// CHECK-DAG: Deoptimize loop:none
165 /// CHECK-NOT: Deoptimize
166 //
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700167 /// CHECK-START: void Main.multipleUnitStridesConditional(int[], int[]) instruction_simplifier$after_bce (after)
Aart Bik67def592016-07-14 17:19:43 -0700168 /// CHECK-DAG: Deoptimize loop:none
169 /// CHECK-DAG: Deoptimize loop:none
170 /// CHECK-DAG: Deoptimize loop:none
171 /// CHECK-NOT: Deoptimize
172 //
173 /// CHECK-START: void Main.multipleUnitStridesConditional(int[], int[]) BCE (after)
174 /// CHECK-NOT: BoundsCheck
175 public static void multipleUnitStridesConditional(int[] a, int[] b) {
176 // Dynamic bce on b requires four deopts: one null and three bounds.
177 // The two conditional references may be included, since they are in range.
178 // One redundant deopt is removed by simplifier.
179 for (int i = 2; i < a.length - 2; i++) {
180 int t = b[i-2] + b[i] + b[i+2] + (((i & 1) == 0) ? b[i+1] : b[i-1]);
181 a[i] = t;
182 }
183 }
184
185 /// CHECK-START: void Main.shifter(int[]) BCE (before)
186 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
187 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
188 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
189 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
190 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
191 //
192 /// CHECK-START: void Main.shifter(int[]) BCE (after)
193 /// CHECK-DAG: Deoptimize loop:none
194 /// CHECK-DAG: Deoptimize loop:none
195 /// CHECK-DAG: Deoptimize loop:none
196 /// CHECK-DAG: Deoptimize loop:none
197 /// CHECK-NOT: Deoptimize
198 //
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700199 /// CHECK-START: void Main.shifter(int[]) instruction_simplifier$after_bce (after)
Aart Bik67def592016-07-14 17:19:43 -0700200 /// CHECK-DAG: Deoptimize loop:none
201 /// CHECK-DAG: Deoptimize loop:none
202 /// CHECK-NOT: Deoptimize
203 //
204 /// CHECK-START: void Main.shifter(int[]) BCE (after)
205 /// CHECK-NOT: BoundsCheck
206 public static void shifter(int[] x) {
207 // Real-life example: should have four deopts: one null and three bounds.
208 // Two redundant deopts are removed by simplifier.
209 for (int i = 16; i < 80; i++) {
210 int t = x[i - 3] ^ x[i - 8] ^ x[i - 14] ^ x[i - 16];
211 x[i] = t << 1 | t >>> 31;
212 }
213 }
214
215 /// CHECK-START: void Main.stencil(int[], int, int) BCE (before)
216 /// CHECK-DAG: BoundsCheck loop:<<Loop:B\d+>>
217 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
218 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
219 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
220 /// CHECK-DAG: BoundsCheck loop:<<Loop>>
221 //
222 /// CHECK-START: void Main.stencil(int[], int, int) BCE (after)
223 /// CHECK-DAG: Deoptimize loop:none
224 /// CHECK-DAG: Deoptimize loop:none
225 /// CHECK-DAG: Deoptimize loop:none
226 /// CHECK-DAG: Deoptimize loop:none
227 /// CHECK-NOT: Deoptimize
228 //
229 /// CHECK-START: void Main.stencil(int[], int, int) BCE (after)
230 /// CHECK-NOT: BoundsCheck
231 public static void stencil(int[] array, int start, int end) {
232 // Real-life example: should have four deopts: one null and three bounds.
233 for (int i = end; i >= start; i--) {
234 array[i] = (array[i-2] + array[i-1] + array[i] + array[i+1] + array[i+2]) / 5;
235 }
236 }
237
238 //
239 // Verifier.
240 //
241
242 public static void main(String[] args) {
243 int[] a = new int[10];
244 int b[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
245 int b1[] = { 100 };
246
247 oneConstantIndex(a, b);
248 for (int i = 0; i < a.length; i++) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800249 expectEquals(2, a[i]);
Aart Bik67def592016-07-14 17:19:43 -0700250 }
251 try {
252 oneConstantIndex(a, b1);
253 throw new Error("Should throw AIOOBE");
254 } catch (ArrayIndexOutOfBoundsException e) {
255 }
256
257 multipleConstantIndices(a, b);
258 for (int i = 0; i < a.length; i++) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800259 expectEquals(6, a[i]);
Aart Bik67def592016-07-14 17:19:43 -0700260 }
261 try {
262 multipleConstantIndices(a, b1);
263 throw new Error("Should throw AIOOBE");
264 } catch (ArrayIndexOutOfBoundsException e) {
265 }
266
267 oneInvariantIndex(a, b, 1);
268 for (int i = 0; i < a.length; i++) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800269 expectEquals(2, a[i]);
Aart Bik67def592016-07-14 17:19:43 -0700270 }
271 try {
272 oneInvariantIndex(a, b1, 1);
273 throw new Error("Should throw AIOOBE");
274 } catch (ArrayIndexOutOfBoundsException e) {
275 }
276
277 multipleInvariantIndices(a, b, 1);
278 for (int i = 0; i < a.length; i++) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800279 expectEquals(6, a[i]);
Aart Bik67def592016-07-14 17:19:43 -0700280 }
281 try {
282 multipleInvariantIndices(a, b1, 1);
283 throw new Error("Should throw AIOOBE");
284 } catch (ArrayIndexOutOfBoundsException e) {
285 }
286
287 oneUnitStride(a, b);
288 for (int i = 0; i < a.length; i++) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800289 expectEquals(i + 1, a[i]);
Aart Bik67def592016-07-14 17:19:43 -0700290 }
291 try {
292 oneUnitStride(a, b1);
293 throw new Error("Should throw AIOOBE");
294 } catch (ArrayIndexOutOfBoundsException e) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800295 expectEquals(100, a[0]);
Aart Bik67def592016-07-14 17:19:43 -0700296 }
297
298 multipleUnitStrides(a, b);
299 for (int i = 1; i < a.length - 1; i++) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800300 expectEquals(3 * i + 3, a[i]);
Aart Bik67def592016-07-14 17:19:43 -0700301 }
302 try {
303 multipleUnitStrides(a, b1);
304 throw new Error("Should throw AIOOBE");
305 } catch (ArrayIndexOutOfBoundsException e) {
306 }
307
308 multipleUnitStridesConditional(a, b);
309 for (int i = 2; i < a.length - 2; i++) {
310 int e = 3 * i + 3 + (((i & 1) == 0) ? i + 2 : i);
Mathieu Chartier6beced42016-11-15 15:51:31 -0800311 expectEquals(e, a[i]);
Aart Bik67def592016-07-14 17:19:43 -0700312 }
313 try {
314 multipleUnitStridesConditional(a, b1);
315 throw new Error("Should throw AIOOBE");
316 } catch (ArrayIndexOutOfBoundsException e) {
317 }
318
319 System.out.println("passed");
320 }
321
322 private static void expectEquals(int expected, int result) {
323 if (expected != result) {
324 throw new Error("Expected: " + expected + ", found: " + result);
325 }
326 }
327}