Debian Codegraph @

test/567-checker-builder-intrinsics/src/TestMinMax.java

raw
1 /*
2 * Copyright (C) 2018 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 * Functional tests for detecting min/max.
19 */
20 public class TestMinMax {
21
22 //
23 // Direct intrinsics.
24 //
25
26 /// CHECK-START: int TestMinMax.minI(int) builder (after)
27 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
28 /// CHECK-DAG: <<Con:i\d+>> IntConstant 20
29 /// CHECK-DAG: <<Min:i\d+>> Min [<<Par>>,<<Con>>]
30 /// CHECK-DAG: Return [<<Min>>]
31 //
32 /// CHECK-START: int TestMinMax.minI(int) builder (after)
33 /// CHECK-NOT: InvokeStaticOrDirect
34 //
35 /// CHECK-START-ARM64: int TestMinMax.minI(int) disassembly (after)
36 /// CHECK-NOT: mov {{w\d+}}, #0x14
37 /// CHECK: cmp {{w\d+}}, #0x14
38 // Check that the constant generation was handled by VIXL.
39 /// CHECK: mov w16, #0x14
40 /// CHECK: csel {{w\d+}}, {{w\d+}}, w16, lt
41 public static int minI(int a) {
42 return Math.min(a, 20);
43 }
44
45 /// CHECK-START: long TestMinMax.minL(long) builder (after)
46 /// CHECK-DAG: <<Par:j\d+>> ParameterValue
47 /// CHECK-DAG: <<Con:j\d+>> LongConstant 20
48 /// CHECK-DAG: <<Min:j\d+>> Min [<<Par>>,<<Con>>]
49 /// CHECK-DAG: Return [<<Min>>]
50 //
51 /// CHECK-START: long TestMinMax.minL(long) builder (after)
52 /// CHECK-NOT: InvokeStaticOrDirect
53 //
54 /// CHECK-START-ARM64: long TestMinMax.minL(long) disassembly (after)
55 /// CHECK-NOT: mov {{x\d+}}, #0x14
56 /// CHECK: cmp {{x\d+}}, #0x14
57 // Check that the constant generation was handled by VIXL.
58 /// CHECK: mov x16, #0x14
59 /// CHECK: csel {{x\d+}}, {{x\d+}}, x16, lt
60 public static long minL(long a) {
61 return Math.min(a, 20L);
62 }
63
64 /// CHECK-START: int TestMinMax.maxI(int) builder (after)
65 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
66 /// CHECK-DAG: <<Con:i\d+>> IntConstant 20
67 /// CHECK-DAG: <<Max:i\d+>> Max [<<Par>>,<<Con>>]
68 /// CHECK-DAG: Return [<<Max>>]
69 //
70 /// CHECK-START: int TestMinMax.maxI(int) builder (after)
71 /// CHECK-NOT: InvokeStaticOrDirect
72 //
73 /// CHECK-START-ARM64: int TestMinMax.maxI(int) disassembly (after)
74 /// CHECK-NOT: mov {{w\d+}}, #0x14
75 /// CHECK: cmp {{w\d+}}, #0x14
76 // Check that the constant generation was handled by VIXL.
77 /// CHECK: mov w16, #0x14
78 /// CHECK: csel {{w\d+}}, {{w\d+}}, w16, gt
79 public static int maxI(int a) {
80 return Math.max(a, 20);
81 }
82
83 /// CHECK-START: long TestMinMax.maxL(long) builder (after)
84 /// CHECK-DAG: <<Par:j\d+>> ParameterValue
85 /// CHECK-DAG: <<Con:j\d+>> LongConstant 20
86 /// CHECK-DAG: <<Max:j\d+>> Max [<<Par>>,<<Con>>]
87 /// CHECK-DAG: Return [<<Max>>]
88 //
89 /// CHECK-START: long TestMinMax.maxL(long) builder (after)
90 /// CHECK-NOT: InvokeStaticOrDirect
91 //
92 /// CHECK-START-ARM64: long TestMinMax.maxL(long) disassembly (after)
93 /// CHECK-NOT: mov {{x\d+}}, #0x14
94 /// CHECK: cmp {{x\d+}}, #0x14
95 // Check that the constant generation was handled by VIXL.
96 /// CHECK: mov x16, #0x14
97 /// CHECK: csel {{x\d+}}, {{x\d+}}, x16, gt
98 public static long maxL(long a) {
99 return Math.max(a, 20L);
100 }
101
102 //
103 // Special Cases
104 //
105
106 /// CHECK-START-ARM64: int TestMinMax.minIntConstantZero(int) disassembly (after)
107 /// CHECK-NOT: InvokeStaticOrDirect
108 /// CHECK-NOT: mov {{w\d+}}, #0x0
109 /// CHECK: cmp {{w\d+}}, #0x0 (0)
110 /// CHECK: csel {{w\d+}}, {{w\d+}}, wzr, lt
111 /// CHECK: ret
112 public static int minIntConstantZero(int a) {
113 return Math.min(a, 0);
114 }
115
116 /// CHECK-START-ARM64: int TestMinMax.minIntConstantOne(int) disassembly (after)
117 /// CHECK-NOT: InvokeStaticOrDirect
118 /// CHECK-NOT: mov {{w\d+}}, #0x1
119 /// CHECK: cmp {{w\d+}}, #0x1 (1)
120 /// CHECK: csinc {{w\d+}}, {{w\d+}}, wzr, lt
121 /// CHECK: ret
122 public static int minIntConstantOne(int a) {
123 return Math.min(a, 1);
124 }
125
126 /// CHECK-START-ARM64: int TestMinMax.minIntConstantMinusOne(int) disassembly (after)
127 /// CHECK-NOT: InvokeStaticOrDirect
128 /// CHECK-NOT: mov {{w\d+}}, #0xffffffff
129 /// CHECK: cmn {{w\d+}}, #0x1 (1)
130 /// CHECK: csinv {{w\d+}}, {{w\d+}}, wzr, lt
131 /// CHECK: ret
132 public static int minIntConstantMinusOne(int a) {
133 return Math.min(a, -1);
134 }
135
136 /// CHECK-START-ARM64: long TestMinMax.minLongConstantZero(long) disassembly (after)
137 /// CHECK-NOT: InvokeStaticOrDirect
138 /// CHECK-NOT: mov {{x\d+}}, #0x0
139 /// CHECK: cmp {{x\d+}}, #0x0 (0)
140 /// CHECK: csel {{x\d+}}, {{x\d+}}, xzr, lt
141 /// CHECK: ret
142 public static long minLongConstantZero(long a) {
143 return Math.min(a, 0L);
144 }
145
146 /// CHECK-START-ARM64: long TestMinMax.minLongConstantOne(long) disassembly (after)
147 /// CHECK-NOT: InvokeStaticOrDirect
148 /// CHECK-NOT: mov {{x\d+}}, #0x1
149 /// CHECK: cmp {{x\d+}}, #0x1 (1)
150 /// CHECK: csinc {{x\d+}}, {{x\d+}}, xzr, lt
151 /// CHECK: ret
152 public static long minLongConstantOne(long a) {
153 return Math.min(a, 1L);
154 }
155
156 /// CHECK-START-ARM64: long TestMinMax.minLongConstantMinusOne(long) disassembly (after)
157 /// CHECK-NOT: InvokeStaticOrDirect
158 /// CHECK-NOT: mov {{x\d+}}, #0xffffffffffffffff
159 /// CHECK: cmn {{x\d+}}, #0x1 (1)
160 /// CHECK: csinv {{x\d+}}, {{x\d+}}, xzr, lt
161 /// CHECK: ret
162 public static long minLongConstantMinusOne(long a) {
163 return Math.min(a, -1L);
164 }
165
166 /// CHECK-START-ARM64: int TestMinMax.maxIntConstantZero(int) disassembly (after)
167 /// CHECK-NOT: InvokeStaticOrDirect
168 /// CHECK-NOT: mov {{w\d+}}, #0x0
169 /// CHECK: cmp {{w\d+}}, #0x0 (0)
170 /// CHECK: csel {{w\d+}}, {{w\d+}}, wzr, gt
171 /// CHECK: ret
172 public static int maxIntConstantZero(int a) {
173 return Math.max(a, 0);
174 }
175
176 /// CHECK-START-ARM64: int TestMinMax.maxIntConstantOne(int) disassembly (after)
177 /// CHECK-NOT: InvokeStaticOrDirect
178 /// CHECK-NOT: mov {{w\d+}}, #0x1
179 /// CHECK: cmp {{w\d+}}, #0x1 (1)
180 /// CHECK: csinc {{w\d+}}, {{w\d+}}, wzr, gt
181 /// CHECK: ret
182 public static int maxIntConstantOne(int a) {
183 return Math.max(a, 1);
184 }
185
186 /// CHECK-START-ARM64: int TestMinMax.maxIntConstantMinusOne(int) disassembly (after)
187 /// CHECK-NOT: InvokeStaticOrDirect
188 /// CHECK-NOT: mov {{w\d+}}, #0xffffffff
189 /// CHECK: cmn {{w\d+}}, #0x1 (1)
190 /// CHECK: csinv {{w\d+}}, {{w\d+}}, wzr, gt
191 /// CHECK: ret
192 public static int maxIntConstantMinusOne(int a) {
193 return Math.max(a, -1);
194 }
195
196 /// CHECK-START-ARM64: int TestMinMax.maxIntLargeConstant(int) disassembly (after)
197 /// CHECK-NOT: InvokeStaticOrDirect
198 /// CHECK: mov {{w\d+}}, #0x2001
199 /// CHECK: cmp {{w\d+}}, {{w\d+}}
200 // Check that constant generation was not handled by VIXL.
201 /// CHECK-NOT: mov {{w\d+}}, #0x2001
202 /// CHECK: csel {{w\d+}}, {{w\d+}}, {{w\d+}}, gt
203 /// CHECK: ret
204 public static int maxIntLargeConstant(int a) {
205 return Math.max(a, 8193);
206 }
207
208 /// CHECK-START-ARM64: long TestMinMax.maxLongConstantZero(long) disassembly (after)
209 /// CHECK-NOT: InvokeStaticOrDirect
210 /// CHECK-NOT: mov {{x\d+}}, #0x0
211 /// CHECK: cmp {{x\d+}}, #0x0 (0)
212 /// CHECK: csel {{x\d+}}, {{x\d+}}, xzr, gt
213 /// CHECK: ret
214 public static long maxLongConstantZero(long a) {
215 return Math.max(a, 0L);
216 }
217
218 /// CHECK-START-ARM64: long TestMinMax.maxLongConstantOne(long) disassembly (after)
219 /// CHECK-NOT: InvokeStaticOrDirect
220 /// CHECK-NOT: mov {{x\d+}}, #0x1
221 /// CHECK: cmp {{x\d+}}, #0x1 (1)
222 /// CHECK: csinc {{x\d+}}, {{x\d+}}, xzr, gt
223 /// CHECK: ret
224 public static long maxLongConstantOne(long a) {
225 return Math.max(a, 1L);
226 }
227
228 /// CHECK-START-ARM64: long TestMinMax.maxLongConstantMinusOne(long) disassembly (after)
229 /// CHECK-NOT: InvokeStaticOrDirect
230 /// CHECK-NOT: mov {{x\d+}}, #0xffffffffffffffff
231 /// CHECK: cmn {{x\d+}}, #0x1 (1)
232 /// CHECK: csinv {{x\d+}}, {{x\d+}}, xzr, gt
233 /// CHECK: ret
234 public static long maxLongConstantMinusOne(long a) {
235 return Math.max(a, -1L);
236 }
237
238 /// CHECK-START-ARM64: long TestMinMax.maxLongLargeConstant(long) disassembly (after)
239 /// CHECK-NOT: InvokeStaticOrDirect
240 /// CHECK: mov {{x\d+}}, #0x2001
241 /// CHECK: cmp {{x\d+}}, {{x\d+}}
242 // Check that constant generation was not handled by VIXL.
243 /// CHECK-NOT: mov {{x\d+}}, #0x2001
244 /// CHECK: csel {{x\d+}}, {{x\d+}}, {{x\d+}}, gt
245 /// CHECK: ret
246 public static long maxLongLargeConstant(long a) {
247 return Math.max(a, 8193L);
248 }
249
250 //
251 // Different types.
252 //
253
254 /// CHECK-START: int TestMinMax.min1(int, int) instruction_simplifier$after_gvn (before)
255 /// CHECK-DAG: <<Cnd:z\d+>> GreaterThanOrEqual [<<Op1:i\d+>>,<<Op2:i\d+>>]
256 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op1>>,<<Op2>>,<<Cnd>>]
257 /// CHECK-DAG: Return [<<Sel>>]
258 //
259 /// CHECK-START: int TestMinMax.min1(int, int) instruction_simplifier$after_gvn (after)
260 /// CHECK-DAG: <<Min:i\d+>> Min
261 /// CHECK-DAG: Return [<<Min>>]
262 //
263 /// CHECK-START: int TestMinMax.min1(int, int) instruction_simplifier$after_gvn (after)
264 /// CHECK-NOT: Select
265 public static int min1(int a, int b) {
266 return a < b ? a : b;
267 }
268
269 /// CHECK-START: int TestMinMax.min2(int, int) instruction_simplifier$after_gvn (before)
270 /// CHECK-DAG: <<Cnd:z\d+>> GreaterThan [<<Op1:i\d+>>,<<Op2:i\d+>>]
271 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op1>>,<<Op2>>,<<Cnd>>]
272 /// CHECK-DAG: Return [<<Sel>>]
273 //
274 /// CHECK-START: int TestMinMax.min2(int, int) instruction_simplifier$after_gvn (after)
275 /// CHECK-DAG: <<Min:i\d+>> Min
276 /// CHECK-DAG: Return [<<Min>>]
277 //
278 /// CHECK-START: int TestMinMax.min2(int, int) instruction_simplifier$after_gvn (after)
279 /// CHECK-NOT: Select
280 public static int min2(int a, int b) {
281 return a <= b ? a : b;
282 }
283
284 /// CHECK-START: int TestMinMax.min3(int, int) instruction_simplifier$after_gvn (before)
285 /// CHECK-DAG: <<Cnd:z\d+>> LessThanOrEqual [<<Op1:i\d+>>,<<Op2:i\d+>>]
286 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op2>>,<<Op1>>,<<Cnd>>]
287 /// CHECK-DAG: Return [<<Sel>>]
288 //
289 /// CHECK-START: int TestMinMax.min3(int, int) instruction_simplifier$after_gvn (after)
290 /// CHECK-DAG: <<Min:i\d+>> Min
291 /// CHECK-DAG: Return [<<Min>>]
292 //
293 /// CHECK-START: int TestMinMax.min3(int, int) instruction_simplifier$after_gvn (after)
294 /// CHECK-NOT: Select
295 public static int min3(int a, int b) {
296 return a > b ? b : a;
297 }
298
299 /// CHECK-START: int TestMinMax.min4(int, int) instruction_simplifier$after_gvn (before)
300 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Op1:i\d+>>,<<Op2:i\d+>>]
301 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op2>>,<<Op1>>,<<Cnd>>]
302 /// CHECK-DAG: Return [<<Sel>>]
303 //
304 /// CHECK-START: int TestMinMax.min4(int, int) instruction_simplifier$after_gvn (after)
305 /// CHECK-DAG: <<Min:i\d+>> Min
306 /// CHECK-DAG: Return [<<Min>>]
307 //
308 /// CHECK-START: int TestMinMax.min4(int, int) instruction_simplifier$after_gvn (after)
309 /// CHECK-NOT: Select
310 public static int min4(int a, int b) {
311 return a >= b ? b : a;
312 }
313
314 /// CHECK-START: int TestMinMax.min5(short, short) instruction_simplifier$after_gvn (before)
315 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Op1:s\d+>>,<<Op2:s\d+>>]
316 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op2>>,<<Op1>>,<<Cnd>>]
317 /// CHECK-DAG: Return [<<Sel>>]
318 //
319 /// CHECK-START: int TestMinMax.min5(short, short) instruction_simplifier$after_gvn (after)
320 /// CHECK-DAG: <<Min:i\d+>> Min
321 /// CHECK-DAG: Return [<<Min>>]
322 //
323 /// CHECK-START: int TestMinMax.min5(short, short) instruction_simplifier$after_gvn (after)
324 /// CHECK-NOT: Select
325 public static int min5(short a, short b) {
326 return a >= b ? b : a;
327 }
328
329 /// CHECK-START: int TestMinMax.min6(byte, byte) instruction_simplifier$after_gvn (before)
330 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Op1:b\d+>>,<<Op2:b\d+>>]
331 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op2>>,<<Op1>>,<<Cnd>>]
332 /// CHECK-DAG: Return [<<Sel>>]
333 //
334 /// CHECK-START: int TestMinMax.min6(byte, byte) instruction_simplifier$after_gvn (after)
335 /// CHECK-DAG: <<Min:i\d+>> Min
336 /// CHECK-DAG: Return [<<Min>>]
337 //
338 /// CHECK-START: int TestMinMax.min6(byte, byte) instruction_simplifier$after_gvn (after)
339 /// CHECK-NOT: Select
340 public static int min6(byte a, byte b) {
341 return a >= b ? b : a;
342 }
343
344 /// CHECK-START: long TestMinMax.min7(long, long) instruction_simplifier$after_gvn (before)
345 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Op1:j\d+>>,<<Op2:j\d+>>]
346 /// CHECK-DAG: <<Sel:j\d+>> Select [<<Op2>>,<<Op1>>,<<Cnd>>]
347 /// CHECK-DAG: Return [<<Sel>>]
348 //
349 /// CHECK-START: long TestMinMax.min7(long, long) instruction_simplifier$after_gvn (after)
350 /// CHECK-DAG: <<Min:j\d+>> Min
351 /// CHECK-DAG: Return [<<Min>>]
352 //
353 /// CHECK-START: long TestMinMax.min7(long, long) instruction_simplifier$after_gvn (after)
354 /// CHECK-NOT: Select
355 public static long min7(long a, long b) {
356 return a >= b ? b : a;
357 }
358
359 /// CHECK-START: int TestMinMax.max1(int, int) instruction_simplifier$after_gvn (before)
360 /// CHECK-DAG: <<Cnd:z\d+>> GreaterThanOrEqual [<<Op1:i\d+>>,<<Op2:i\d+>>]
361 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op2>>,<<Op1>>,<<Cnd>>]
362 /// CHECK-DAG: Return [<<Sel>>]
363 //
364 /// CHECK-START: int TestMinMax.max1(int, int) instruction_simplifier$after_gvn (after)
365 /// CHECK-DAG: <<Max:i\d+>> Max
366 /// CHECK-DAG: Return [<<Max>>]
367 //
368 /// CHECK-START: int TestMinMax.max1(int, int) instruction_simplifier$after_gvn (after)
369 /// CHECK-NOT: Select
370 public static int max1(int a, int b) {
371 return a < b ? b : a;
372 }
373
374 /// CHECK-START: int TestMinMax.max2(int, int) instruction_simplifier$after_gvn (before)
375 /// CHECK-DAG: <<Cnd:z\d+>> GreaterThan [<<Op1:i\d+>>,<<Op2:i\d+>>]
376 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op2>>,<<Op1>>,<<Cnd>>]
377 /// CHECK-DAG: Return [<<Sel>>]
378 //
379 /// CHECK-START: int TestMinMax.max2(int, int) instruction_simplifier$after_gvn (after)
380 /// CHECK-DAG: <<Max:i\d+>> Max
381 /// CHECK-DAG: Return [<<Max>>]
382 //
383 /// CHECK-START: int TestMinMax.max2(int, int) instruction_simplifier$after_gvn (after)
384 /// CHECK-NOT: Select
385 public static int max2(int a, int b) {
386 return a <= b ? b : a;
387 }
388
389 /// CHECK-START: int TestMinMax.max3(int, int) instruction_simplifier$after_gvn (before)
390 /// CHECK-DAG: <<Cnd:z\d+>> LessThanOrEqual [<<Op1:i\d+>>,<<Op2:i\d+>>]
391 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op1>>,<<Op2>>,<<Cnd>>]
392 /// CHECK-DAG: Return [<<Sel>>]
393 //
394 /// CHECK-START: int TestMinMax.max3(int, int) instruction_simplifier$after_gvn (after)
395 /// CHECK-DAG: <<Max:i\d+>> Max
396 /// CHECK-DAG: Return [<<Max>>]
397 //
398 /// CHECK-START: int TestMinMax.max3(int, int) instruction_simplifier$after_gvn (after)
399 /// CHECK-NOT: Select
400 public static int max3(int a, int b) {
401 return a > b ? a : b;
402 }
403
404 /// CHECK-START: int TestMinMax.max4(int, int) instruction_simplifier$after_gvn (before)
405 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Op1:i\d+>>,<<Op2:i\d+>>]
406 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op1>>,<<Op2>>,<<Cnd>>]
407 /// CHECK-DAG: Return [<<Sel>>]
408 //
409 /// CHECK-START: int TestMinMax.max4(int, int) instruction_simplifier$after_gvn (after)
410 /// CHECK-DAG: <<Max:i\d+>> Max
411 /// CHECK-DAG: Return [<<Max>>]
412 //
413 /// CHECK-START: int TestMinMax.max4(int, int) instruction_simplifier$after_gvn (after)
414 /// CHECK-NOT: Select
415 public static int max4(int a, int b) {
416 return a >= b ? a : b;
417 }
418
419 /// CHECK-START: int TestMinMax.max5(short, short) instruction_simplifier$after_gvn (before)
420 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Op1:s\d+>>,<<Op2:s\d+>>]
421 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op1>>,<<Op2>>,<<Cnd>>]
422 /// CHECK-DAG: Return [<<Sel>>]
423 //
424 /// CHECK-START: int TestMinMax.max5(short, short) instruction_simplifier$after_gvn (after)
425 /// CHECK-DAG: <<Max:i\d+>> Max
426 /// CHECK-DAG: Return [<<Max>>]
427 //
428 /// CHECK-START: int TestMinMax.max5(short, short) instruction_simplifier$after_gvn (after)
429 /// CHECK-NOT: Select
430 public static int max5(short a, short b) {
431 return a >= b ? a : b;
432 }
433
434 /// CHECK-START: int TestMinMax.max6(byte, byte) instruction_simplifier$after_gvn (before)
435 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Op1:b\d+>>,<<Op2:b\d+>>]
436 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Op1>>,<<Op2>>,<<Cnd>>]
437 /// CHECK-DAG: Return [<<Sel>>]
438 //
439 /// CHECK-START: int TestMinMax.max6(byte, byte) instruction_simplifier$after_gvn (after)
440 /// CHECK-DAG: <<Max:i\d+>> Max
441 /// CHECK-DAG: Return [<<Max>>]
442 //
443 /// CHECK-START: int TestMinMax.max6(byte, byte) instruction_simplifier$after_gvn (after)
444 /// CHECK-NOT: Select
445 public static int max6(byte a, byte b) {
446 return a >= b ? a : b;
447 }
448
449 /// CHECK-START: long TestMinMax.max7(long, long) instruction_simplifier$after_gvn (before)
450 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Op1:j\d+>>,<<Op2:j\d+>>]
451 /// CHECK-DAG: <<Sel:j\d+>> Select [<<Op1>>,<<Op2>>,<<Cnd>>]
452 /// CHECK-DAG: Return [<<Sel>>]
453 //
454 /// CHECK-START: long TestMinMax.max7(long, long) instruction_simplifier$after_gvn (after)
455 /// CHECK-DAG: <<Max:j\d+>> Max
456 /// CHECK-DAG: Return [<<Max>>]
457 //
458 /// CHECK-START: long TestMinMax.max7(long, long) instruction_simplifier$after_gvn (after)
459 /// CHECK-NOT: Select
460 public static long max7(long a, long b) {
461 return a >= b ? a : b;
462 }
463
464 //
465 // Complications.
466 //
467
468 /// CHECK-START: int TestMinMax.min0(int[], int[]) instruction_simplifier$after_gvn (before)
469 /// CHECK-DAG: <<Ar1:i\d+>> ArrayGet [{{l\d+}},{{i\d+}}]
470 /// CHECK-DAG: <<Ar2:i\d+>> ArrayGet [{{l\d+}},{{i\d+}}]
471 /// CHECK-DAG: <<Cnd:z\d+>> GreaterThan [<<Ar1>>,<<Ar2>>]
472 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Ar1>>,<<Ar2>>,<<Cnd>>]
473 /// CHECK-DAG: Return [<<Sel>>]
474 //
475 /// CHECK-START: int TestMinMax.min0(int[], int[]) instruction_simplifier$after_gvn (after)
476 /// CHECK-DAG: <<Min:i\d+>> Min
477 /// CHECK-DAG: Return [<<Min>>]
478 //
479 /// CHECK-START: int TestMinMax.min0(int[], int[]) instruction_simplifier$after_gvn (after)
480 /// CHECK-NOT: Select
481 public static int min0(int[] a, int[] b) {
482 // Repeat of array references needs finding the common subexpressions
483 // prior to doing the select and min/max recognition.
484 return a[0] <= b[0] ? a[0] : b[0];
485 }
486
487 /// CHECK-START: int TestMinMax.max0(int[], int[]) instruction_simplifier$after_gvn (before)
488 /// CHECK-DAG: <<Ar1:i\d+>> ArrayGet [{{l\d+}},{{i\d+}}]
489 /// CHECK-DAG: <<Ar2:i\d+>> ArrayGet [{{l\d+}},{{i\d+}}]
490 /// CHECK-DAG: <<Cnd:z\d+>> LessThan [<<Ar1>>,<<Ar2>>]
491 /// CHECK-DAG: <<Sel:i\d+>> Select [<<Ar1>>,<<Ar2>>,<<Cnd>>]
492 /// CHECK-DAG: Return [<<Sel>>]
493 //
494 /// CHECK-START: int TestMinMax.max0(int[], int[]) instruction_simplifier$after_gvn (after)
495 /// CHECK-DAG: <<Max:i\d+>> Max
496 /// CHECK-DAG: Return [<<Max>>]
497 //
498 /// CHECK-START: int TestMinMax.max0(int[], int[]) instruction_simplifier$after_gvn (after)
499 /// CHECK-NOT: Select
500 public static int max0(int[] a, int[] b) {
501 // Repeat of array references needs finding the common subexpressions
502 // prior to doing the select and min/max recognition.
503 return a[0] >= b[0] ? a[0] : b[0];
504 }
505
506 /// CHECK-START: int TestMinMax.minmax1(int) instruction_simplifier$after_gvn (before)
507 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
508 /// CHECK-DAG: <<P100:i\d+>> IntConstant 100
509 /// CHECK-DAG: <<M100:i\d+>> IntConstant -100
510 /// CHECK-DAG: <<Cnd1:z\d+>> LessThanOrEqual [<<Par>>,<<P100>>]
511 /// CHECK-DAG: <<Sel1:i\d+>> Select [<<P100>>,<<Par>>,<<Cnd1>>]
512 /// CHECK-DAG: <<Cnd2:z\d+>> GreaterThanOrEqual [<<Sel1>>,<<M100>>]
513 /// CHECK-DAG: <<Sel2:i\d+>> Select [<<M100>>,<<Sel1>>,<<Cnd2>>]
514 /// CHECK-DAG: Return [<<Sel2>>]
515 //
516 /// CHECK-START: int TestMinMax.minmax1(int) instruction_simplifier$after_gvn (after)
517 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
518 /// CHECK-DAG: <<P100:i\d+>> IntConstant 100
519 /// CHECK-DAG: <<M100:i\d+>> IntConstant -100
520 /// CHECK-DAG: <<Min:i\d+>> Min [<<Par>>,<<P100>>]
521 /// CHECK-DAG: <<Max:i\d+>> Max [<<Min>>,<<M100>>]
522 /// CHECK-DAG: Return [<<Max>>]
523 //
524 /// CHECK-START: int TestMinMax.minmax1(int) instruction_simplifier$after_gvn (after)
525 /// CHECK-NOT: Select
526 public static int minmax1(int x) {
527 // Simple if-if gives clean select sequence.
528 if (x > 100) {
529 x = 100;
530 }
531 if (x < -100) {
532 x = -100;
533 }
534 return x;
535 }
536
537 /// CHECK-START: int TestMinMax.minmax2(int) instruction_simplifier$after_gvn (before)
538 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
539 /// CHECK-DAG: <<P100:i\d+>> IntConstant 100
540 /// CHECK-DAG: <<M100:i\d+>> IntConstant -100
541 /// CHECK-DAG: <<Cnd1:z\d+>> LessThanOrEqual [<<Par>>,<<P100>>]
542 /// CHECK-DAG: <<Cnd2:z\d+>> GreaterThanOrEqual [<<Par>>,<<M100>>]
543 /// CHECK-DAG: <<Sel1:i\d+>> Select [<<M100>>,<<Par>>,<<Cnd2>>]
544 /// CHECK-DAG: <<Sel2:i\d+>> Select [<<P100>>,<<Sel1>>,<<Cnd1>>]
545 /// CHECK-DAG: Return [<<Sel2>>]
546 //
547 /// CHECK-START: int TestMinMax.minmax2(int) instruction_simplifier$after_gvn (after)
548 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
549 /// CHECK-DAG: <<P100:i\d+>> IntConstant 100
550 /// CHECK-DAG: <<M100:i\d+>> IntConstant -100
551 /// CHECK-DAG: <<Max:i\d+>> Max [<<Par>>,<<M100>>]
552 /// CHECK-DAG: <<Min:i\d+>> Min [<<Max>>,<<P100>>]
553 /// CHECK-DAG: Return [<<Min>>]
554 //
555 /// CHECK-START: int TestMinMax.minmax2(int) instruction_simplifier$after_gvn (after)
556 /// CHECK-NOT: Select
557 public static int minmax2(int x) {
558 // Simple if-else requires inspecting bounds of resulting selects.
559 if (x > 100) {
560 x = 100;
561 } else if (x < -100) {
562 x = -100;
563 }
564 return x;
565 }
566
567 /// CHECK-START: int TestMinMax.minmax3(int) select_generator (after)
568 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
569 /// CHECK-DAG: <<P100:i\d+>> IntConstant 100
570 /// CHECK-DAG: <<M100:i\d+>> IntConstant -100
571 /// CHECK-DAG: <<Cnd1:z\d+>> LessThanOrEqual [<<Par>>,<<P100>>]
572 /// CHECK-DAG: <<Cnd2:z\d+>> GreaterThanOrEqual [<<Par>>,<<M100>>]
573 /// CHECK-DAG: <<Sel1:i\d+>> Select [<<M100>>,<<Par>>,<<Cnd2>>]
574 /// CHECK-DAG: <<Sel2:i\d+>> Select [<<P100>>,<<Sel1>>,<<Cnd1>>]
575 /// CHECK-DAG: Return [<<Sel2>>]
576
577 /// CHECK-START: int TestMinMax.minmax3(int) instruction_simplifier$after_gvn (after)
578 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
579 /// CHECK-DAG: <<P100:i\d+>> IntConstant 100
580 /// CHECK-DAG: <<M100:i\d+>> IntConstant -100
581 /// CHECK-DAG: <<Max:i\d+>> Max [<<Par>>,<<M100>>]
582 /// CHECK-DAG: <<Min:i\d+>> Min [<<Max>>,<<P100>>]
583 /// CHECK-DAG: Return [<<Min>>]
584 //
585 /// CHECK-START: int TestMinMax.minmax3(int) instruction_simplifier$after_gvn (after)
586 /// CHECK-NOT: Select
587 public static int minmax3(int x) {
588 return (x > 100) ? 100 : ((x < -100) ? -100 : x);
589 }
590
591 /// CHECK-START: int TestMinMax.minmax4(int) select_generator (after)
592 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
593 /// CHECK-DAG: <<P100:i\d+>> IntConstant 100
594 /// CHECK-DAG: <<M100:i\d+>> IntConstant -100
595 /// CHECK-DAG: <<Cnd1:z\d+>> GreaterThanOrEqual [<<Par>>,<<M100>>]
596 /// CHECK-DAG: <<Cnd2:z\d+>> LessThanOrEqual [<<Par>>,<<P100>>]
597 /// CHECK-DAG: <<Sel1:i\d+>> Select [<<P100>>,<<Par>>,<<Cnd2>>]
598 /// CHECK-DAG: <<Sel2:i\d+>> Select [<<M100>>,<<Sel1>>,<<Cnd1>>]
599 /// CHECK-DAG: Return [<<Sel2>>]
600
601 /// CHECK-START: int TestMinMax.minmax4(int) instruction_simplifier$after_gvn (after)
602 /// CHECK-DAG: <<Par:i\d+>> ParameterValue
603 /// CHECK-DAG: <<P100:i\d+>> IntConstant 100
604 /// CHECK-DAG: <<M100:i\d+>> IntConstant -100
605 /// CHECK-DAG: <<Min:i\d+>> Min [<<Par>>,<<P100>>]
606 /// CHECK-DAG: <<Max:i\d+>> Max [<<Min>>,<<M100>>]
607 /// CHECK-DAG: Return [<<Max>>]
608 //
609 /// CHECK-START: int TestMinMax.minmax4(int) instruction_simplifier$after_gvn (after)
610 /// CHECK-NOT: Select
611 public static int minmax4(int x) {
612 return (x < -100) ? -100 : ((x > 100) ? 100 : x);
613 }
614
615 /// CHECK-START: int TestMinMax.minmaxCSEScalar(int, int) select_generator (after)
616 /// CHECK-DAG: <<Par1:i\d+>> ParameterValue
617 /// CHECK-DAG: <<Par2:i\d+>> ParameterValue
618 /// CHECK-DAG: <<Cnd1:z\d+>> LessThanOrEqual [<<Par1>>,<<Par2>>]
619 /// CHECK-DAG: <<Sel1:i\d+>> Select [<<Par1>>,<<Par2>>,<<Cnd1>>]
620 /// CHECK-DAG: <<Cnd2:z\d+>> GreaterThanOrEqual [<<Par1>>,<<Par2>>]
621 /// CHECK-DAG: <<Sel2:i\d+>> Select [<<Par1>>,<<Par2>>,<<Cnd2>>]
622 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Sel1>>,<<Sel2>>]
623 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Sel1>>,<<Add1>>]
624 /// CHECK-DAG: <<Add3:i\d+>> Add [<<Sel2>>,<<Add2>>]
625 /// CHECK-DAG: <<Add4:i\d+>> Add [<<Sel1>>,<<Add3>>]
626 /// CHECK-DAG: <<Add5:i\d+>> Add [<<Sel2>>,<<Add4>>]
627 /// CHECK-DAG: Return [<<Add5>>]
628 //
629 /// CHECK-START: int TestMinMax.minmaxCSEScalar(int, int) instruction_simplifier$after_gvn (after)
630 /// CHECK-DAG: <<Par1:i\d+>> ParameterValue
631 /// CHECK-DAG: <<Par2:i\d+>> ParameterValue
632 /// CHECK-DAG: <<Max:i\d+>> Max [<<Par1>>,<<Par2>>]
633 /// CHECK-DAG: <<Min:i\d+>> Min [<<Par1>>,<<Par2>>]
634 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Max>>,<<Min>>]
635 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Max>>,<<Add1>>]
636 /// CHECK-DAG: <<Add3:i\d+>> Add [<<Min>>,<<Add2>>]
637 /// CHECK-DAG: <<Add4:i\d+>> Add [<<Max>>,<<Add3>>]
638 /// CHECK-DAG: <<Add5:i\d+>> Add [<<Min>>,<<Add4>>]
639 /// CHECK-DAG: Return [<<Add5>>]
640 public static int minmaxCSEScalar(int x, int y) {
641 int t1 = (x > y) ? x : y;
642 int t2 = (x < y) ? x : y;
643 int t3 = (x > y) ? x : y;
644 int t4 = (x < y) ? x : y;
645 int t5 = (x > y) ? x : y;
646 int t6 = (x < y) ? x : y;
647 // Make sure min/max is CSEed.
648 return t1 + t2 + t3 + t4 + t5 + t6;
649 }
650
651 /// CHECK-START: int TestMinMax.minmaxCSEArray(int[], int[]) select_generator (after)
652 /// CHECK-DAG: <<Arr1:i\d+>> ArrayGet
653 /// CHECK-DAG: <<Arr2:i\d+>> ArrayGet
654 /// CHECK-DAG: <<Cnd1:z\d+>> LessThanOrEqual [<<Arr1>>,<<Arr2>>]
655 /// CHECK-DAG: <<Sel1:i\d+>> Select [<<Arr1>>,<<Arr2>>,<<Cnd1>>]
656 /// CHECK-DAG: <<Cnd2:z\d+>> GreaterThanOrEqual [<<Arr1>>,<<Arr2>>]
657 /// CHECK-DAG: <<Sel2:i\d+>> Select [<<Arr1>>,<<Arr2>>,<<Cnd2>>]
658 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Sel1>>,<<Sel2>>]
659 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Sel1>>,<<Add1>>]
660 /// CHECK-DAG: <<Add3:i\d+>> Add [<<Sel2>>,<<Add2>>]
661 /// CHECK-DAG: <<Add4:i\d+>> Add [<<Sel1>>,<<Add3>>]
662 /// CHECK-DAG: <<Add5:i\d+>> Add [<<Sel2>>,<<Add4>>]
663 /// CHECK-DAG: Return [<<Add5>>]
664 //
665 /// CHECK-START: int TestMinMax.minmaxCSEArray(int[], int[]) instruction_simplifier$after_gvn (after)
666 /// CHECK-DAG: <<Arr1:i\d+>> ArrayGet
667 /// CHECK-DAG: <<Arr2:i\d+>> ArrayGet
668 /// CHECK-DAG: <<Max:i\d+>> Max [<<Arr1>>,<<Arr2>>]
669 /// CHECK-DAG: <<Min:i\d+>> Min [<<Arr1>>,<<Arr2>>]
670 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Max>>,<<Min>>]
671 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Max>>,<<Add1>>]
672 /// CHECK-DAG: <<Add3:i\d+>> Add [<<Min>>,<<Add2>>]
673 /// CHECK-DAG: <<Add4:i\d+>> Add [<<Max>>,<<Add3>>]
674 /// CHECK-DAG: <<Add5:i\d+>> Add [<<Min>>,<<Add4>>]
675 /// CHECK-DAG: Return [<<Add5>>]
676 public static int minmaxCSEArray(int[] x, int[] y) {
677 int t1 = (x[0] > y[0]) ? x[0] : y[0];
678 int t2 = (x[0] < y[0]) ? x[0] : y[0];
679 int t3 = (x[0] > y[0]) ? x[0] : y[0];
680 int t4 = (x[0] < y[0]) ? x[0] : y[0];
681 int t5 = (x[0] > y[0]) ? x[0] : y[0];
682 int t6 = (x[0] < y[0]) ? x[0] : y[0];
683 // Make sure min/max is CSEed.
684 return t1 + t2 + t3 + t4 + t5 + t6;
685 }
686
687 /// CHECK-START: int TestMinMax.minmaxCSEScalarAndCond(int, int) instruction_simplifier$after_gvn (after)
688 /// CHECK-DAG: <<Par1:i\d+>> ParameterValue
689 /// CHECK-DAG: <<Par2:i\d+>> ParameterValue
690 /// CHECK-DAG: <<Max:i\d+>> Max [<<Par1>>,<<Par2>>]
691 /// CHECK-DAG: <<Min:i\d+>> Min [<<Par1>>,<<Par2>>]
692 /// CHECK-DAG: <<Add:i\d+>> Add [<<Max>>,<<Min>>]
693 /// CHECK-DAG: Return [<<Add>>]
694 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Max>>,<<Min>>]
695 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Max>>,<<Add1>>]
696 /// CHECK-DAG: <<Add3:i\d+>> Add [<<Min>>,<<Add2>>]
697 /// CHECK-DAG: Return [<<Add3>>]
698 public static int minmaxCSEScalarAndCond(int x, int y) {
699 int t1 = (x > y) ? x : y;
700 int t2 = (x < y) ? x : y;
701 if (x == y)
702 return t1 + t2;
703 int t3 = (x > y) ? x : y;
704 int t4 = (x < y) ? x : y;
705 // Make sure min/max is CSEed.
706 return t1 + t2 + t3 + t4;
707 }
708
709 public static void main() {
710 // Intrinsics.
711 expectEquals(10, minI(10));
712 expectEquals(20, minI(25));
713 expectEquals(-1, minIntConstantZero(-1));
714 expectEquals(0, minIntConstantZero(1));
715 expectEquals(0, minIntConstantOne(0));
716 expectEquals(1, minIntConstantOne(2));
717 expectEquals(-2, minIntConstantMinusOne(-2));
718 expectEquals(-1, minIntConstantMinusOne(0));
719 expectEquals(10L, minL(10L));
720 expectEquals(20L, minL(25L));
721 expectEquals(-1L, minLongConstantZero(-1L));
722 expectEquals(0L, minLongConstantZero(1L));
723 expectEquals(0L, minLongConstantOne(0L));
724 expectEquals(1L, minLongConstantOne(2L));
725 expectEquals(-2L, minLongConstantMinusOne(-2L));
726 expectEquals(-1L, minLongConstantMinusOne(0L));
727 expectEquals(20, maxI(10));
728 expectEquals(25, maxI(25));
729 expectEquals(0, maxIntConstantZero(-1));
730 expectEquals(1, maxIntConstantZero(1));
731 expectEquals(1, maxIntConstantOne(0));
732 expectEquals(2, maxIntConstantOne(2));
733 expectEquals(-1, maxIntConstantMinusOne(-2));
734 expectEquals(0, maxIntConstantMinusOne(0));
735 expectEquals(8193, maxIntLargeConstant(8192));
736 expectEquals(9000, maxIntLargeConstant(9000));
737 expectEquals(20L, maxL(10L));
738 expectEquals(25L, maxL(25L));
739 expectEquals(0L, maxLongConstantZero(-1L));
740 expectEquals(1L, maxLongConstantZero(1L));
741 expectEquals(1L, maxLongConstantOne(0L));
742 expectEquals(2L, maxLongConstantOne(2L));
743 expectEquals(-1L, maxLongConstantMinusOne(-2L));
744 expectEquals(0L, maxLongConstantMinusOne(0L));
745 expectEquals(8193L, maxLongLargeConstant(8192L));
746 expectEquals(9000L, maxLongLargeConstant(9000L));
747 // Types.
748 expectEquals(10, min1(10, 20));
749 expectEquals(10, min2(10, 20));
750 expectEquals(10, min3(10, 20));
751 expectEquals(10, min4(10, 20));
752 expectEquals(10, min5((short) 10, (short) 20));
753 expectEquals(10, min6((byte) 10, (byte) 20));
754 expectEquals(10L, min7(10L, 20L));
755 expectEquals(20, max1(10, 20));
756 expectEquals(20, max2(10, 20));
757 expectEquals(20, max3(10, 20));
758 expectEquals(20, max4(10, 20));
759 expectEquals(20, max5((short) 10, (short) 20));
760 expectEquals(20, max6((byte) 10, (byte) 20));
761 expectEquals(20L, max7(10L, 20L));
762 // Complications.
763 int[] a = { 10 };
764 int[] b = { 20 };
765 expectEquals(10, min0(a, b));
766 expectEquals(20, max0(a, b));
767 expectEquals(-100, minmax1(-200));
768 expectEquals(10, minmax1(10));
769 expectEquals(100, minmax1(200));
770 expectEquals(-100, minmax2(-200));
771 expectEquals(10, minmax2(10));
772 expectEquals(100, minmax2(200));
773 expectEquals(-100, minmax3(-200));
774 expectEquals(10, minmax3(10));
775 expectEquals(100, minmax3(200));
776 expectEquals(-100, minmax4(-200));
777 expectEquals(10, minmax4(10));
778 expectEquals(100, minmax4(200));
779 expectEquals(90, minmaxCSEScalar(10, 20));
780 expectEquals(90, minmaxCSEArray(a, b));
781 expectEquals(20, minmaxCSEScalarAndCond(10, 10));
782 expectEquals(60, minmaxCSEScalarAndCond(10, 20));
783 System.out.println("TestMinMax passed");
784 }
785
786 private static void expectEquals(int expected, int result) {
787 if (expected != result) {
788 throw new Error("Expected: " + expected + ", found: " + result);
789 }
790 }
791
792 private static void expectEquals(long expected, long result) {
793 if (expected != result) {
794 throw new Error("Expected: " + expected + ", found: " + result);
795 }
796 }
797 }