blob: 59e7282ac705080a5f9b9b3d31d0f6e480dd338e [file] [log] [blame]
David Brazdilee690a32014-12-01 17:04:16 +00001/*
Roland Levillain6a92a032015-07-23 12:15:01 +01002 * Copyright (C) 2014 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 */
David Brazdilee690a32014-12-01 17:04:16 +000016
David Brazdil4846d132015-01-15 19:07:08 +000017public class Main {
David Brazdilee690a32014-12-01 17:04:16 +000018
Roland Levillain3b55ebb2015-05-08 13:13:19 +010019 public static void assertFalse(boolean condition) {
20 if (condition) {
21 throw new Error();
22 }
23 }
24
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000025 public static void assertIntEquals(int expected, int result) {
26 if (expected != result) {
27 throw new Error("Expected: " + expected + ", found: " + result);
28 }
29 }
30
31 public static void assertLongEquals(long expected, long result) {
32 if (expected != result) {
33 throw new Error("Expected: " + expected + ", found: " + result);
34 }
35 }
36
Mark Mendelle82549b2015-05-06 10:55:34 -040037 public static void assertFloatEquals(float expected, float result) {
38 if (expected != result) {
39 throw new Error("Expected: " + expected + ", found: " + result);
40 }
41 }
42
43 public static void assertDoubleEquals(double expected, double result) {
44 if (expected != result) {
45 throw new Error("Expected: " + expected + ", found: " + result);
46 }
47 }
48
Roland Levillainb65eb502015-07-23 12:11:42 +010049
David Brazdilee690a32014-12-01 17:04:16 +000050 /**
Roland Levillainb65eb502015-07-23 12:11:42 +010051 * Exercise constant folding on negation.
David Brazdilee690a32014-12-01 17:04:16 +000052 */
53
David Brazdila06d66a2015-05-28 11:14:54 +010054 /// CHECK-START: int Main.IntNegation() constant_folding (before)
55 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42
56 /// CHECK-DAG: <<Neg:i\d+>> Neg [<<Const42>>]
57 /// CHECK-DAG: Return [<<Neg>>]
David Brazdilee690a32014-12-01 17:04:16 +000058
David Brazdila06d66a2015-05-28 11:14:54 +010059 /// CHECK-START: int Main.IntNegation() constant_folding (after)
60 /// CHECK-DAG: <<ConstN42:i\d+>> IntConstant -42
61 /// CHECK-DAG: Return [<<ConstN42>>]
David Brazdilee690a32014-12-01 17:04:16 +000062
Roland Levillainb65eb502015-07-23 12:11:42 +010063 /// CHECK-START: int Main.IntNegation() constant_folding (after)
64 /// CHECK-NOT: Neg
65
David Brazdilee690a32014-12-01 17:04:16 +000066 public static int IntNegation() {
67 int x, y;
68 x = 42;
69 y = -x;
70 return y;
71 }
72
Roland Levillainc90bc7c2014-12-11 12:14:33 +000073 /// CHECK-START: long Main.LongNegation() constant_folding (before)
74 /// CHECK-DAG: <<Const42:j\d+>> LongConstant 42
75 /// CHECK-DAG: <<Neg:j\d+>> Neg [<<Const42>>]
76 /// CHECK-DAG: Return [<<Neg>>]
77
78 /// CHECK-START: long Main.LongNegation() constant_folding (after)
79 /// CHECK-DAG: <<ConstN42:j\d+>> LongConstant -42
80 /// CHECK-DAG: Return [<<ConstN42>>]
81
82 /// CHECK-START: long Main.LongNegation() constant_folding (after)
83 /// CHECK-NOT: Neg
84
85 public static long LongNegation() {
86 long x, y;
87 x = 42L;
88 y = -x;
89 return y;
90 }
91
Roland Levillainb65eb502015-07-23 12:11:42 +010092
David Brazdilee690a32014-12-01 17:04:16 +000093 /**
Roland Levillainb65eb502015-07-23 12:11:42 +010094 * Exercise constant folding on addition.
David Brazdilee690a32014-12-01 17:04:16 +000095 */
96
David Brazdila06d66a2015-05-28 11:14:54 +010097 /// CHECK-START: int Main.IntAddition1() constant_folding (before)
98 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
99 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
100 /// CHECK-DAG: <<Add:i\d+>> Add [<<Const1>>,<<Const2>>]
101 /// CHECK-DAG: Return [<<Add>>]
David Brazdilee690a32014-12-01 17:04:16 +0000102
David Brazdila06d66a2015-05-28 11:14:54 +0100103 /// CHECK-START: int Main.IntAddition1() constant_folding (after)
104 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
105 /// CHECK-DAG: Return [<<Const3>>]
David Brazdilee690a32014-12-01 17:04:16 +0000106
Roland Levillainb65eb502015-07-23 12:11:42 +0100107 /// CHECK-START: int Main.IntAddition1() constant_folding (after)
108 /// CHECK-NOT: Add
109
David Brazdilee690a32014-12-01 17:04:16 +0000110 public static int IntAddition1() {
111 int a, b, c;
112 a = 1;
113 b = 2;
114 c = a + b;
115 return c;
116 }
117
David Brazdila06d66a2015-05-28 11:14:54 +0100118 /// CHECK-START: int Main.IntAddition2() constant_folding (before)
119 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
120 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
121 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
122 /// CHECK-DAG: <<Const6:i\d+>> IntConstant 6
123 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Const1>>,<<Const2>>]
124 /// CHECK-DAG: <<Add2:i\d+>> Add [<<Const5>>,<<Const6>>]
125 /// CHECK-DAG: <<Add3:i\d+>> Add [<<Add1>>,<<Add2>>]
126 /// CHECK-DAG: Return [<<Add3>>]
David Brazdilee690a32014-12-01 17:04:16 +0000127
David Brazdila06d66a2015-05-28 11:14:54 +0100128 /// CHECK-START: int Main.IntAddition2() constant_folding (after)
129 /// CHECK-DAG: <<Const14:i\d+>> IntConstant 14
130 /// CHECK-DAG: Return [<<Const14>>]
David Brazdilee690a32014-12-01 17:04:16 +0000131
Roland Levillainb65eb502015-07-23 12:11:42 +0100132 /// CHECK-START: int Main.IntAddition2() constant_folding (after)
133 /// CHECK-NOT: Add
134
David Brazdilee690a32014-12-01 17:04:16 +0000135 public static int IntAddition2() {
136 int a, b, c;
137 a = 1;
138 b = 2;
139 a += b;
140 b = 5;
141 c = 6;
142 b += c;
143 c = a + b;
144 return c;
145 }
146
Roland Levillainb65eb502015-07-23 12:11:42 +0100147 /// CHECK-START: long Main.LongAddition() constant_folding (before)
148 /// CHECK-DAG: <<Const1:j\d+>> LongConstant 1
149 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
150 /// CHECK-DAG: <<Add:j\d+>> Add [<<Const1>>,<<Const2>>]
151 /// CHECK-DAG: Return [<<Add>>]
152
153 /// CHECK-START: long Main.LongAddition() constant_folding (after)
154 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
155 /// CHECK-DAG: Return [<<Const3>>]
156
157 /// CHECK-START: long Main.LongAddition() constant_folding (after)
158 /// CHECK-NOT: Add
159
160 public static long LongAddition() {
161 long a, b, c;
162 a = 1L;
163 b = 2L;
164 c = a + b;
165 return c;
166 }
167
168
David Brazdilee690a32014-12-01 17:04:16 +0000169 /**
Roland Levillainb65eb502015-07-23 12:11:42 +0100170 * Exercise constant folding on subtraction.
David Brazdilee690a32014-12-01 17:04:16 +0000171 */
172
David Brazdila06d66a2015-05-28 11:14:54 +0100173 /// CHECK-START: int Main.IntSubtraction() constant_folding (before)
174 /// CHECK-DAG: <<Const6:i\d+>> IntConstant 6
175 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
176 /// CHECK-DAG: <<Sub:i\d+>> Sub [<<Const6>>,<<Const2>>]
177 /// CHECK-DAG: Return [<<Sub>>]
David Brazdilee690a32014-12-01 17:04:16 +0000178
David Brazdila06d66a2015-05-28 11:14:54 +0100179 /// CHECK-START: int Main.IntSubtraction() constant_folding (after)
180 /// CHECK-DAG: <<Const4:i\d+>> IntConstant 4
181 /// CHECK-DAG: Return [<<Const4>>]
David Brazdilee690a32014-12-01 17:04:16 +0000182
Roland Levillainb65eb502015-07-23 12:11:42 +0100183 /// CHECK-START: int Main.IntSubtraction() constant_folding (after)
184 /// CHECK-NOT: Sub
185
David Brazdilee690a32014-12-01 17:04:16 +0000186 public static int IntSubtraction() {
187 int a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000188 a = 6;
David Brazdilee690a32014-12-01 17:04:16 +0000189 b = 2;
190 c = a - b;
191 return c;
192 }
193
David Brazdila06d66a2015-05-28 11:14:54 +0100194 /// CHECK-START: long Main.LongSubtraction() constant_folding (before)
195 /// CHECK-DAG: <<Const6:j\d+>> LongConstant 6
196 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
197 /// CHECK-DAG: <<Sub:j\d+>> Sub [<<Const6>>,<<Const2>>]
198 /// CHECK-DAG: Return [<<Sub>>]
David Brazdilee690a32014-12-01 17:04:16 +0000199
David Brazdila06d66a2015-05-28 11:14:54 +0100200 /// CHECK-START: long Main.LongSubtraction() constant_folding (after)
201 /// CHECK-DAG: <<Const4:j\d+>> LongConstant 4
202 /// CHECK-DAG: Return [<<Const4>>]
David Brazdilee690a32014-12-01 17:04:16 +0000203
Roland Levillainb65eb502015-07-23 12:11:42 +0100204 /// CHECK-START: long Main.LongSubtraction() constant_folding (after)
205 /// CHECK-NOT: Sub
206
David Brazdilee690a32014-12-01 17:04:16 +0000207 public static long LongSubtraction() {
208 long a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000209 a = 6L;
David Brazdilee690a32014-12-01 17:04:16 +0000210 b = 2L;
211 c = a - b;
212 return c;
213 }
214
Roland Levillainb65eb502015-07-23 12:11:42 +0100215
David Brazdilee690a32014-12-01 17:04:16 +0000216 /**
Roland Levillainf7746ad2015-07-22 14:12:01 +0100217 * Exercise constant folding on multiplication.
218 */
219
220 /// CHECK-START: int Main.IntMultiplication() constant_folding (before)
221 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
222 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
223 /// CHECK-DAG: <<Mul:i\d+>> Mul [<<Const7>>,<<Const3>>]
224 /// CHECK-DAG: Return [<<Mul>>]
225
226 /// CHECK-START: int Main.IntMultiplication() constant_folding (after)
227 /// CHECK-DAG: <<Const21:i\d+>> IntConstant 21
228 /// CHECK-DAG: Return [<<Const21>>]
229
230 /// CHECK-START: int Main.IntMultiplication() constant_folding (after)
231 /// CHECK-NOT: Mul
232
233 public static int IntMultiplication() {
234 int a, b, c;
235 a = 7;
236 b = 3;
237 c = a * b;
238 return c;
239 }
240
241 /// CHECK-START: long Main.LongMultiplication() constant_folding (before)
242 /// CHECK-DAG: <<Const7:j\d+>> LongConstant 7
243 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
244 /// CHECK-DAG: <<Mul:j\d+>> Mul [<<Const7>>,<<Const3>>]
245 /// CHECK-DAG: Return [<<Mul>>]
246
247 /// CHECK-START: long Main.LongMultiplication() constant_folding (after)
248 /// CHECK-DAG: <<Const21:j\d+>> LongConstant 21
249 /// CHECK-DAG: Return [<<Const21>>]
250
251 /// CHECK-START: long Main.LongMultiplication() constant_folding (after)
252 /// CHECK-NOT: Mul
253
254 public static long LongMultiplication() {
255 long a, b, c;
256 a = 7L;
257 b = 3L;
258 c = a * b;
259 return c;
260 }
261
262
263 /**
264 * Exercise constant folding on division.
265 */
266
267 /// CHECK-START: int Main.IntDivision() constant_folding (before)
268 /// CHECK-DAG: <<Const8:i\d+>> IntConstant 8
269 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
270 /// CHECK-DAG: <<Div0Chk:i\d+>> DivZeroCheck [<<Const3>>]
271 /// CHECK-DAG: <<Div:i\d+>> Div [<<Const8>>,<<Div0Chk>>]
272 /// CHECK-DAG: Return [<<Div>>]
273
274 /// CHECK-START: int Main.IntDivision() constant_folding (after)
275 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
276 /// CHECK-DAG: Return [<<Const2>>]
277
278 /// CHECK-START: int Main.IntDivision() constant_folding (after)
279 /// CHECK-NOT: DivZeroCheck
280 /// CHECK-NOT: Div
281
282 public static int IntDivision() {
283 int a, b, c;
284 a = 8;
285 b = 3;
286 c = a / b;
287 return c;
288 }
289
290 /// CHECK-START: long Main.LongDivision() constant_folding (before)
291 /// CHECK-DAG: <<Const8:j\d+>> LongConstant 8
292 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
293 /// CHECK-DAG: <<Div0Chk:j\d+>> DivZeroCheck [<<Const3>>]
294 /// CHECK-DAG: <<Div:j\d+>> Div [<<Const8>>,<<Div0Chk>>]
295 /// CHECK-DAG: Return [<<Div>>]
296
297 /// CHECK-START: long Main.LongDivision() constant_folding (after)
298 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
299 /// CHECK-DAG: Return [<<Const2>>]
300
301 /// CHECK-START: long Main.LongDivision() constant_folding (after)
302 /// CHECK-NOT: DivZeroCheck
303 /// CHECK-NOT: Div
304
305 public static long LongDivision() {
306 long a, b, c;
307 a = 8L;
308 b = 3L;
309 c = a / b;
310 return c;
311 }
312
313
314 /**
315 * Exercise constant folding on remainder.
316 */
317
318 /// CHECK-START: int Main.IntRemainder() constant_folding (before)
319 /// CHECK-DAG: <<Const8:i\d+>> IntConstant 8
320 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
321 /// CHECK-DAG: <<Div0Chk:i\d+>> DivZeroCheck [<<Const3>>]
322 /// CHECK-DAG: <<Rem:i\d+>> Rem [<<Const8>>,<<Div0Chk>>]
323 /// CHECK-DAG: Return [<<Rem>>]
324
325 /// CHECK-START: int Main.IntRemainder() constant_folding (after)
326 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
327 /// CHECK-DAG: Return [<<Const2>>]
328
329 /// CHECK-START: int Main.IntRemainder() constant_folding (after)
330 /// CHECK-NOT: DivZeroCheck
331 /// CHECK-NOT: Rem
332
333 public static int IntRemainder() {
334 int a, b, c;
335 a = 8;
336 b = 3;
337 c = a % b;
338 return c;
339 }
340
341 /// CHECK-START: long Main.LongRemainder() constant_folding (before)
342 /// CHECK-DAG: <<Const8:j\d+>> LongConstant 8
343 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
344 /// CHECK-DAG: <<Div0Chk:j\d+>> DivZeroCheck [<<Const3>>]
345 /// CHECK-DAG: <<Rem:j\d+>> Rem [<<Const8>>,<<Div0Chk>>]
346 /// CHECK-DAG: Return [<<Rem>>]
347
348 /// CHECK-START: long Main.LongRemainder() constant_folding (after)
349 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
350 /// CHECK-DAG: Return [<<Const2>>]
351
352 /// CHECK-START: long Main.LongRemainder() constant_folding (after)
353 /// CHECK-NOT: DivZeroCheck
354 /// CHECK-NOT: Rem
355
356 public static long LongRemainder() {
357 long a, b, c;
358 a = 8L;
359 b = 3L;
360 c = a % b;
361 return c;
362 }
363
364
365 /**
Roland Levillain9867bc72015-08-05 10:21:34 +0100366 * Exercise constant folding on left shift.
367 */
368
369 /// CHECK-START: int Main.ShlIntLong() constant_folding (before)
370 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
371 /// CHECK-DAG: <<Const2L:j\d+>> LongConstant 2
372 /// CHECK-DAG: <<TypeConv:i\d+>> TypeConversion [<<Const2L>>]
373 /// CHECK-DAG: <<Shl:i\d+>> Shl [<<Const1>>,<<TypeConv>>]
374 /// CHECK-DAG: Return [<<Shl>>]
375
376 /// CHECK-START: int Main.ShlIntLong() constant_folding (after)
377 /// CHECK-DAG: <<Const4:i\d+>> IntConstant 4
378 /// CHECK-DAG: Return [<<Const4>>]
379
380 /// CHECK-START: int Main.ShlIntLong() constant_folding (after)
381 /// CHECK-NOT: Shl
382
383 public static int ShlIntLong() {
384 int lhs = 1;
385 long rhs = 2;
386 return lhs << rhs;
387 }
388
389 /// CHECK-START: long Main.ShlLongInt() constant_folding (before)
390 /// CHECK-DAG: <<Const3L:j\d+>> LongConstant 3
391 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
392 /// CHECK-DAG: <<Shl:j\d+>> Shl [<<Const3L>>,<<Const2>>]
393 /// CHECK-DAG: Return [<<Shl>>]
394
395 /// CHECK-START: long Main.ShlLongInt() constant_folding (after)
396 /// CHECK-DAG: <<Const12L:j\d+>> LongConstant 12
397 /// CHECK-DAG: Return [<<Const12L>>]
398
399 /// CHECK-START: long Main.ShlLongInt() constant_folding (after)
400 /// CHECK-NOT: Shl
401
402 public static long ShlLongInt() {
403 long lhs = 3;
404 int rhs = 2;
405 return lhs << rhs;
406 }
407
408
409 /**
410 * Exercise constant folding on right shift.
411 */
412
413 /// CHECK-START: int Main.ShrIntLong() constant_folding (before)
414 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
415 /// CHECK-DAG: <<Const2L:j\d+>> LongConstant 2
416 /// CHECK-DAG: <<TypeConv:i\d+>> TypeConversion [<<Const2L>>]
417 /// CHECK-DAG: <<Shr:i\d+>> Shr [<<Const7>>,<<TypeConv>>]
418 /// CHECK-DAG: Return [<<Shr>>]
419
420 /// CHECK-START: int Main.ShrIntLong() constant_folding (after)
421 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
422 /// CHECK-DAG: Return [<<Const1>>]
423
424 /// CHECK-START: int Main.ShrIntLong() constant_folding (after)
425 /// CHECK-NOT: Shr
426
427 public static int ShrIntLong() {
428 int lhs = 7;
429 long rhs = 2;
430 return lhs >> rhs;
431 }
432
433 /// CHECK-START: long Main.ShrLongInt() constant_folding (before)
434 /// CHECK-DAG: <<Const9L:j\d+>> LongConstant 9
435 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
436 /// CHECK-DAG: <<Shr:j\d+>> Shr [<<Const9L>>,<<Const2>>]
437 /// CHECK-DAG: Return [<<Shr>>]
438
439 /// CHECK-START: long Main.ShrLongInt() constant_folding (after)
440 /// CHECK-DAG: <<Const2L:j\d+>> LongConstant 2
441 /// CHECK-DAG: Return [<<Const2L>>]
442
443 /// CHECK-START: long Main.ShrLongInt() constant_folding (after)
444 /// CHECK-NOT: Shr
445
446 public static long ShrLongInt() {
447 long lhs = 9;
448 int rhs = 2;
449 return lhs >> rhs;
450 }
451
452
453 /**
454 * Exercise constant folding on unsigned right shift.
455 */
456
457 /// CHECK-START: int Main.UShrIntLong() constant_folding (before)
458 /// CHECK-DAG: <<ConstM7:i\d+>> IntConstant -7
459 /// CHECK-DAG: <<Const2L:j\d+>> LongConstant 2
460 /// CHECK-DAG: <<TypeConv:i\d+>> TypeConversion [<<Const2L>>]
461 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<ConstM7>>,<<TypeConv>>]
462 /// CHECK-DAG: Return [<<UShr>>]
463
464 /// CHECK-START: int Main.UShrIntLong() constant_folding (after)
465 /// CHECK-DAG: <<ConstRes:i\d+>> IntConstant 1073741822
466 /// CHECK-DAG: Return [<<ConstRes>>]
467
468 /// CHECK-START: int Main.UShrIntLong() constant_folding (after)
469 /// CHECK-NOT: UShr
470
471 public static int UShrIntLong() {
472 int lhs = -7;
473 long rhs = 2;
474 return lhs >>> rhs;
475 }
476
477 /// CHECK-START: long Main.UShrLongInt() constant_folding (before)
478 /// CHECK-DAG: <<ConstM9L:j\d+>> LongConstant -9
479 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
480 /// CHECK-DAG: <<UShr:j\d+>> UShr [<<ConstM9L>>,<<Const2>>]
481 /// CHECK-DAG: Return [<<UShr>>]
482
483 /// CHECK-START: long Main.UShrLongInt() constant_folding (after)
484 /// CHECK-DAG: <<ConstRes:j\d+>> LongConstant 4611686018427387901
485 /// CHECK-DAG: Return [<<ConstRes>>]
486
487 /// CHECK-START: long Main.UShrLongInt() constant_folding (after)
488 /// CHECK-NOT: UShr
489
490 public static long UShrLongInt() {
491 long lhs = -9;
492 int rhs = 2;
493 return lhs >>> rhs;
494 }
495
496
497 /**
498 * Exercise constant folding on logical and.
499 */
500
501 /// CHECK-START: long Main.AndIntLong() constant_folding (before)
502 /// CHECK-DAG: <<Const10:i\d+>> IntConstant 10
503 /// CHECK-DAG: <<Const3L:j\d+>> LongConstant 3
504 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const10>>]
505 /// CHECK-DAG: <<And:j\d+>> And [<<TypeConv>>,<<Const3L>>]
506 /// CHECK-DAG: Return [<<And>>]
507
508 /// CHECK-START: long Main.AndIntLong() constant_folding (after)
509 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
510 /// CHECK-DAG: Return [<<Const2>>]
511
512 /// CHECK-START: long Main.AndIntLong() constant_folding (after)
513 /// CHECK-NOT: And
514
515 public static long AndIntLong() {
516 int lhs = 10;
517 long rhs = 3;
518 return lhs & rhs;
519 }
520
521 /// CHECK-START: long Main.AndLongInt() constant_folding (before)
522 /// CHECK-DAG: <<Const10L:j\d+>> LongConstant 10
523 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
524 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const3>>]
525 /// CHECK-DAG: <<And:j\d+>> And [<<Const10L>>,<<TypeConv>>]
526 /// CHECK-DAG: Return [<<And>>]
527
528 /// CHECK-START: long Main.AndLongInt() constant_folding (after)
529 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
530 /// CHECK-DAG: Return [<<Const2>>]
531
532 /// CHECK-START: long Main.AndLongInt() constant_folding (after)
533 /// CHECK-NOT: And
534
535 public static long AndLongInt() {
536 long lhs = 10;
537 int rhs = 3;
538 return lhs & rhs;
539 }
540
541
542 /**
543 * Exercise constant folding on logical or.
544 */
545
546 /// CHECK-START: long Main.OrIntLong() constant_folding (before)
547 /// CHECK-DAG: <<Const10:i\d+>> IntConstant 10
548 /// CHECK-DAG: <<Const3L:j\d+>> LongConstant 3
549 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const10>>]
550 /// CHECK-DAG: <<Or:j\d+>> Or [<<TypeConv>>,<<Const3L>>]
551 /// CHECK-DAG: Return [<<Or>>]
552
553 /// CHECK-START: long Main.OrIntLong() constant_folding (after)
554 /// CHECK-DAG: <<Const11:j\d+>> LongConstant 11
555 /// CHECK-DAG: Return [<<Const11>>]
556
557 /// CHECK-START: long Main.OrIntLong() constant_folding (after)
558 /// CHECK-NOT: Or
559
560 public static long OrIntLong() {
561 int lhs = 10;
562 long rhs = 3;
563 return lhs | rhs;
564 }
565
566 /// CHECK-START: long Main.OrLongInt() constant_folding (before)
567 /// CHECK-DAG: <<Const10L:j\d+>> LongConstant 10
568 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
569 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const3>>]
570 /// CHECK-DAG: <<Or:j\d+>> Or [<<Const10L>>,<<TypeConv>>]
571 /// CHECK-DAG: Return [<<Or>>]
572
573 /// CHECK-START: long Main.OrLongInt() constant_folding (after)
574 /// CHECK-DAG: <<Const11:j\d+>> LongConstant 11
575 /// CHECK-DAG: Return [<<Const11>>]
576
577 /// CHECK-START: long Main.OrLongInt() constant_folding (after)
578 /// CHECK-NOT: Or
579
580 public static long OrLongInt() {
581 long lhs = 10;
582 int rhs = 3;
583 return lhs | rhs;
584 }
585
586
587 /**
588 * Exercise constant folding on logical exclusive or.
589 */
590
591 /// CHECK-START: long Main.XorIntLong() constant_folding (before)
592 /// CHECK-DAG: <<Const10:i\d+>> IntConstant 10
593 /// CHECK-DAG: <<Const3L:j\d+>> LongConstant 3
594 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const10>>]
595 /// CHECK-DAG: <<Xor:j\d+>> Xor [<<TypeConv>>,<<Const3L>>]
596 /// CHECK-DAG: Return [<<Xor>>]
597
598 /// CHECK-START: long Main.XorIntLong() constant_folding (after)
599 /// CHECK-DAG: <<Const9:j\d+>> LongConstant 9
600 /// CHECK-DAG: Return [<<Const9>>]
601
602 /// CHECK-START: long Main.XorIntLong() constant_folding (after)
603 /// CHECK-NOT: Xor
604
605 public static long XorIntLong() {
606 int lhs = 10;
607 long rhs = 3;
608 return lhs ^ rhs;
609 }
610
611 /// CHECK-START: long Main.XorLongInt() constant_folding (before)
612 /// CHECK-DAG: <<Const10L:j\d+>> LongConstant 10
613 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
614 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const3>>]
615 /// CHECK-DAG: <<Xor:j\d+>> Xor [<<Const10L>>,<<TypeConv>>]
616 /// CHECK-DAG: Return [<<Xor>>]
617
618 /// CHECK-START: long Main.XorLongInt() constant_folding (after)
619 /// CHECK-DAG: <<Const9:j\d+>> LongConstant 9
620 /// CHECK-DAG: Return [<<Const9>>]
621
622 /// CHECK-START: long Main.XorLongInt() constant_folding (after)
623 /// CHECK-NOT: Xor
624
625 public static long XorLongInt() {
626 long lhs = 10;
627 int rhs = 3;
628 return lhs ^ rhs;
629 }
630
631
632 /**
Roland Levillainb65eb502015-07-23 12:11:42 +0100633 * Exercise constant folding on constant (static) condition.
David Brazdilee690a32014-12-01 17:04:16 +0000634 */
635
David Brazdila06d66a2015-05-28 11:14:54 +0100636 /// CHECK-START: int Main.StaticCondition() constant_folding (before)
637 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
638 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
639 /// CHECK-DAG: <<Cond:z\d+>> GreaterThanOrEqual [<<Const7>>,<<Const2>>]
640 /// CHECK-DAG: If [<<Cond>>]
David Brazdilee690a32014-12-01 17:04:16 +0000641
David Brazdila06d66a2015-05-28 11:14:54 +0100642 /// CHECK-START: int Main.StaticCondition() constant_folding (after)
643 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
644 /// CHECK-DAG: If [<<Const1>>]
David Brazdilee690a32014-12-01 17:04:16 +0000645
Roland Levillainb65eb502015-07-23 12:11:42 +0100646 /// CHECK-START: int Main.StaticCondition() constant_folding (after)
647 /// CHECK-NOT: GreaterThanOrEqual
648
David Brazdilee690a32014-12-01 17:04:16 +0000649 public static int StaticCondition() {
650 int a, b, c;
David Brazdil4846d132015-01-15 19:07:08 +0000651 a = 7;
David Brazdilee690a32014-12-01 17:04:16 +0000652 b = 2;
653 if (a < b)
654 c = a + b;
655 else
656 c = a - b;
657 return c;
658 }
659
Roland Levillainb65eb502015-07-23 12:11:42 +0100660
David Brazdilee690a32014-12-01 17:04:16 +0000661 /**
Roland Levillainb65eb502015-07-23 12:11:42 +0100662 * Exercise constant folding on a program with condition
663 * (i.e. jumps) leading to the creation of many blocks.
David Brazdilee690a32014-12-01 17:04:16 +0000664 *
665 * The intent of this test is to ensure that all constant expressions
666 * are actually evaluated at compile-time, thanks to the reverse
667 * (forward) post-order traversal of the the dominator tree.
668 */
669
David Brazdila06d66a2015-05-28 11:14:54 +0100670 /// CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (before)
671 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
672 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
673 /// CHECK-DAG: <<Add:i\d+>> Add [<<Const5>>,<<Const2>>]
674 /// CHECK-DAG: <<Sub:i\d+>> Sub [<<Const5>>,<<Const2>>]
675 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Add>>,<<Sub>>]
676 /// CHECK-DAG: Return [<<Phi>>]
David Brazdilee690a32014-12-01 17:04:16 +0000677
David Brazdila06d66a2015-05-28 11:14:54 +0100678 /// CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (after)
679 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
680 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
681 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Const7>>,<<Const3>>]
682 /// CHECK-DAG: Return [<<Phi>>]
David Brazdilee690a32014-12-01 17:04:16 +0000683
Roland Levillainb65eb502015-07-23 12:11:42 +0100684 /// CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (after)
685 /// CHECK-NOT: Add
686 /// CHECK-NOT: Sub
687
David Brazdilee690a32014-12-01 17:04:16 +0000688 public static int JumpsAndConditionals(boolean cond) {
689 int a, b, c;
690 a = 5;
691 b = 2;
692 if (cond)
693 c = a + b;
694 else
695 c = a - b;
696 return c;
697 }
David Brazdil4846d132015-01-15 19:07:08 +0000698
Roland Levillainb65eb502015-07-23 12:11:42 +0100699
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000700 /**
701 * Test optimizations of arithmetic identities yielding a constant result.
702 */
703
David Brazdila06d66a2015-05-28 11:14:54 +0100704 /// CHECK-START: int Main.And0(int) constant_folding (before)
705 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
706 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
707 /// CHECK-DAG: <<And:i\d+>> And [<<Arg>>,<<Const0>>]
708 /// CHECK-DAG: Return [<<And>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000709
David Brazdila06d66a2015-05-28 11:14:54 +0100710 /// CHECK-START: int Main.And0(int) constant_folding (after)
711 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
712 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100713 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000714
Roland Levillainb65eb502015-07-23 12:11:42 +0100715 /// CHECK-START: int Main.And0(int) constant_folding (after)
716 /// CHECK-NOT: And
717
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000718 public static int And0(int arg) {
719 return arg & 0;
720 }
721
David Brazdila06d66a2015-05-28 11:14:54 +0100722 /// CHECK-START: long Main.Mul0(long) constant_folding (before)
723 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
724 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
725 /// CHECK-DAG: <<Mul:j\d+>> Mul [<<Arg>>,<<Const0>>]
726 /// CHECK-DAG: Return [<<Mul>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000727
David Brazdila06d66a2015-05-28 11:14:54 +0100728 /// CHECK-START: long Main.Mul0(long) constant_folding (after)
729 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
730 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100731 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000732
Roland Levillainb65eb502015-07-23 12:11:42 +0100733 /// CHECK-START: long Main.Mul0(long) constant_folding (after)
734 /// CHECK-NOT: Mul
735
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000736 public static long Mul0(long arg) {
737 return arg * 0;
738 }
739
David Brazdila06d66a2015-05-28 11:14:54 +0100740 /// CHECK-START: int Main.OrAllOnes(int) constant_folding (before)
741 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
742 /// CHECK-DAG: <<ConstF:i\d+>> IntConstant -1
743 /// CHECK-DAG: <<Or:i\d+>> Or [<<Arg>>,<<ConstF>>]
744 /// CHECK-DAG: Return [<<Or>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000745
David Brazdila06d66a2015-05-28 11:14:54 +0100746 /// CHECK-START: int Main.OrAllOnes(int) constant_folding (after)
747 /// CHECK-DAG: <<ConstF:i\d+>> IntConstant -1
David Brazdila06d66a2015-05-28 11:14:54 +0100748 /// CHECK-DAG: Return [<<ConstF>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000749
Roland Levillainb65eb502015-07-23 12:11:42 +0100750 /// CHECK-START: int Main.OrAllOnes(int) constant_folding (after)
751 /// CHECK-NOT: Or
752
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000753 public static int OrAllOnes(int arg) {
754 return arg | -1;
755 }
756
David Brazdila06d66a2015-05-28 11:14:54 +0100757 /// CHECK-START: long Main.Rem0(long) constant_folding (before)
758 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
759 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
760 /// CHECK-DAG: <<DivZeroCheck:j\d+>> DivZeroCheck [<<Arg>>]
761 /// CHECK-DAG: <<Rem:j\d+>> Rem [<<Const0>>,<<DivZeroCheck>>]
762 /// CHECK-DAG: Return [<<Rem>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000763
David Brazdila06d66a2015-05-28 11:14:54 +0100764 /// CHECK-START: long Main.Rem0(long) constant_folding (after)
765 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100766 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000767
Roland Levillainb65eb502015-07-23 12:11:42 +0100768 /// CHECK-START: long Main.Rem0(long) constant_folding (after)
769 /// CHECK-NOT: Rem
770
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000771 public static long Rem0(long arg) {
772 return 0 % arg;
773 }
774
David Brazdila06d66a2015-05-28 11:14:54 +0100775 /// CHECK-START: int Main.Rem1(int) constant_folding (before)
776 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
777 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
778 /// CHECK-DAG: <<Rem:i\d+>> Rem [<<Arg>>,<<Const1>>]
779 /// CHECK-DAG: Return [<<Rem>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000780
David Brazdila06d66a2015-05-28 11:14:54 +0100781 /// CHECK-START: int Main.Rem1(int) constant_folding (after)
782 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100783 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000784
Roland Levillainb65eb502015-07-23 12:11:42 +0100785 /// CHECK-START: int Main.Rem1(int) constant_folding (after)
786 /// CHECK-NOT: Rem
787
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000788 public static int Rem1(int arg) {
789 return arg % 1;
790 }
791
David Brazdila06d66a2015-05-28 11:14:54 +0100792 /// CHECK-START: long Main.RemN1(long) constant_folding (before)
793 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
794 /// CHECK-DAG: <<ConstN1:j\d+>> LongConstant -1
795 /// CHECK-DAG: <<DivZeroCheck:j\d+>> DivZeroCheck [<<ConstN1>>]
796 /// CHECK-DAG: <<Rem:j\d+>> Rem [<<Arg>>,<<DivZeroCheck>>]
797 /// CHECK-DAG: Return [<<Rem>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000798
David Brazdila06d66a2015-05-28 11:14:54 +0100799 /// CHECK-START: long Main.RemN1(long) constant_folding (after)
800 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100801 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000802
Roland Levillainb65eb502015-07-23 12:11:42 +0100803 /// CHECK-START: long Main.RemN1(long) constant_folding (after)
804 /// CHECK-NOT: Rem
805
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000806 public static long RemN1(long arg) {
807 return arg % -1;
808 }
809
David Brazdila06d66a2015-05-28 11:14:54 +0100810 /// CHECK-START: int Main.Shl0(int) constant_folding (before)
811 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
812 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
813 /// CHECK-DAG: <<Shl:i\d+>> Shl [<<Const0>>,<<Arg>>]
814 /// CHECK-DAG: Return [<<Shl>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000815
David Brazdila06d66a2015-05-28 11:14:54 +0100816 /// CHECK-START: int Main.Shl0(int) constant_folding (after)
817 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
818 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100819 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000820
Roland Levillainb65eb502015-07-23 12:11:42 +0100821 /// CHECK-START: int Main.Shl0(int) constant_folding (after)
822 /// CHECK-NOT: Shl
823
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000824 public static int Shl0(int arg) {
825 return 0 << arg;
826 }
827
Roland Levillain9867bc72015-08-05 10:21:34 +0100828 /// CHECK-START: long Main.ShlLong0WithInt(int) constant_folding (before)
829 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
830 /// CHECK-DAG: <<Const0L:j\d+>> LongConstant 0
831 /// CHECK-DAG: <<Shl:j\d+>> Shl [<<Const0L>>,<<Arg>>]
832 /// CHECK-DAG: Return [<<Shl>>]
833
834 /// CHECK-START: long Main.ShlLong0WithInt(int) constant_folding (after)
835 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
836 /// CHECK-DAG: <<Const0L:j\d+>> LongConstant 0
837 /// CHECK-DAG: Return [<<Const0L>>]
838
839 /// CHECK-START: long Main.ShlLong0WithInt(int) constant_folding (after)
840 /// CHECK-NOT: Shl
841
842 public static long ShlLong0WithInt(int arg) {
843 long long_zero = 0;
844 return long_zero << arg;
845 }
846
David Brazdila06d66a2015-05-28 11:14:54 +0100847 /// CHECK-START: long Main.Shr0(int) constant_folding (before)
848 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
849 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
850 /// CHECK-DAG: <<Shr:j\d+>> Shr [<<Const0>>,<<Arg>>]
851 /// CHECK-DAG: Return [<<Shr>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000852
David Brazdila06d66a2015-05-28 11:14:54 +0100853 /// CHECK-START: long Main.Shr0(int) constant_folding (after)
854 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
855 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100856 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000857
Roland Levillainb65eb502015-07-23 12:11:42 +0100858 /// CHECK-START: long Main.Shr0(int) constant_folding (after)
859 /// CHECK-NOT: Shr
860
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000861 public static long Shr0(int arg) {
862 return (long)0 >> arg;
863 }
864
David Brazdila06d66a2015-05-28 11:14:54 +0100865 /// CHECK-START: long Main.SubSameLong(long) constant_folding (before)
866 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
867 /// CHECK-DAG: <<Sub:j\d+>> Sub [<<Arg>>,<<Arg>>]
868 /// CHECK-DAG: Return [<<Sub>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000869
David Brazdila06d66a2015-05-28 11:14:54 +0100870 /// CHECK-START: long Main.SubSameLong(long) constant_folding (after)
871 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
872 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100873 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000874
Roland Levillainb65eb502015-07-23 12:11:42 +0100875 /// CHECK-START: long Main.SubSameLong(long) constant_folding (after)
876 /// CHECK-NOT: Sub
877
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000878 public static long SubSameLong(long arg) {
879 return arg - arg;
880 }
881
David Brazdila06d66a2015-05-28 11:14:54 +0100882 /// CHECK-START: int Main.UShr0(int) constant_folding (before)
883 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
884 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
885 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Const0>>,<<Arg>>]
886 /// CHECK-DAG: Return [<<UShr>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000887
David Brazdila06d66a2015-05-28 11:14:54 +0100888 /// CHECK-START: int Main.UShr0(int) constant_folding (after)
889 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
890 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100891 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000892
Roland Levillainb65eb502015-07-23 12:11:42 +0100893 /// CHECK-START: int Main.UShr0(int) constant_folding (after)
894 /// CHECK-NOT: UShr
895
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000896 public static int UShr0(int arg) {
897 return 0 >>> arg;
898 }
899
David Brazdila06d66a2015-05-28 11:14:54 +0100900 /// CHECK-START: int Main.XorSameInt(int) constant_folding (before)
901 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
902 /// CHECK-DAG: <<Xor:i\d+>> Xor [<<Arg>>,<<Arg>>]
903 /// CHECK-DAG: Return [<<Xor>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000904
David Brazdila06d66a2015-05-28 11:14:54 +0100905 /// CHECK-START: int Main.XorSameInt(int) constant_folding (after)
906 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
907 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
David Brazdila06d66a2015-05-28 11:14:54 +0100908 /// CHECK-DAG: Return [<<Const0>>]
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000909
Roland Levillainb65eb502015-07-23 12:11:42 +0100910 /// CHECK-START: int Main.XorSameInt(int) constant_folding (after)
911 /// CHECK-NOT: Xor
912
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000913 public static int XorSameInt(int arg) {
914 return arg ^ arg;
915 }
916
David Brazdila06d66a2015-05-28 11:14:54 +0100917 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (before)
918 /// CHECK-DAG: <<Arg:f\d+>> ParameterValue
919 /// CHECK-DAG: <<ConstNan:f\d+>> FloatConstant nan
920 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
921 /// CHECK-DAG: IntConstant 1
922 /// CHECK-DAG: <<Cmp:i\d+>> Compare [<<Arg>>,<<ConstNan>>]
923 /// CHECK-DAG: <<Le:z\d+>> LessThanOrEqual [<<Cmp>>,<<Const0>>]
924 /// CHECK-DAG: If [<<Le>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100925
David Brazdila06d66a2015-05-28 11:14:54 +0100926 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (after)
927 /// CHECK-DAG: ParameterValue
928 /// CHECK-DAG: FloatConstant nan
929 /// CHECK-DAG: IntConstant 0
930 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
931 /// CHECK-DAG: If [<<Const1>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100932
David Brazdila06d66a2015-05-28 11:14:54 +0100933 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (after)
934 /// CHECK-NOT: Compare
935 /// CHECK-NOT: LessThanOrEqual
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100936
937 public static boolean CmpFloatGreaterThanNaN(float arg) {
938 return arg > Float.NaN;
939 }
940
David Brazdila06d66a2015-05-28 11:14:54 +0100941 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (before)
942 /// CHECK-DAG: <<Arg:d\d+>> ParameterValue
943 /// CHECK-DAG: <<ConstNan:d\d+>> DoubleConstant nan
944 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
945 /// CHECK-DAG: IntConstant 1
946 /// CHECK-DAG: <<Cmp:i\d+>> Compare [<<Arg>>,<<ConstNan>>]
947 /// CHECK-DAG: <<Ge:z\d+>> GreaterThanOrEqual [<<Cmp>>,<<Const0>>]
948 /// CHECK-DAG: If [<<Ge>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100949
David Brazdila06d66a2015-05-28 11:14:54 +0100950 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (after)
951 /// CHECK-DAG: ParameterValue
952 /// CHECK-DAG: DoubleConstant nan
953 /// CHECK-DAG: IntConstant 0
954 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
955 /// CHECK-DAG: If [<<Const1>>]
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100956
David Brazdila06d66a2015-05-28 11:14:54 +0100957 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (after)
958 /// CHECK-NOT: Compare
959 /// CHECK-NOT: GreaterThanOrEqual
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100960
961 public static boolean CmpDoubleLessThanNaN(double arg) {
962 return arg < Double.NaN;
963 }
964
Roland Levillainb65eb502015-07-23 12:11:42 +0100965
966 /**
967 * Exercise constant folding on type conversions.
968 */
969
David Brazdila06d66a2015-05-28 11:14:54 +0100970 /// CHECK-START: int Main.ReturnInt33() constant_folding (before)
971 /// CHECK-DAG: <<Const33:j\d+>> LongConstant 33
972 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<Const33>>]
973 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400974
David Brazdila06d66a2015-05-28 11:14:54 +0100975 /// CHECK-START: int Main.ReturnInt33() constant_folding (after)
976 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
977 /// CHECK-DAG: Return [<<Const33>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400978
Roland Levillainb65eb502015-07-23 12:11:42 +0100979 /// CHECK-START: int Main.ReturnInt33() constant_folding (after)
980 /// CHECK-NOT: TypeConversion
981
Mark Mendelle82549b2015-05-06 10:55:34 -0400982 public static int ReturnInt33() {
983 long imm = 33L;
984 return (int) imm;
985 }
986
David Brazdila06d66a2015-05-28 11:14:54 +0100987 /// CHECK-START: int Main.ReturnIntMax() constant_folding (before)
988 /// CHECK-DAG: <<ConstMax:f\d+>> FloatConstant 1e+34
989 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<ConstMax>>]
990 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400991
David Brazdila06d66a2015-05-28 11:14:54 +0100992 /// CHECK-START: int Main.ReturnIntMax() constant_folding (after)
993 /// CHECK-DAG: <<ConstMax:i\d+>> IntConstant 2147483647
994 /// CHECK-DAG: Return [<<ConstMax>>]
Mark Mendelle82549b2015-05-06 10:55:34 -0400995
Roland Levillainb65eb502015-07-23 12:11:42 +0100996 /// CHECK-START: int Main.ReturnIntMax() constant_folding (after)
997 /// CHECK-NOT: TypeConversion
998
Mark Mendelle82549b2015-05-06 10:55:34 -0400999 public static int ReturnIntMax() {
1000 float imm = 1.0e34f;
1001 return (int) imm;
1002 }
1003
David Brazdila06d66a2015-05-28 11:14:54 +01001004 /// CHECK-START: int Main.ReturnInt0() constant_folding (before)
1005 /// CHECK-DAG: <<ConstNaN:d\d+>> DoubleConstant nan
1006 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<ConstNaN>>]
1007 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001008
David Brazdila06d66a2015-05-28 11:14:54 +01001009 /// CHECK-START: int Main.ReturnInt0() constant_folding (after)
1010 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1011 /// CHECK-DAG: Return [<<Const0>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001012
Roland Levillainb65eb502015-07-23 12:11:42 +01001013 /// CHECK-START: int Main.ReturnInt0() constant_folding (after)
1014 /// CHECK-NOT: TypeConversion
1015
Mark Mendelle82549b2015-05-06 10:55:34 -04001016 public static int ReturnInt0() {
1017 double imm = Double.NaN;
1018 return (int) imm;
1019 }
1020
David Brazdila06d66a2015-05-28 11:14:54 +01001021 /// CHECK-START: long Main.ReturnLong33() constant_folding (before)
1022 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
1023 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<Const33>>]
1024 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001025
David Brazdila06d66a2015-05-28 11:14:54 +01001026 /// CHECK-START: long Main.ReturnLong33() constant_folding (after)
1027 /// CHECK-DAG: <<Const33:j\d+>> LongConstant 33
1028 /// CHECK-DAG: Return [<<Const33>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001029
Roland Levillainb65eb502015-07-23 12:11:42 +01001030 /// CHECK-START: long Main.ReturnLong33() constant_folding (after)
1031 /// CHECK-NOT: TypeConversion
1032
Mark Mendelle82549b2015-05-06 10:55:34 -04001033 public static long ReturnLong33() {
1034 int imm = 33;
1035 return (long) imm;
1036 }
1037
David Brazdila06d66a2015-05-28 11:14:54 +01001038 /// CHECK-START: long Main.ReturnLong34() constant_folding (before)
1039 /// CHECK-DAG: <<Const34:f\d+>> FloatConstant 34
1040 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<Const34>>]
1041 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001042
David Brazdila06d66a2015-05-28 11:14:54 +01001043 /// CHECK-START: long Main.ReturnLong34() constant_folding (after)
1044 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
1045 /// CHECK-DAG: Return [<<Const34>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001046
Roland Levillainb65eb502015-07-23 12:11:42 +01001047 /// CHECK-START: long Main.ReturnLong34() constant_folding (after)
1048 /// CHECK-NOT: TypeConversion
1049
Mark Mendelle82549b2015-05-06 10:55:34 -04001050 public static long ReturnLong34() {
1051 float imm = 34.0f;
1052 return (long) imm;
1053 }
1054
David Brazdila06d66a2015-05-28 11:14:54 +01001055 /// CHECK-START: long Main.ReturnLong0() constant_folding (before)
1056 /// CHECK-DAG: <<ConstNaN:d\d+>> DoubleConstant nan
1057 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<ConstNaN>>]
1058 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001059
David Brazdila06d66a2015-05-28 11:14:54 +01001060 /// CHECK-START: long Main.ReturnLong0() constant_folding (after)
1061 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1062 /// CHECK-DAG: Return [<<Const0>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001063
Roland Levillainb65eb502015-07-23 12:11:42 +01001064 /// CHECK-START: long Main.ReturnLong0() constant_folding (after)
1065 /// CHECK-NOT: TypeConversion
1066
Mark Mendelle82549b2015-05-06 10:55:34 -04001067 public static long ReturnLong0() {
1068 double imm = -Double.NaN;
1069 return (long) imm;
1070 }
1071
David Brazdila06d66a2015-05-28 11:14:54 +01001072 /// CHECK-START: float Main.ReturnFloat33() constant_folding (before)
1073 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
1074 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const33>>]
1075 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001076
David Brazdila06d66a2015-05-28 11:14:54 +01001077 /// CHECK-START: float Main.ReturnFloat33() constant_folding (after)
1078 /// CHECK-DAG: <<Const33:f\d+>> FloatConstant 33
1079 /// CHECK-DAG: Return [<<Const33>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001080
Roland Levillainb65eb502015-07-23 12:11:42 +01001081 /// CHECK-START: float Main.ReturnFloat33() constant_folding (after)
1082 /// CHECK-NOT: TypeConversion
1083
Mark Mendelle82549b2015-05-06 10:55:34 -04001084 public static float ReturnFloat33() {
1085 int imm = 33;
1086 return (float) imm;
1087 }
1088
David Brazdila06d66a2015-05-28 11:14:54 +01001089 /// CHECK-START: float Main.ReturnFloat34() constant_folding (before)
1090 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
1091 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const34>>]
1092 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001093
David Brazdila06d66a2015-05-28 11:14:54 +01001094 /// CHECK-START: float Main.ReturnFloat34() constant_folding (after)
1095 /// CHECK-DAG: <<Const34:f\d+>> FloatConstant 34
1096 /// CHECK-DAG: Return [<<Const34>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001097
Roland Levillainb65eb502015-07-23 12:11:42 +01001098 /// CHECK-START: float Main.ReturnFloat34() constant_folding (after)
1099 /// CHECK-NOT: TypeConversion
1100
Mark Mendelle82549b2015-05-06 10:55:34 -04001101 public static float ReturnFloat34() {
1102 long imm = 34L;
1103 return (float) imm;
1104 }
1105
David Brazdila06d66a2015-05-28 11:14:54 +01001106 /// CHECK-START: float Main.ReturnFloat99P25() constant_folding (before)
1107 /// CHECK-DAG: <<Const:d\d+>> DoubleConstant 99.25
1108 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const>>]
1109 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001110
David Brazdila06d66a2015-05-28 11:14:54 +01001111 /// CHECK-START: float Main.ReturnFloat99P25() constant_folding (after)
1112 /// CHECK-DAG: <<Const:f\d+>> FloatConstant 99.25
1113 /// CHECK-DAG: Return [<<Const>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001114
Roland Levillainb65eb502015-07-23 12:11:42 +01001115 /// CHECK-START: float Main.ReturnFloat99P25() constant_folding (after)
1116 /// CHECK-NOT: TypeConversion
1117
Mark Mendelle82549b2015-05-06 10:55:34 -04001118 public static float ReturnFloat99P25() {
1119 double imm = 99.25;
1120 return (float) imm;
1121 }
1122
David Brazdila06d66a2015-05-28 11:14:54 +01001123 /// CHECK-START: double Main.ReturnDouble33() constant_folding (before)
1124 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
1125 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const33>>]
1126 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001127
David Brazdila06d66a2015-05-28 11:14:54 +01001128 /// CHECK-START: double Main.ReturnDouble33() constant_folding (after)
1129 /// CHECK-DAG: <<Const33:d\d+>> DoubleConstant 33
1130 /// CHECK-DAG: Return [<<Const33>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001131
1132 public static double ReturnDouble33() {
1133 int imm = 33;
1134 return (double) imm;
1135 }
1136
David Brazdila06d66a2015-05-28 11:14:54 +01001137 /// CHECK-START: double Main.ReturnDouble34() constant_folding (before)
1138 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
1139 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const34>>]
1140 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001141
David Brazdila06d66a2015-05-28 11:14:54 +01001142 /// CHECK-START: double Main.ReturnDouble34() constant_folding (after)
1143 /// CHECK-DAG: <<Const34:d\d+>> DoubleConstant 34
1144 /// CHECK-DAG: Return [<<Const34>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001145
Roland Levillainb65eb502015-07-23 12:11:42 +01001146 /// CHECK-START: double Main.ReturnDouble34() constant_folding (after)
1147 /// CHECK-NOT: TypeConversion
1148
Mark Mendelle82549b2015-05-06 10:55:34 -04001149 public static double ReturnDouble34() {
1150 long imm = 34L;
1151 return (double) imm;
1152 }
1153
David Brazdila06d66a2015-05-28 11:14:54 +01001154 /// CHECK-START: double Main.ReturnDouble99P25() constant_folding (before)
1155 /// CHECK-DAG: <<Const:f\d+>> FloatConstant 99.25
1156 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const>>]
1157 /// CHECK-DAG: Return [<<Convert>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001158
David Brazdila06d66a2015-05-28 11:14:54 +01001159 /// CHECK-START: double Main.ReturnDouble99P25() constant_folding (after)
1160 /// CHECK-DAG: <<Const:d\d+>> DoubleConstant 99.25
1161 /// CHECK-DAG: Return [<<Const>>]
Mark Mendelle82549b2015-05-06 10:55:34 -04001162
Roland Levillainb65eb502015-07-23 12:11:42 +01001163 /// CHECK-START: double Main.ReturnDouble99P25() constant_folding (after)
1164 /// CHECK-NOT: TypeConversion
1165
Mark Mendelle82549b2015-05-06 10:55:34 -04001166 public static double ReturnDouble99P25() {
1167 float imm = 99.25f;
1168 return (double) imm;
1169 }
1170
Roland Levillainb65eb502015-07-23 12:11:42 +01001171
David Brazdil4846d132015-01-15 19:07:08 +00001172 public static void main(String[] args) {
Roland Levillainb65eb502015-07-23 12:11:42 +01001173 assertIntEquals(-42, IntNegation());
Roland Levillainc90bc7c2014-12-11 12:14:33 +00001174 assertLongEquals(-42L, LongNegation());
Roland Levillainb65eb502015-07-23 12:11:42 +01001175
1176 assertIntEquals(3, IntAddition1());
1177 assertIntEquals(14, IntAddition2());
1178 assertLongEquals(3L, LongAddition());
1179
1180 assertIntEquals(4, IntSubtraction());
1181 assertLongEquals(4L, LongSubtraction());
1182
Roland Levillainf7746ad2015-07-22 14:12:01 +01001183 assertIntEquals(21, IntMultiplication());
1184 assertLongEquals(21L, LongMultiplication());
1185
1186 assertIntEquals(2, IntDivision());
1187 assertLongEquals(2L, LongDivision());
1188
1189 assertIntEquals(2, IntRemainder());
1190 assertLongEquals(2L, LongRemainder());
1191
Roland Levillain9867bc72015-08-05 10:21:34 +01001192 assertIntEquals(4, ShlIntLong());
1193 assertLongEquals(12L, ShlLongInt());
1194
1195 assertIntEquals(1, ShrIntLong());
1196 assertLongEquals(2L, ShrLongInt());
1197
1198 assertIntEquals(1073741822, UShrIntLong());
1199 assertLongEquals(4611686018427387901L, UShrLongInt());
1200
1201 assertLongEquals(2, AndIntLong());
1202 assertLongEquals(2, AndLongInt());
1203
1204 assertLongEquals(11, OrIntLong());
1205 assertLongEquals(11, OrLongInt());
1206
1207 assertLongEquals(9, XorIntLong());
1208 assertLongEquals(9, XorLongInt());
1209
Roland Levillainb65eb502015-07-23 12:11:42 +01001210 assertIntEquals(5, StaticCondition());
1211
1212 assertIntEquals(7, JumpsAndConditionals(true));
1213 assertIntEquals(3, JumpsAndConditionals(false));
1214
Roland Levillain3b55ebb2015-05-08 13:13:19 +01001215 int arbitrary = 123456; // Value chosen arbitrarily.
Roland Levillainb65eb502015-07-23 12:11:42 +01001216
1217 assertIntEquals(0, And0(arbitrary));
1218 assertLongEquals(0, Mul0(arbitrary));
1219 assertIntEquals(-1, OrAllOnes(arbitrary));
1220 assertLongEquals(0, Rem0(arbitrary));
1221 assertIntEquals(0, Rem1(arbitrary));
1222 assertLongEquals(0, RemN1(arbitrary));
1223 assertIntEquals(0, Shl0(arbitrary));
Roland Levillain9867bc72015-08-05 10:21:34 +01001224 assertLongEquals(0, ShlLong0WithInt(arbitrary));
Roland Levillainb65eb502015-07-23 12:11:42 +01001225 assertLongEquals(0, Shr0(arbitrary));
1226 assertLongEquals(0, SubSameLong(arbitrary));
1227 assertIntEquals(0, UShr0(arbitrary));
1228 assertIntEquals(0, XorSameInt(arbitrary));
1229
Roland Levillain3b55ebb2015-05-08 13:13:19 +01001230 assertFalse(CmpFloatGreaterThanNaN(arbitrary));
1231 assertFalse(CmpDoubleLessThanNaN(arbitrary));
Roland Levillainb65eb502015-07-23 12:11:42 +01001232
1233 assertIntEquals(33, ReturnInt33());
1234 assertIntEquals(2147483647, ReturnIntMax());
1235 assertIntEquals(0, ReturnInt0());
1236
1237 assertLongEquals(33, ReturnLong33());
1238 assertLongEquals(34, ReturnLong34());
1239 assertLongEquals(0, ReturnLong0());
1240
1241 assertFloatEquals(33, ReturnFloat33());
1242 assertFloatEquals(34, ReturnFloat34());
1243 assertFloatEquals(99.25f, ReturnFloat99P25());
1244
1245 assertDoubleEquals(33, ReturnDouble33());
1246 assertDoubleEquals(34, ReturnDouble34());
1247 assertDoubleEquals(99.25, ReturnDouble99P25());
David Brazdil4846d132015-01-15 19:07:08 +00001248 }
David Brazdilee690a32014-12-01 17:04:16 +00001249}