Debian Codegraph @

test/442-checker-constant-folding/src/Main.java

raw
1 /*
2 * 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 */
16
17 import java.lang.reflect.Method;
18
19 public class Main {
20 public static void assertFalse(boolean condition) {
21 if (condition) {
22 throw new Error();
23 }
24 }
25
26 public static void assertIntEquals(int expected, int result) {
27 if (expected != result) {
28 throw new Error("Expected: " + expected + ", found: " + result);
29 }
30 }
31
32 public static void assertLongEquals(long expected, long result) {
33 if (expected != result) {
34 throw new Error("Expected: " + expected + ", found: " + result);
35 }
36 }
37
38 public static void assertFloatEquals(float expected, float result) {
39 if (expected != result) {
40 throw new Error("Expected: " + expected + ", found: " + result);
41 }
42 }
43
44 public static void assertDoubleEquals(double expected, double result) {
45 if (expected != result) {
46 throw new Error("Expected: " + expected + ", found: " + result);
47 }
48 }
49
50
51 // Wrappers around methods located in file TestCmp.smali.
52
53 public int smaliCmpLongConstants() throws Exception {
54 Method m = testCmp.getMethod("$opt$CmpLongConstants");
55 return (Integer)m.invoke(null);
56 }
57 public int smaliCmpGtFloatConstants() throws Exception {
58 Method m = testCmp.getMethod("$opt$CmpGtFloatConstants");
59 return (Integer)m.invoke(null);
60 }
61 public int smaliCmpLtFloatConstants() throws Exception {
62 Method m = testCmp.getMethod("$opt$CmpLtFloatConstants");
63 return (Integer)m.invoke(null);
64 }
65 public int smaliCmpGtDoubleConstants() throws Exception {
66 Method m = testCmp.getMethod("$opt$CmpGtDoubleConstants");
67 return (Integer)m.invoke(null);
68 }
69 public int smaliCmpLtDoubleConstants() throws Exception {
70 Method m = testCmp.getMethod("$opt$CmpLtDoubleConstants");
71 return (Integer)m.invoke(null);
72 }
73
74 public int smaliCmpLongSameConstant() throws Exception {
75 Method m = testCmp.getMethod("$opt$CmpLongSameConstant");
76 return (Integer)m.invoke(null);
77 }
78 public int smaliCmpGtFloatSameConstant() throws Exception {
79 Method m = testCmp.getMethod("$opt$CmpGtFloatSameConstant");
80 return (Integer)m.invoke(null);
81 }
82 public int smaliCmpLtFloatSameConstant() throws Exception {
83 Method m = testCmp.getMethod("$opt$CmpLtFloatSameConstant");
84 return (Integer)m.invoke(null);
85 }
86 public int smaliCmpGtDoubleSameConstant() throws Exception {
87 Method m = testCmp.getMethod("$opt$CmpGtDoubleSameConstant");
88 return (Integer)m.invoke(null);
89 }
90 public int smaliCmpLtDoubleSameConstant() throws Exception {
91 Method m = testCmp.getMethod("$opt$CmpLtDoubleSameConstant");
92 return (Integer)m.invoke(null);
93 }
94
95 public int smaliCmpGtFloatConstantWithNaN() throws Exception {
96 Method m = testCmp.getMethod("$opt$CmpGtFloatConstantWithNaN");
97 return (Integer)m.invoke(null);
98 }
99 public int smaliCmpLtFloatConstantWithNaN() throws Exception {
100 Method m = testCmp.getMethod("$opt$CmpLtFloatConstantWithNaN");
101 return (Integer)m.invoke(null);
102 }
103 public int smaliCmpGtDoubleConstantWithNaN() throws Exception {
104 Method m = testCmp.getMethod("$opt$CmpGtDoubleConstantWithNaN");
105 return (Integer)m.invoke(null);
106 }
107 public int smaliCmpLtDoubleConstantWithNaN() throws Exception {
108 Method m = testCmp.getMethod("$opt$CmpLtDoubleConstantWithNaN");
109 return (Integer)m.invoke(null);
110 }
111
112 public static int smaliIntAddition2() throws Exception {
113 Method m = Class.forName("TestCmp").getMethod("IntAddition2");
114 return (Integer)m.invoke(null);
115 }
116 public static int smaliIntAddition2AddAndMove() throws Exception {
117 Method m = Class.forName("TestCmp").getMethod("IntAddition2AddAndMove");
118 return (Integer)m.invoke(null);
119 }
120 public static int smaliJumpsAndConditionals(boolean cond) throws Exception {
121 Method m = Class.forName("TestCmp").getMethod("JumpsAndConditionals", boolean.class);
122 return (Integer)m.invoke(null, cond);
123 }
124
125
126 /**
127 * Exercise constant folding on negation.
128 */
129
130 /// CHECK-START: int Main.IntNegation() constant_folding (before)
131 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42
132 /// CHECK-DAG: <<Neg:i\d+>> Neg [<<Const42>>]
133 /// CHECK-DAG: Return [<<Neg>>]
134
135 /// CHECK-START: int Main.IntNegation() constant_folding (after)
136 /// CHECK-DAG: <<ConstN42:i\d+>> IntConstant -42
137 /// CHECK-DAG: Return [<<ConstN42>>]
138
139 /// CHECK-START: int Main.IntNegation() constant_folding (after)
140 /// CHECK-NOT: Neg
141
142 public static int IntNegation() {
143 int x, y;
144 x = 42;
145 y = -x;
146 return y;
147 }
148
149 /// CHECK-START: long Main.LongNegation() constant_folding (before)
150 /// CHECK-DAG: <<Const42:j\d+>> LongConstant 42
151 /// CHECK-DAG: <<Neg:j\d+>> Neg [<<Const42>>]
152 /// CHECK-DAG: Return [<<Neg>>]
153
154 /// CHECK-START: long Main.LongNegation() constant_folding (after)
155 /// CHECK-DAG: <<ConstN42:j\d+>> LongConstant -42
156 /// CHECK-DAG: Return [<<ConstN42>>]
157
158 /// CHECK-START: long Main.LongNegation() constant_folding (after)
159 /// CHECK-NOT: Neg
160
161 public static long LongNegation() {
162 long x, y;
163 x = 42L;
164 y = -x;
165 return y;
166 }
167
168 /// CHECK-START: float Main.FloatNegation() constant_folding (before)
169 /// CHECK-DAG: <<Const42:f\d+>> FloatConstant 42
170 /// CHECK-DAG: <<Neg:f\d+>> Neg [<<Const42>>]
171 /// CHECK-DAG: Return [<<Neg>>]
172
173 /// CHECK-START: float Main.FloatNegation() constant_folding (after)
174 /// CHECK-DAG: <<ConstN42:f\d+>> FloatConstant -42
175 /// CHECK-DAG: Return [<<ConstN42>>]
176
177 /// CHECK-START: float Main.FloatNegation() constant_folding (after)
178 /// CHECK-NOT: Neg
179
180 public static float FloatNegation() {
181 float x, y;
182 x = 42F;
183 y = -x;
184 return y;
185 }
186
187 /// CHECK-START: double Main.DoubleNegation() constant_folding (before)
188 /// CHECK-DAG: <<Const42:d\d+>> DoubleConstant 42
189 /// CHECK-DAG: <<Neg:d\d+>> Neg [<<Const42>>]
190 /// CHECK-DAG: Return [<<Neg>>]
191
192 /// CHECK-START: double Main.DoubleNegation() constant_folding (after)
193 /// CHECK-DAG: <<ConstN42:d\d+>> DoubleConstant -42
194 /// CHECK-DAG: Return [<<ConstN42>>]
195
196 /// CHECK-START: double Main.DoubleNegation() constant_folding (after)
197 /// CHECK-NOT: Neg
198
199 public static double DoubleNegation() {
200 double x, y;
201 x = 42D;
202 y = -x;
203 return y;
204 }
205
206
207 /**
208 * Exercise constant folding on addition.
209 */
210
211 /// CHECK-START: int Main.IntAddition1() constant_folding (before)
212 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
213 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
214 /// CHECK-DAG: <<Add:i\d+>> Add [<<Const1>>,<<Const2>>]
215 /// CHECK-DAG: Return [<<Add>>]
216
217 /// CHECK-START: int Main.IntAddition1() constant_folding (after)
218 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
219 /// CHECK-DAG: Return [<<Const3>>]
220
221 /// CHECK-START: int Main.IntAddition1() constant_folding (after)
222 /// CHECK-NOT: Add
223
224 public static int IntAddition1() {
225 int a, b, c;
226 a = 1;
227 b = 2;
228 c = a + b;
229 return c;
230 }
231
232 /// CHECK-START: int Main.IntAddition2() constant_folding (before)
233 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
234 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
235 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
236 /// CHECK-DAG: <<Const6:i\d+>> IntConstant 6
237 /// CHECK-DAG: <<Add1:i\d+>> Add [<<Const1>>,<<Const2>>]
238 /// CHECK-DAG: Add [<<Const5>>,<<Const6>>]
239
240 /// CHECK-START: int Main.IntAddition2() constant_folding (after)
241 /// CHECK-DAG: <<Const14:i\d+>> IntConstant 14
242 /// CHECK-DAG: Return [<<Const14>>]
243
244 /// CHECK-START: int Main.IntAddition2() constant_folding (after)
245 /// CHECK-NOT: Add
246
247 public static int IntAddition2() {
248 int a, b, c;
249 a = 1;
250 b = 2;
251 a += b;
252 b = 5;
253 c = 6;
254 b += c;
255 c = a + b;
256 return c;
257 }
258
259 /// CHECK-START: long Main.LongAddition() constant_folding (before)
260 /// CHECK-DAG: <<Const1:j\d+>> LongConstant 1
261 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
262 /// CHECK-DAG: <<Add:j\d+>> Add [<<Const1>>,<<Const2>>]
263 /// CHECK-DAG: Return [<<Add>>]
264
265 /// CHECK-START: long Main.LongAddition() constant_folding (after)
266 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
267 /// CHECK-DAG: Return [<<Const3>>]
268
269 /// CHECK-START: long Main.LongAddition() constant_folding (after)
270 /// CHECK-NOT: Add
271
272 public static long LongAddition() {
273 long a, b, c;
274 a = 1L;
275 b = 2L;
276 c = a + b;
277 return c;
278 }
279
280 /// CHECK-START: float Main.FloatAddition() constant_folding (before)
281 /// CHECK-DAG: <<Const1:f\d+>> FloatConstant 1
282 /// CHECK-DAG: <<Const2:f\d+>> FloatConstant 2
283 /// CHECK-DAG: <<Add:f\d+>> Add [<<Const1>>,<<Const2>>]
284 /// CHECK-DAG: Return [<<Add>>]
285
286 /// CHECK-START: float Main.FloatAddition() constant_folding (after)
287 /// CHECK-DAG: <<Const3:f\d+>> FloatConstant 3
288 /// CHECK-DAG: Return [<<Const3>>]
289
290 /// CHECK-START: float Main.FloatAddition() constant_folding (after)
291 /// CHECK-NOT: Add
292
293 public static float FloatAddition() {
294 float a, b, c;
295 a = 1F;
296 b = 2F;
297 c = a + b;
298 return c;
299 }
300
301 /// CHECK-START: double Main.DoubleAddition() constant_folding (before)
302 /// CHECK-DAG: <<Const1:d\d+>> DoubleConstant 1
303 /// CHECK-DAG: <<Const2:d\d+>> DoubleConstant 2
304 /// CHECK-DAG: <<Add:d\d+>> Add [<<Const1>>,<<Const2>>]
305 /// CHECK-DAG: Return [<<Add>>]
306
307 /// CHECK-START: double Main.DoubleAddition() constant_folding (after)
308 /// CHECK-DAG: <<Const3:d\d+>> DoubleConstant 3
309 /// CHECK-DAG: Return [<<Const3>>]
310
311 /// CHECK-START: double Main.DoubleAddition() constant_folding (after)
312 /// CHECK-NOT: Add
313
314 public static double DoubleAddition() {
315 double a, b, c;
316 a = 1D;
317 b = 2D;
318 c = a + b;
319 return c;
320 }
321
322
323 /**
324 * Exercise constant folding on subtraction.
325 */
326
327 /// CHECK-START: int Main.IntSubtraction() constant_folding (before)
328 /// CHECK-DAG: <<Const6:i\d+>> IntConstant 6
329 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
330 /// CHECK-DAG: <<Sub:i\d+>> Sub [<<Const6>>,<<Const2>>]
331 /// CHECK-DAG: Return [<<Sub>>]
332
333 /// CHECK-START: int Main.IntSubtraction() constant_folding (after)
334 /// CHECK-DAG: <<Const4:i\d+>> IntConstant 4
335 /// CHECK-DAG: Return [<<Const4>>]
336
337 /// CHECK-START: int Main.IntSubtraction() constant_folding (after)
338 /// CHECK-NOT: Sub
339
340 public static int IntSubtraction() {
341 int a, b, c;
342 a = 6;
343 b = 2;
344 c = a - b;
345 return c;
346 }
347
348 /// CHECK-START: long Main.LongSubtraction() constant_folding (before)
349 /// CHECK-DAG: <<Const6:j\d+>> LongConstant 6
350 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
351 /// CHECK-DAG: <<Sub:j\d+>> Sub [<<Const6>>,<<Const2>>]
352 /// CHECK-DAG: Return [<<Sub>>]
353
354 /// CHECK-START: long Main.LongSubtraction() constant_folding (after)
355 /// CHECK-DAG: <<Const4:j\d+>> LongConstant 4
356 /// CHECK-DAG: Return [<<Const4>>]
357
358 /// CHECK-START: long Main.LongSubtraction() constant_folding (after)
359 /// CHECK-NOT: Sub
360
361 public static long LongSubtraction() {
362 long a, b, c;
363 a = 6L;
364 b = 2L;
365 c = a - b;
366 return c;
367 }
368
369 /// CHECK-START: float Main.FloatSubtraction() constant_folding (before)
370 /// CHECK-DAG: <<Const6:f\d+>> FloatConstant 6
371 /// CHECK-DAG: <<Const2:f\d+>> FloatConstant 2
372 /// CHECK-DAG: <<Sub:f\d+>> Sub [<<Const6>>,<<Const2>>]
373 /// CHECK-DAG: Return [<<Sub>>]
374
375 /// CHECK-START: float Main.FloatSubtraction() constant_folding (after)
376 /// CHECK-DAG: <<Const4:f\d+>> FloatConstant 4
377 /// CHECK-DAG: Return [<<Const4>>]
378
379 /// CHECK-START: float Main.FloatSubtraction() constant_folding (after)
380 /// CHECK-NOT: Sub
381
382 public static float FloatSubtraction() {
383 float a, b, c;
384 a = 6F;
385 b = 2F;
386 c = a - b;
387 return c;
388 }
389
390 /// CHECK-START: double Main.DoubleSubtraction() constant_folding (before)
391 /// CHECK-DAG: <<Const6:d\d+>> DoubleConstant 6
392 /// CHECK-DAG: <<Const2:d\d+>> DoubleConstant 2
393 /// CHECK-DAG: <<Sub:d\d+>> Sub [<<Const6>>,<<Const2>>]
394 /// CHECK-DAG: Return [<<Sub>>]
395
396 /// CHECK-START: double Main.DoubleSubtraction() constant_folding (after)
397 /// CHECK-DAG: <<Const4:d\d+>> DoubleConstant 4
398 /// CHECK-DAG: Return [<<Const4>>]
399
400 /// CHECK-START: double Main.DoubleSubtraction() constant_folding (after)
401 /// CHECK-NOT: Sub
402
403 public static double DoubleSubtraction() {
404 double a, b, c;
405 a = 6D;
406 b = 2D;
407 c = a - b;
408 return c;
409 }
410
411
412 /**
413 * Exercise constant folding on multiplication.
414 */
415
416 /// CHECK-START: int Main.IntMultiplication() constant_folding (before)
417 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
418 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
419 /// CHECK-DAG: <<Mul:i\d+>> Mul [<<Const7>>,<<Const3>>]
420 /// CHECK-DAG: Return [<<Mul>>]
421
422 /// CHECK-START: int Main.IntMultiplication() constant_folding (after)
423 /// CHECK-DAG: <<Const21:i\d+>> IntConstant 21
424 /// CHECK-DAG: Return [<<Const21>>]
425
426 /// CHECK-START: int Main.IntMultiplication() constant_folding (after)
427 /// CHECK-NOT: Mul
428
429 public static int IntMultiplication() {
430 int a, b, c;
431 a = 7;
432 b = 3;
433 c = a * b;
434 return c;
435 }
436
437 /// CHECK-START: long Main.LongMultiplication() constant_folding (before)
438 /// CHECK-DAG: <<Const7:j\d+>> LongConstant 7
439 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
440 /// CHECK-DAG: <<Mul:j\d+>> Mul [<<Const7>>,<<Const3>>]
441 /// CHECK-DAG: Return [<<Mul>>]
442
443 /// CHECK-START: long Main.LongMultiplication() constant_folding (after)
444 /// CHECK-DAG: <<Const21:j\d+>> LongConstant 21
445 /// CHECK-DAG: Return [<<Const21>>]
446
447 /// CHECK-START: long Main.LongMultiplication() constant_folding (after)
448 /// CHECK-NOT: Mul
449
450 public static long LongMultiplication() {
451 long a, b, c;
452 a = 7L;
453 b = 3L;
454 c = a * b;
455 return c;
456 }
457
458 /// CHECK-START: float Main.FloatMultiplication() constant_folding (before)
459 /// CHECK-DAG: <<Const7:f\d+>> FloatConstant 7
460 /// CHECK-DAG: <<Const3:f\d+>> FloatConstant 3
461 /// CHECK-DAG: <<Mul:f\d+>> Mul [<<Const7>>,<<Const3>>]
462 /// CHECK-DAG: Return [<<Mul>>]
463
464 /// CHECK-START: float Main.FloatMultiplication() constant_folding (after)
465 /// CHECK-DAG: <<Const21:f\d+>> FloatConstant 21
466 /// CHECK-DAG: Return [<<Const21>>]
467
468 /// CHECK-START: float Main.FloatMultiplication() constant_folding (after)
469 /// CHECK-NOT: Mul
470
471 public static float FloatMultiplication() {
472 float a, b, c;
473 a = 7F;
474 b = 3F;
475 c = a * b;
476 return c;
477 }
478
479 /// CHECK-START: double Main.DoubleMultiplication() constant_folding (before)
480 /// CHECK-DAG: <<Const7:d\d+>> DoubleConstant 7
481 /// CHECK-DAG: <<Const3:d\d+>> DoubleConstant 3
482 /// CHECK-DAG: <<Mul:d\d+>> Mul [<<Const7>>,<<Const3>>]
483 /// CHECK-DAG: Return [<<Mul>>]
484
485 /// CHECK-START: double Main.DoubleMultiplication() constant_folding (after)
486 /// CHECK-DAG: <<Const21:d\d+>> DoubleConstant 21
487 /// CHECK-DAG: Return [<<Const21>>]
488
489 /// CHECK-START: double Main.DoubleMultiplication() constant_folding (after)
490 /// CHECK-NOT: Mul
491
492 public static double DoubleMultiplication() {
493 double a, b, c;
494 a = 7D;
495 b = 3D;
496 c = a * b;
497 return c;
498 }
499
500
501 /**
502 * Exercise constant folding on division.
503 */
504
505 /// CHECK-START: int Main.IntDivision() constant_folding (before)
506 /// CHECK-DAG: <<Const8:i\d+>> IntConstant 8
507 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
508 /// CHECK-DAG: <<Div0Chk:i\d+>> DivZeroCheck [<<Const3>>]
509 /// CHECK-DAG: <<Div:i\d+>> Div [<<Const8>>,<<Div0Chk>>]
510 /// CHECK-DAG: Return [<<Div>>]
511
512 /// CHECK-START: int Main.IntDivision() constant_folding (after)
513 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
514 /// CHECK-DAG: Return [<<Const2>>]
515
516 /// CHECK-START: int Main.IntDivision() constant_folding (after)
517 /// CHECK-NOT: DivZeroCheck
518 /// CHECK-NOT: Div
519
520 public static int IntDivision() {
521 int a, b, c;
522 a = 8;
523 b = 3;
524 c = a / b;
525 return c;
526 }
527
528 /// CHECK-START: long Main.LongDivision() constant_folding (before)
529 /// CHECK-DAG: <<Const8:j\d+>> LongConstant 8
530 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
531 /// CHECK-DAG: <<Div0Chk:j\d+>> DivZeroCheck [<<Const3>>]
532 /// CHECK-DAG: <<Div:j\d+>> Div [<<Const8>>,<<Div0Chk>>]
533 /// CHECK-DAG: Return [<<Div>>]
534
535 /// CHECK-START: long Main.LongDivision() constant_folding (after)
536 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
537 /// CHECK-DAG: Return [<<Const2>>]
538
539 /// CHECK-START: long Main.LongDivision() constant_folding (after)
540 /// CHECK-NOT: DivZeroCheck
541 /// CHECK-NOT: Div
542
543 public static long LongDivision() {
544 long a, b, c;
545 a = 8L;
546 b = 3L;
547 c = a / b;
548 return c;
549 }
550
551 /// CHECK-START: float Main.FloatDivision() constant_folding (before)
552 /// CHECK-DAG: <<Const8:f\d+>> FloatConstant 8
553 /// CHECK-DAG: <<Const2P5:f\d+>> FloatConstant 2.5
554 /// CHECK-DAG: <<Div:f\d+>> Div [<<Const8>>,<<Const2P5>>]
555 /// CHECK-DAG: Return [<<Div>>]
556
557 /// CHECK-START: float Main.FloatDivision() constant_folding (after)
558 /// CHECK-DAG: <<Const3P2:f\d+>> FloatConstant 3.2
559 /// CHECK-DAG: Return [<<Const3P2>>]
560
561 /// CHECK-START: float Main.FloatDivision() constant_folding (after)
562 /// CHECK-NOT: Div
563
564 public static float FloatDivision() {
565 float a, b, c;
566 a = 8F;
567 b = 2.5F;
568 c = a / b;
569 return c;
570 }
571
572 /// CHECK-START: double Main.DoubleDivision() constant_folding (before)
573 /// CHECK-DAG: <<Const8:d\d+>> DoubleConstant 8
574 /// CHECK-DAG: <<Const2P5:d\d+>> DoubleConstant 2.5
575 /// CHECK-DAG: <<Div:d\d+>> Div [<<Const8>>,<<Const2P5>>]
576 /// CHECK-DAG: Return [<<Div>>]
577
578 /// CHECK-START: double Main.DoubleDivision() constant_folding (after)
579 /// CHECK-DAG: <<Const3P2:d\d+>> DoubleConstant 3.2
580 /// CHECK-DAG: Return [<<Const3P2>>]
581
582 /// CHECK-START: double Main.DoubleDivision() constant_folding (after)
583 /// CHECK-NOT: Div
584
585 public static double DoubleDivision() {
586 double a, b, c;
587 a = 8D;
588 b = 2.5D;
589 c = a / b;
590 return c;
591 }
592
593
594 /**
595 * Exercise constant folding on remainder.
596 */
597
598 /// CHECK-START: int Main.IntRemainder() constant_folding (before)
599 /// CHECK-DAG: <<Const8:i\d+>> IntConstant 8
600 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
601 /// CHECK-DAG: <<Div0Chk:i\d+>> DivZeroCheck [<<Const3>>]
602 /// CHECK-DAG: <<Rem:i\d+>> Rem [<<Const8>>,<<Div0Chk>>]
603 /// CHECK-DAG: Return [<<Rem>>]
604
605 /// CHECK-START: int Main.IntRemainder() constant_folding (after)
606 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
607 /// CHECK-DAG: Return [<<Const2>>]
608
609 /// CHECK-START: int Main.IntRemainder() constant_folding (after)
610 /// CHECK-NOT: DivZeroCheck
611 /// CHECK-NOT: Rem
612
613 public static int IntRemainder() {
614 int a, b, c;
615 a = 8;
616 b = 3;
617 c = a % b;
618 return c;
619 }
620
621 /// CHECK-START: long Main.LongRemainder() constant_folding (before)
622 /// CHECK-DAG: <<Const8:j\d+>> LongConstant 8
623 /// CHECK-DAG: <<Const3:j\d+>> LongConstant 3
624 /// CHECK-DAG: <<Div0Chk:j\d+>> DivZeroCheck [<<Const3>>]
625 /// CHECK-DAG: <<Rem:j\d+>> Rem [<<Const8>>,<<Div0Chk>>]
626 /// CHECK-DAG: Return [<<Rem>>]
627
628 /// CHECK-START: long Main.LongRemainder() constant_folding (after)
629 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
630 /// CHECK-DAG: Return [<<Const2>>]
631
632 /// CHECK-START: long Main.LongRemainder() constant_folding (after)
633 /// CHECK-NOT: DivZeroCheck
634 /// CHECK-NOT: Rem
635
636 public static long LongRemainder() {
637 long a, b, c;
638 a = 8L;
639 b = 3L;
640 c = a % b;
641 return c;
642 }
643
644 /// CHECK-START: float Main.FloatRemainder() constant_folding (before)
645 /// CHECK-DAG: <<Const8:f\d+>> FloatConstant 8
646 /// CHECK-DAG: <<Const2P5:f\d+>> FloatConstant 2.5
647 /// CHECK-DAG: <<Rem:f\d+>> Rem [<<Const8>>,<<Const2P5>>]
648 /// CHECK-DAG: Return [<<Rem>>]
649
650 /// CHECK-START: float Main.FloatRemainder() constant_folding (after)
651 /// CHECK-DAG: <<Const0P5:f\d+>> FloatConstant 0.5
652 /// CHECK-DAG: Return [<<Const0P5>>]
653
654 /// CHECK-START: float Main.FloatRemainder() constant_folding (after)
655 /// CHECK-NOT: Rem
656
657 public static float FloatRemainder() {
658 float a, b, c;
659 a = 8F;
660 b = 2.5F;
661 c = a % b;
662 return c;
663 }
664
665 /// CHECK-START: double Main.DoubleRemainder() constant_folding (before)
666 /// CHECK-DAG: <<Const8:d\d+>> DoubleConstant 8
667 /// CHECK-DAG: <<Const2P5:d\d+>> DoubleConstant 2.5
668 /// CHECK-DAG: <<Rem:d\d+>> Rem [<<Const8>>,<<Const2P5>>]
669 /// CHECK-DAG: Return [<<Rem>>]
670
671 /// CHECK-START: double Main.DoubleRemainder() constant_folding (after)
672 /// CHECK-DAG: <<Const0P5:d\d+>> DoubleConstant 0.5
673 /// CHECK-DAG: Return [<<Const0P5>>]
674
675 /// CHECK-START: double Main.DoubleRemainder() constant_folding (after)
676 /// CHECK-NOT: Rem
677
678 public static double DoubleRemainder() {
679 double a, b, c;
680 a = 8D;
681 b = 2.5D;
682 c = a % b;
683 return c;
684 }
685
686
687 /**
688 * Exercise constant folding on left shift.
689 */
690
691 /// CHECK-START: int Main.ShlIntLong() constant_folding (before)
692 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
693 /// CHECK-DAG: <<Const2L:j\d+>> LongConstant 2
694 /// CHECK-DAG: <<TypeConv:i\d+>> TypeConversion [<<Const2L>>]
695 /// CHECK-DAG: <<Shl:i\d+>> Shl [<<Const1>>,<<TypeConv>>]
696 /// CHECK-DAG: Return [<<Shl>>]
697
698 /// CHECK-START: int Main.ShlIntLong() constant_folding (after)
699 /// CHECK-DAG: <<Const4:i\d+>> IntConstant 4
700 /// CHECK-DAG: Return [<<Const4>>]
701
702 /// CHECK-START: int Main.ShlIntLong() constant_folding (after)
703 /// CHECK-NOT: Shl
704
705 public static int ShlIntLong() {
706 int lhs = 1;
707 long rhs = 2;
708 return lhs << rhs;
709 }
710
711 /// CHECK-START: long Main.ShlLongInt() constant_folding (before)
712 /// CHECK-DAG: <<Const3L:j\d+>> LongConstant 3
713 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
714 /// CHECK-DAG: <<Shl:j\d+>> Shl [<<Const3L>>,<<Const2>>]
715 /// CHECK-DAG: Return [<<Shl>>]
716
717 /// CHECK-START: long Main.ShlLongInt() constant_folding (after)
718 /// CHECK-DAG: <<Const12L:j\d+>> LongConstant 12
719 /// CHECK-DAG: Return [<<Const12L>>]
720
721 /// CHECK-START: long Main.ShlLongInt() constant_folding (after)
722 /// CHECK-NOT: Shl
723
724 public static long ShlLongInt() {
725 long lhs = 3;
726 int rhs = 2;
727 return lhs << rhs;
728 }
729
730
731 /**
732 * Exercise constant folding on right shift.
733 */
734
735 /// CHECK-START: int Main.ShrIntLong() constant_folding (before)
736 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
737 /// CHECK-DAG: <<Const2L:j\d+>> LongConstant 2
738 /// CHECK-DAG: <<TypeConv:i\d+>> TypeConversion [<<Const2L>>]
739 /// CHECK-DAG: <<Shr:i\d+>> Shr [<<Const7>>,<<TypeConv>>]
740 /// CHECK-DAG: Return [<<Shr>>]
741
742 /// CHECK-START: int Main.ShrIntLong() constant_folding (after)
743 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
744 /// CHECK-DAG: Return [<<Const1>>]
745
746 /// CHECK-START: int Main.ShrIntLong() constant_folding (after)
747 /// CHECK-NOT: Shr
748
749 public static int ShrIntLong() {
750 int lhs = 7;
751 long rhs = 2;
752 return lhs >> rhs;
753 }
754
755 /// CHECK-START: long Main.ShrLongInt() constant_folding (before)
756 /// CHECK-DAG: <<Const9L:j\d+>> LongConstant 9
757 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
758 /// CHECK-DAG: <<Shr:j\d+>> Shr [<<Const9L>>,<<Const2>>]
759 /// CHECK-DAG: Return [<<Shr>>]
760
761 /// CHECK-START: long Main.ShrLongInt() constant_folding (after)
762 /// CHECK-DAG: <<Const2L:j\d+>> LongConstant 2
763 /// CHECK-DAG: Return [<<Const2L>>]
764
765 /// CHECK-START: long Main.ShrLongInt() constant_folding (after)
766 /// CHECK-NOT: Shr
767
768 public static long ShrLongInt() {
769 long lhs = 9;
770 int rhs = 2;
771 return lhs >> rhs;
772 }
773
774
775 /**
776 * Exercise constant folding on unsigned right shift.
777 */
778
779 /// CHECK-START: int Main.UShrIntLong() constant_folding (before)
780 /// CHECK-DAG: <<ConstM7:i\d+>> IntConstant -7
781 /// CHECK-DAG: <<Const2L:j\d+>> LongConstant 2
782 /// CHECK-DAG: <<TypeConv:i\d+>> TypeConversion [<<Const2L>>]
783 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<ConstM7>>,<<TypeConv>>]
784 /// CHECK-DAG: Return [<<UShr>>]
785
786 /// CHECK-START: int Main.UShrIntLong() constant_folding (after)
787 /// CHECK-DAG: <<ConstRes:i\d+>> IntConstant 1073741822
788 /// CHECK-DAG: Return [<<ConstRes>>]
789
790 /// CHECK-START: int Main.UShrIntLong() constant_folding (after)
791 /// CHECK-NOT: UShr
792
793 public static int UShrIntLong() {
794 int lhs = -7;
795 long rhs = 2;
796 return lhs >>> rhs;
797 }
798
799 /// CHECK-START: long Main.UShrLongInt() constant_folding (before)
800 /// CHECK-DAG: <<ConstM9L:j\d+>> LongConstant -9
801 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
802 /// CHECK-DAG: <<UShr:j\d+>> UShr [<<ConstM9L>>,<<Const2>>]
803 /// CHECK-DAG: Return [<<UShr>>]
804
805 /// CHECK-START: long Main.UShrLongInt() constant_folding (after)
806 /// CHECK-DAG: <<ConstRes:j\d+>> LongConstant 4611686018427387901
807 /// CHECK-DAG: Return [<<ConstRes>>]
808
809 /// CHECK-START: long Main.UShrLongInt() constant_folding (after)
810 /// CHECK-NOT: UShr
811
812 public static long UShrLongInt() {
813 long lhs = -9;
814 int rhs = 2;
815 return lhs >>> rhs;
816 }
817
818
819 /**
820 * Exercise constant folding on logical and.
821 */
822
823 /// CHECK-START: long Main.AndIntLong() constant_folding (before)
824 /// CHECK-DAG: <<Const10:i\d+>> IntConstant 10
825 /// CHECK-DAG: <<Const3L:j\d+>> LongConstant 3
826 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const10>>]
827 /// CHECK-DAG: <<And:j\d+>> And [<<TypeConv>>,<<Const3L>>]
828 /// CHECK-DAG: Return [<<And>>]
829
830 /// CHECK-START: long Main.AndIntLong() constant_folding (after)
831 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
832 /// CHECK-DAG: Return [<<Const2>>]
833
834 /// CHECK-START: long Main.AndIntLong() constant_folding (after)
835 /// CHECK-NOT: And
836
837 public static long AndIntLong() {
838 int lhs = 10;
839 long rhs = 3;
840 return lhs & rhs;
841 }
842
843 /// CHECK-START: long Main.AndLongInt() constant_folding (before)
844 /// CHECK-DAG: <<Const10L:j\d+>> LongConstant 10
845 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
846 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const3>>]
847 /// CHECK-DAG: <<And:j\d+>> And [<<TypeConv>>,<<Const10L>>]
848 /// CHECK-DAG: Return [<<And>>]
849
850 /// CHECK-START: long Main.AndLongInt() constant_folding (after)
851 /// CHECK-DAG: <<Const2:j\d+>> LongConstant 2
852 /// CHECK-DAG: Return [<<Const2>>]
853
854 /// CHECK-START: long Main.AndLongInt() constant_folding (after)
855 /// CHECK-NOT: And
856
857 public static long AndLongInt() {
858 long lhs = 10;
859 int rhs = 3;
860 return lhs & rhs;
861 }
862
863 /// CHECK-START: int Main.AndSelfNegated(int) constant_folding (before)
864 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
865 /// CHECK-DAG: <<Not:i\d+>> Not [<<Arg>>]
866 /// CHECK-DAG: <<And:i\d+>> And [<<Not>>,<<Arg>>]
867 /// CHECK-DAG: Return [<<And>>]
868
869 /// CHECK-START: int Main.AndSelfNegated(int) constant_folding (after)
870 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
871 /// CHECK-DAG: Return [<<Const0>>]
872
873 /// CHECK-START: int Main.AndSelfNegated(int) constant_folding (after)
874 /// CHECK-NOT: And
875
876 public static int AndSelfNegated(int arg) {
877 return arg & ~arg;
878 }
879
880
881 /**
882 * Exercise constant folding on logical or.
883 */
884
885 /// CHECK-START: long Main.OrIntLong() constant_folding (before)
886 /// CHECK-DAG: <<Const10:i\d+>> IntConstant 10
887 /// CHECK-DAG: <<Const3L:j\d+>> LongConstant 3
888 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const10>>]
889 /// CHECK-DAG: <<Or:j\d+>> Or [<<TypeConv>>,<<Const3L>>]
890 /// CHECK-DAG: Return [<<Or>>]
891
892 /// CHECK-START: long Main.OrIntLong() constant_folding (after)
893 /// CHECK-DAG: <<Const11:j\d+>> LongConstant 11
894 /// CHECK-DAG: Return [<<Const11>>]
895
896 /// CHECK-START: long Main.OrIntLong() constant_folding (after)
897 /// CHECK-NOT: Or
898
899 public static long OrIntLong() {
900 int lhs = 10;
901 long rhs = 3;
902 return lhs | rhs;
903 }
904
905 /// CHECK-START: long Main.OrLongInt() constant_folding (before)
906 /// CHECK-DAG: <<Const10L:j\d+>> LongConstant 10
907 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
908 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const3>>]
909 /// CHECK-DAG: <<Or:j\d+>> Or [<<TypeConv>>,<<Const10L>>]
910 /// CHECK-DAG: Return [<<Or>>]
911
912 /// CHECK-START: long Main.OrLongInt() constant_folding (after)
913 /// CHECK-DAG: <<Const11:j\d+>> LongConstant 11
914 /// CHECK-DAG: Return [<<Const11>>]
915
916 /// CHECK-START: long Main.OrLongInt() constant_folding (after)
917 /// CHECK-NOT: Or
918
919 public static long OrLongInt() {
920 long lhs = 10;
921 int rhs = 3;
922 return lhs | rhs;
923 }
924
925
926 /**
927 * Exercise constant folding on logical exclusive or.
928 */
929
930 /// CHECK-START: long Main.XorIntLong() constant_folding (before)
931 /// CHECK-DAG: <<Const10:i\d+>> IntConstant 10
932 /// CHECK-DAG: <<Const3L:j\d+>> LongConstant 3
933 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const10>>]
934 /// CHECK-DAG: <<Xor:j\d+>> Xor [<<TypeConv>>,<<Const3L>>]
935 /// CHECK-DAG: Return [<<Xor>>]
936
937 /// CHECK-START: long Main.XorIntLong() constant_folding (after)
938 /// CHECK-DAG: <<Const9:j\d+>> LongConstant 9
939 /// CHECK-DAG: Return [<<Const9>>]
940
941 /// CHECK-START: long Main.XorIntLong() constant_folding (after)
942 /// CHECK-NOT: Xor
943
944 public static long XorIntLong() {
945 int lhs = 10;
946 long rhs = 3;
947 return lhs ^ rhs;
948 }
949
950 /// CHECK-START: long Main.XorLongInt() constant_folding (before)
951 /// CHECK-DAG: <<Const10L:j\d+>> LongConstant 10
952 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
953 /// CHECK-DAG: <<TypeConv:j\d+>> TypeConversion [<<Const3>>]
954 /// CHECK-DAG: <<Xor:j\d+>> Xor [<<TypeConv>>,<<Const10L>>]
955 /// CHECK-DAG: Return [<<Xor>>]
956
957 /// CHECK-START: long Main.XorLongInt() constant_folding (after)
958 /// CHECK-DAG: <<Const9:j\d+>> LongConstant 9
959 /// CHECK-DAG: Return [<<Const9>>]
960
961 /// CHECK-START: long Main.XorLongInt() constant_folding (after)
962 /// CHECK-NOT: Xor
963
964 public static long XorLongInt() {
965 long lhs = 10;
966 int rhs = 3;
967 return lhs ^ rhs;
968 }
969
970
971 /**
972 * Exercise constant folding on constant (static) condition.
973 */
974
975 /// CHECK-START: int Main.StaticCondition() constant_folding (before)
976 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
977 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
978 /// CHECK-DAG: <<Cond:z\d+>> GreaterThanOrEqual [<<Const7>>,<<Const2>>]
979 /// CHECK-DAG: If [<<Cond>>]
980
981 /// CHECK-START: int Main.StaticCondition() constant_folding (after)
982 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
983 /// CHECK-DAG: If [<<Const1>>]
984
985 /// CHECK-START: int Main.StaticCondition() constant_folding (after)
986 /// CHECK-NOT: GreaterThanOrEqual
987
988 public static int StaticCondition() {
989 int a, b, c;
990 a = 7;
991 b = 2;
992 if (a < b)
993 c = a + b;
994 else
995 c = a - b;
996 return c;
997 }
998
999
1000 /**
1001 * Exercise constant folding on constant (static) condition for null references.
1002 */
1003
1004 /// CHECK-START: int Main.StaticConditionNulls() constant_folding$after_inlining (before)
1005 /// CHECK-DAG: <<Null:l\d+>> NullConstant
1006 /// CHECK-DAG: <<Cond:z\d+>> NotEqual [<<Null>>,<<Null>>]
1007 /// CHECK-DAG: If [<<Cond>>]
1008
1009 /// CHECK-START: int Main.StaticConditionNulls() constant_folding$after_inlining (after)
1010 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1011 /// CHECK-DAG: If [<<Const0>>]
1012
1013 /// CHECK-START: int Main.StaticConditionNulls() constant_folding$after_inlining (after)
1014 /// CHECK-NOT: NotEqual
1015
1016 /// CHECK-START: int Main.StaticConditionNulls() dead_code_elimination$after_inlining (before)
1017 /// CHECK-DAG: <<Phi:i\d+>> Phi
1018 /// CHECK-DAG: Return [<<Phi>>]
1019 //
1020 /// CHECK-START: int Main.StaticConditionNulls() dead_code_elimination$after_inlining (after)
1021 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
1022 /// CHECK-DAG: Return [<<Const5>>]
1023
1024 private static Object getNull() {
1025 return null;
1026 }
1027
1028 public static int StaticConditionNulls() {
1029 Object a = getNull();
1030 Object b = getNull();
1031 return (a == b) ? 5 : 2;
1032 }
1033
1034
1035 /**
1036 * Exercise constant folding on a program with condition
1037 * (i.e. jumps) leading to the creation of many blocks.
1038 *
1039 * The intent of this test is to ensure that all constant expressions
1040 * are actually evaluated at compile-time, thanks to the reverse
1041 * (forward) post-order traversal of the the dominator tree.
1042 */
1043
1044 /// CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (before)
1045 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2
1046 /// CHECK-DAG: <<Const5:i\d+>> IntConstant 5
1047 /// CHECK-DAG: <<Add:i\d+>> Add [<<Const5>>,<<Const2>>]
1048 /// CHECK-DAG: <<Sub:i\d+>> Sub [<<Const5>>,<<Const2>>]
1049 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Add>>,<<Sub>>]
1050 /// CHECK-DAG: Return [<<Phi>>]
1051
1052 /// CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (after)
1053 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3
1054 /// CHECK-DAG: <<Const7:i\d+>> IntConstant 7
1055 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Const7>>,<<Const3>>]
1056 /// CHECK-DAG: Return [<<Phi>>]
1057
1058 /// CHECK-START: int Main.JumpsAndConditionals(boolean) constant_folding (after)
1059 /// CHECK-NOT: Add
1060 /// CHECK-NOT: Sub
1061
1062 public static int JumpsAndConditionals(boolean cond) {
1063 int a, b, c;
1064 a = 5;
1065 b = 2;
1066 if (cond)
1067 c = a + b;
1068 else
1069 c = a - b;
1070 return c;
1071 }
1072
1073
1074 /**
1075 * Test optimizations of arithmetic identities yielding a constant result.
1076 */
1077
1078 /// CHECK-START: int Main.And0(int) constant_folding (before)
1079 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1080 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1081 /// CHECK-DAG: <<And:i\d+>> And [<<Arg>>,<<Const0>>]
1082 /// CHECK-DAG: Return [<<And>>]
1083
1084 /// CHECK-START: int Main.And0(int) constant_folding (after)
1085 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1086 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1087 /// CHECK-DAG: Return [<<Const0>>]
1088
1089 /// CHECK-START: int Main.And0(int) constant_folding (after)
1090 /// CHECK-NOT: And
1091
1092 public static int And0(int arg) {
1093 return arg & 0;
1094 }
1095
1096 /// CHECK-START: long Main.Mul0(long) constant_folding (before)
1097 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
1098 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1099 /// CHECK-DAG: <<Mul:j\d+>> Mul [<<Const0>>,<<Arg>>]
1100 /// CHECK-DAG: Return [<<Mul>>]
1101
1102 /// CHECK-START: long Main.Mul0(long) constant_folding (after)
1103 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
1104 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1105 /// CHECK-DAG: Return [<<Const0>>]
1106
1107 /// CHECK-START: long Main.Mul0(long) constant_folding (after)
1108 /// CHECK-NOT: Mul
1109
1110 public static long Mul0(long arg) {
1111 return arg * 0;
1112 }
1113
1114 /// CHECK-START: int Main.OrAllOnes(int) constant_folding (before)
1115 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1116 /// CHECK-DAG: <<ConstF:i\d+>> IntConstant -1
1117 /// CHECK-DAG: <<Or:i\d+>> Or [<<Arg>>,<<ConstF>>]
1118 /// CHECK-DAG: Return [<<Or>>]
1119
1120 /// CHECK-START: int Main.OrAllOnes(int) constant_folding (after)
1121 /// CHECK-DAG: <<ConstF:i\d+>> IntConstant -1
1122 /// CHECK-DAG: Return [<<ConstF>>]
1123
1124 /// CHECK-START: int Main.OrAllOnes(int) constant_folding (after)
1125 /// CHECK-NOT: Or
1126
1127 public static int OrAllOnes(int arg) {
1128 return arg | -1;
1129 }
1130
1131 /// CHECK-START: long Main.Rem0(long) constant_folding (before)
1132 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
1133 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1134 /// CHECK-DAG: <<DivZeroCheck:j\d+>> DivZeroCheck [<<Arg>>]
1135 /// CHECK-DAG: <<Rem:j\d+>> Rem [<<Const0>>,<<DivZeroCheck>>]
1136 /// CHECK-DAG: Return [<<Rem>>]
1137
1138 /// CHECK-START: long Main.Rem0(long) constant_folding (after)
1139 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1140 /// CHECK-DAG: Return [<<Const0>>]
1141
1142 /// CHECK-START: long Main.Rem0(long) constant_folding (after)
1143 /// CHECK-NOT: Rem
1144
1145 public static long Rem0(long arg) {
1146 return 0 % arg;
1147 }
1148
1149 /// CHECK-START: int Main.Rem1(int) constant_folding (before)
1150 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1151 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
1152 /// CHECK-DAG: <<Rem:i\d+>> Rem [<<Arg>>,<<Const1>>]
1153 /// CHECK-DAG: Return [<<Rem>>]
1154
1155 /// CHECK-START: int Main.Rem1(int) constant_folding (after)
1156 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1157 /// CHECK-DAG: Return [<<Const0>>]
1158
1159 /// CHECK-START: int Main.Rem1(int) constant_folding (after)
1160 /// CHECK-NOT: Rem
1161
1162 public static int Rem1(int arg) {
1163 return arg % 1;
1164 }
1165
1166 /// CHECK-START: int Main.RemN1(int) constant_folding (before)
1167 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1168 /// CHECK-DAG: <<ConstN1:i\d+>> IntConstant -1
1169 /// CHECK-DAG: <<Rem:i\d+>> Rem [<<Arg>>,<<ConstN1>>]
1170 /// CHECK-DAG: Return [<<Rem>>]
1171
1172 /// CHECK-START: int Main.RemN1(int) constant_folding (after)
1173 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1174 /// CHECK-DAG: Return [<<Const0>>]
1175
1176 /// CHECK-START: int Main.RemN1(int) constant_folding (after)
1177 /// CHECK-NOT: Rem
1178
1179 public static int RemN1(int arg) {
1180 return arg % -1;
1181 }
1182
1183 /// CHECK-START: long Main.Rem1(long) constant_folding (before)
1184 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
1185 /// CHECK-DAG: <<Const1:j\d+>> LongConstant 1
1186 /// CHECK-DAG: <<DivZeroCheck:j\d+>> DivZeroCheck [<<Const1>>]
1187 /// CHECK-DAG: <<Rem:j\d+>> Rem [<<Arg>>,<<DivZeroCheck>>]
1188 /// CHECK-DAG: Return [<<Rem>>]
1189
1190 /// CHECK-START: long Main.Rem1(long) constant_folding (after)
1191 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1192 /// CHECK-DAG: Return [<<Const0>>]
1193
1194 /// CHECK-START: long Main.Rem1(long) constant_folding (after)
1195 /// CHECK-NOT: Rem
1196
1197 public static long Rem1(long arg) {
1198 return arg % 1;
1199 }
1200
1201 /// CHECK-START: long Main.RemN1(long) constant_folding (before)
1202 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
1203 /// CHECK-DAG: <<ConstN1:j\d+>> LongConstant -1
1204 /// CHECK-DAG: <<DivZeroCheck:j\d+>> DivZeroCheck [<<ConstN1>>]
1205 /// CHECK-DAG: <<Rem:j\d+>> Rem [<<Arg>>,<<DivZeroCheck>>]
1206 /// CHECK-DAG: Return [<<Rem>>]
1207
1208 /// CHECK-START: long Main.RemN1(long) constant_folding (after)
1209 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1210 /// CHECK-DAG: Return [<<Const0>>]
1211
1212 /// CHECK-START: long Main.RemN1(long) constant_folding (after)
1213 /// CHECK-NOT: Rem
1214
1215 public static long RemN1(long arg) {
1216 return arg % -1;
1217 }
1218
1219 /// CHECK-START: int Main.Shl0(int) constant_folding (before)
1220 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1221 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1222 /// CHECK-DAG: <<Shl:i\d+>> Shl [<<Const0>>,<<Arg>>]
1223 /// CHECK-DAG: Return [<<Shl>>]
1224
1225 /// CHECK-START: int Main.Shl0(int) constant_folding (after)
1226 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1227 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1228 /// CHECK-DAG: Return [<<Const0>>]
1229
1230 /// CHECK-START: int Main.Shl0(int) constant_folding (after)
1231 /// CHECK-NOT: Shl
1232
1233 public static int Shl0(int arg) {
1234 return 0 << arg;
1235 }
1236
1237 /// CHECK-START: long Main.ShlLong0WithInt(int) constant_folding (before)
1238 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1239 /// CHECK-DAG: <<Const0L:j\d+>> LongConstant 0
1240 /// CHECK-DAG: <<Shl:j\d+>> Shl [<<Const0L>>,<<Arg>>]
1241 /// CHECK-DAG: Return [<<Shl>>]
1242
1243 /// CHECK-START: long Main.ShlLong0WithInt(int) constant_folding (after)
1244 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1245 /// CHECK-DAG: <<Const0L:j\d+>> LongConstant 0
1246 /// CHECK-DAG: Return [<<Const0L>>]
1247
1248 /// CHECK-START: long Main.ShlLong0WithInt(int) constant_folding (after)
1249 /// CHECK-NOT: Shl
1250
1251 public static long ShlLong0WithInt(int arg) {
1252 long long_zero = 0;
1253 return long_zero << arg;
1254 }
1255
1256 /// CHECK-START: long Main.Shr0(int) constant_folding (before)
1257 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1258 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1259 /// CHECK-DAG: <<Shr:j\d+>> Shr [<<Const0>>,<<Arg>>]
1260 /// CHECK-DAG: Return [<<Shr>>]
1261
1262 /// CHECK-START: long Main.Shr0(int) constant_folding (after)
1263 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1264 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1265 /// CHECK-DAG: Return [<<Const0>>]
1266
1267 /// CHECK-START: long Main.Shr0(int) constant_folding (after)
1268 /// CHECK-NOT: Shr
1269
1270 public static long Shr0(int arg) {
1271 return (long)0 >> arg;
1272 }
1273
1274 /// CHECK-START: long Main.SubSameLong(long) constant_folding (before)
1275 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
1276 /// CHECK-DAG: <<Sub:j\d+>> Sub [<<Arg>>,<<Arg>>]
1277 /// CHECK-DAG: Return [<<Sub>>]
1278
1279 /// CHECK-START: long Main.SubSameLong(long) constant_folding (after)
1280 /// CHECK-DAG: <<Arg:j\d+>> ParameterValue
1281 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1282 /// CHECK-DAG: Return [<<Const0>>]
1283
1284 /// CHECK-START: long Main.SubSameLong(long) constant_folding (after)
1285 /// CHECK-NOT: Sub
1286
1287 public static long SubSameLong(long arg) {
1288 return arg - arg;
1289 }
1290
1291 /// CHECK-START: int Main.UShr0(int) constant_folding (before)
1292 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1293 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1294 /// CHECK-DAG: <<UShr:i\d+>> UShr [<<Const0>>,<<Arg>>]
1295 /// CHECK-DAG: Return [<<UShr>>]
1296
1297 /// CHECK-START: int Main.UShr0(int) constant_folding (after)
1298 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1299 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1300 /// CHECK-DAG: Return [<<Const0>>]
1301
1302 /// CHECK-START: int Main.UShr0(int) constant_folding (after)
1303 /// CHECK-NOT: UShr
1304
1305 public static int UShr0(int arg) {
1306 return 0 >>> arg;
1307 }
1308
1309 /// CHECK-START: int Main.XorSameInt(int) constant_folding (before)
1310 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1311 /// CHECK-DAG: <<Xor:i\d+>> Xor [<<Arg>>,<<Arg>>]
1312 /// CHECK-DAG: Return [<<Xor>>]
1313
1314 /// CHECK-START: int Main.XorSameInt(int) constant_folding (after)
1315 /// CHECK-DAG: <<Arg:i\d+>> ParameterValue
1316 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1317 /// CHECK-DAG: Return [<<Const0>>]
1318
1319 /// CHECK-START: int Main.XorSameInt(int) constant_folding (after)
1320 /// CHECK-NOT: Xor
1321
1322 public static int XorSameInt(int arg) {
1323 return arg ^ arg;
1324 }
1325
1326 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (before)
1327 /// CHECK-DAG: <<Arg:f\d+>> ParameterValue
1328 /// CHECK-DAG: <<ConstNan:f\d+>> FloatConstant nan
1329 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1330 /// CHECK-DAG: IntConstant 1
1331 /// CHECK-DAG: <<Cmp:i\d+>> Compare [<<Arg>>,<<ConstNan>>]
1332 /// CHECK-DAG: <<Le:z\d+>> LessThanOrEqual [<<Cmp>>,<<Const0>>]
1333 /// CHECK-DAG: If [<<Le>>]
1334
1335 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (after)
1336 /// CHECK-DAG: ParameterValue
1337 /// CHECK-DAG: FloatConstant nan
1338 /// CHECK-DAG: IntConstant 0
1339 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
1340 /// CHECK-DAG: If [<<Const1>>]
1341
1342 /// CHECK-START: boolean Main.CmpFloatGreaterThanNaN(float) constant_folding (after)
1343 /// CHECK-NOT: Compare
1344 /// CHECK-NOT: LessThanOrEqual
1345
1346 public static boolean CmpFloatGreaterThanNaN(float arg) {
1347 return arg > Float.NaN;
1348 }
1349
1350 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (before)
1351 /// CHECK-DAG: <<Arg:d\d+>> ParameterValue
1352 /// CHECK-DAG: <<ConstNan:d\d+>> DoubleConstant nan
1353 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1354 /// CHECK-DAG: IntConstant 1
1355 /// CHECK-DAG: <<Cmp:i\d+>> Compare [<<Arg>>,<<ConstNan>>]
1356 /// CHECK-DAG: <<Ge:z\d+>> GreaterThanOrEqual [<<Cmp>>,<<Const0>>]
1357 /// CHECK-DAG: If [<<Ge>>]
1358
1359 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (after)
1360 /// CHECK-DAG: ParameterValue
1361 /// CHECK-DAG: DoubleConstant nan
1362 /// CHECK-DAG: IntConstant 0
1363 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
1364 /// CHECK-DAG: If [<<Const1>>]
1365
1366 /// CHECK-START: boolean Main.CmpDoubleLessThanNaN(double) constant_folding (after)
1367 /// CHECK-NOT: Compare
1368 /// CHECK-NOT: GreaterThanOrEqual
1369
1370 public static boolean CmpDoubleLessThanNaN(double arg) {
1371 return arg < Double.NaN;
1372 }
1373
1374
1375 /**
1376 * Exercise constant folding on type conversions.
1377 */
1378
1379 /// CHECK-START: int Main.ReturnInt33() constant_folding (before)
1380 /// CHECK-DAG: <<Const33:j\d+>> LongConstant 33
1381 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<Const33>>]
1382 /// CHECK-DAG: Return [<<Convert>>]
1383
1384 /// CHECK-START: int Main.ReturnInt33() constant_folding (after)
1385 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
1386 /// CHECK-DAG: Return [<<Const33>>]
1387
1388 /// CHECK-START: int Main.ReturnInt33() constant_folding (after)
1389 /// CHECK-NOT: TypeConversion
1390
1391 public static int ReturnInt33() {
1392 long imm = 33L;
1393 return (int) imm;
1394 }
1395
1396 /// CHECK-START: int Main.ReturnIntMax() constant_folding (before)
1397 /// CHECK-DAG: <<ConstMax:f\d+>> FloatConstant 1e+34
1398 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<ConstMax>>]
1399 /// CHECK-DAG: Return [<<Convert>>]
1400
1401 /// CHECK-START: int Main.ReturnIntMax() constant_folding (after)
1402 /// CHECK-DAG: <<ConstMax:i\d+>> IntConstant 2147483647
1403 /// CHECK-DAG: Return [<<ConstMax>>]
1404
1405 /// CHECK-START: int Main.ReturnIntMax() constant_folding (after)
1406 /// CHECK-NOT: TypeConversion
1407
1408 public static int ReturnIntMax() {
1409 float imm = 1.0e34f;
1410 return (int) imm;
1411 }
1412
1413 /// CHECK-START: int Main.ReturnInt0() constant_folding (before)
1414 /// CHECK-DAG: <<ConstNaN:d\d+>> DoubleConstant nan
1415 /// CHECK-DAG: <<Convert:i\d+>> TypeConversion [<<ConstNaN>>]
1416 /// CHECK-DAG: Return [<<Convert>>]
1417
1418 /// CHECK-START: int Main.ReturnInt0() constant_folding (after)
1419 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1420 /// CHECK-DAG: Return [<<Const0>>]
1421
1422 /// CHECK-START: int Main.ReturnInt0() constant_folding (after)
1423 /// CHECK-NOT: TypeConversion
1424
1425 public static int ReturnInt0() {
1426 double imm = Double.NaN;
1427 return (int) imm;
1428 }
1429
1430 /// CHECK-START: long Main.ReturnLong33() constant_folding (before)
1431 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
1432 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<Const33>>]
1433 /// CHECK-DAG: Return [<<Convert>>]
1434
1435 /// CHECK-START: long Main.ReturnLong33() constant_folding (after)
1436 /// CHECK-DAG: <<Const33:j\d+>> LongConstant 33
1437 /// CHECK-DAG: Return [<<Const33>>]
1438
1439 /// CHECK-START: long Main.ReturnLong33() constant_folding (after)
1440 /// CHECK-NOT: TypeConversion
1441
1442 public static long ReturnLong33() {
1443 int imm = 33;
1444 return (long) imm;
1445 }
1446
1447 /// CHECK-START: long Main.ReturnLong34() constant_folding (before)
1448 /// CHECK-DAG: <<Const34:f\d+>> FloatConstant 34
1449 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<Const34>>]
1450 /// CHECK-DAG: Return [<<Convert>>]
1451
1452 /// CHECK-START: long Main.ReturnLong34() constant_folding (after)
1453 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
1454 /// CHECK-DAG: Return [<<Const34>>]
1455
1456 /// CHECK-START: long Main.ReturnLong34() constant_folding (after)
1457 /// CHECK-NOT: TypeConversion
1458
1459 public static long ReturnLong34() {
1460 float imm = 34.0f;
1461 return (long) imm;
1462 }
1463
1464 /// CHECK-START: long Main.ReturnLong0() constant_folding (before)
1465 /// CHECK-DAG: <<ConstNaN:d\d+>> DoubleConstant nan
1466 /// CHECK-DAG: <<Convert:j\d+>> TypeConversion [<<ConstNaN>>]
1467 /// CHECK-DAG: Return [<<Convert>>]
1468
1469 /// CHECK-START: long Main.ReturnLong0() constant_folding (after)
1470 /// CHECK-DAG: <<Const0:j\d+>> LongConstant 0
1471 /// CHECK-DAG: Return [<<Const0>>]
1472
1473 /// CHECK-START: long Main.ReturnLong0() constant_folding (after)
1474 /// CHECK-NOT: TypeConversion
1475
1476 public static long ReturnLong0() {
1477 double imm = -Double.NaN;
1478 return (long) imm;
1479 }
1480
1481 /// CHECK-START: float Main.ReturnFloat33() constant_folding (before)
1482 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
1483 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const33>>]
1484 /// CHECK-DAG: Return [<<Convert>>]
1485
1486 /// CHECK-START: float Main.ReturnFloat33() constant_folding (after)
1487 /// CHECK-DAG: <<Const33:f\d+>> FloatConstant 33
1488 /// CHECK-DAG: Return [<<Const33>>]
1489
1490 /// CHECK-START: float Main.ReturnFloat33() constant_folding (after)
1491 /// CHECK-NOT: TypeConversion
1492
1493 public static float ReturnFloat33() {
1494 int imm = 33;
1495 return (float) imm;
1496 }
1497
1498 /// CHECK-START: float Main.ReturnFloat34() constant_folding (before)
1499 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
1500 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const34>>]
1501 /// CHECK-DAG: Return [<<Convert>>]
1502
1503 /// CHECK-START: float Main.ReturnFloat34() constant_folding (after)
1504 /// CHECK-DAG: <<Const34:f\d+>> FloatConstant 34
1505 /// CHECK-DAG: Return [<<Const34>>]
1506
1507 /// CHECK-START: float Main.ReturnFloat34() constant_folding (after)
1508 /// CHECK-NOT: TypeConversion
1509
1510 public static float ReturnFloat34() {
1511 long imm = 34L;
1512 return (float) imm;
1513 }
1514
1515 /// CHECK-START: float Main.ReturnFloat99P25() constant_folding (before)
1516 /// CHECK-DAG: <<Const:d\d+>> DoubleConstant 99.25
1517 /// CHECK-DAG: <<Convert:f\d+>> TypeConversion [<<Const>>]
1518 /// CHECK-DAG: Return [<<Convert>>]
1519
1520 /// CHECK-START: float Main.ReturnFloat99P25() constant_folding (after)
1521 /// CHECK-DAG: <<Const:f\d+>> FloatConstant 99.25
1522 /// CHECK-DAG: Return [<<Const>>]
1523
1524 /// CHECK-START: float Main.ReturnFloat99P25() constant_folding (after)
1525 /// CHECK-NOT: TypeConversion
1526
1527 public static float ReturnFloat99P25() {
1528 double imm = 99.25;
1529 return (float) imm;
1530 }
1531
1532 /// CHECK-START: double Main.ReturnDouble33() constant_folding (before)
1533 /// CHECK-DAG: <<Const33:i\d+>> IntConstant 33
1534 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const33>>]
1535 /// CHECK-DAG: Return [<<Convert>>]
1536
1537 /// CHECK-START: double Main.ReturnDouble33() constant_folding (after)
1538 /// CHECK-DAG: <<Const33:d\d+>> DoubleConstant 33
1539 /// CHECK-DAG: Return [<<Const33>>]
1540
1541 public static double ReturnDouble33() {
1542 int imm = 33;
1543 return (double) imm;
1544 }
1545
1546 /// CHECK-START: double Main.ReturnDouble34() constant_folding (before)
1547 /// CHECK-DAG: <<Const34:j\d+>> LongConstant 34
1548 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const34>>]
1549 /// CHECK-DAG: Return [<<Convert>>]
1550
1551 /// CHECK-START: double Main.ReturnDouble34() constant_folding (after)
1552 /// CHECK-DAG: <<Const34:d\d+>> DoubleConstant 34
1553 /// CHECK-DAG: Return [<<Const34>>]
1554
1555 /// CHECK-START: double Main.ReturnDouble34() constant_folding (after)
1556 /// CHECK-NOT: TypeConversion
1557
1558 public static double ReturnDouble34() {
1559 long imm = 34L;
1560 return (double) imm;
1561 }
1562
1563 /// CHECK-START: double Main.ReturnDouble99P25() constant_folding (before)
1564 /// CHECK-DAG: <<Const:f\d+>> FloatConstant 99.25
1565 /// CHECK-DAG: <<Convert:d\d+>> TypeConversion [<<Const>>]
1566 /// CHECK-DAG: Return [<<Convert>>]
1567
1568 /// CHECK-START: double Main.ReturnDouble99P25() constant_folding (after)
1569 /// CHECK-DAG: <<Const:d\d+>> DoubleConstant 99.25
1570 /// CHECK-DAG: Return [<<Const>>]
1571
1572 /// CHECK-START: double Main.ReturnDouble99P25() constant_folding (after)
1573 /// CHECK-NOT: TypeConversion
1574
1575 public static double ReturnDouble99P25() {
1576 float imm = 99.25f;
1577 return (double) imm;
1578 }
1579
1580 /// CHECK-START: int Main.$inline$SpecialCaseForZeroInt(int) constant_folding$after_gvn (before)
1581 /// CHECK-DAG: Add
1582 /// CHECK-DAG: Shl
1583 /// CHECK-DAG: Add
1584
1585 /// CHECK-START: int Main.$inline$SpecialCaseForZeroInt(int) constant_folding$after_gvn (before)
1586 /// CHECK-NOT: IntConstant 6
1587
1588 /// CHECK-START: int Main.$inline$SpecialCaseForZeroInt(int) constant_folding$after_gvn (after)
1589 /// CHECK-NOT: Add
1590
1591 /// CHECK-START: int Main.$inline$SpecialCaseForZeroInt(int) constant_folding$after_gvn (after)
1592 /// CHECK-NOT: Shl
1593
1594 /// CHECK-START: int Main.$inline$SpecialCaseForZeroInt(int) constant_folding$after_gvn (after)
1595 /// CHECK-DAG: <<Const:i\d+>> IntConstant 6
1596 /// CHECK-DAG: Return [<<Const>>]
1597 private static int $inline$SpecialCaseForZeroInt(int value) {
1598 if (value == 0) {
1599 return (value + 2) * 3;
1600 }
1601 return value;
1602 }
1603
1604 /// CHECK-START: long Main.$inline$SpecialCaseForZeroLong(long) constant_folding$after_gvn (before)
1605 /// CHECK-DAG: Add
1606 /// CHECK-DAG: Shl
1607 /// CHECK-DAG: Add
1608
1609 /// CHECK-START: long Main.$inline$SpecialCaseForZeroLong(long) constant_folding$after_gvn (before)
1610 /// CHECK-NOT: LongConstant 6
1611
1612 /// CHECK-START: long Main.$inline$SpecialCaseForZeroLong(long) constant_folding$after_gvn (after)
1613 /// CHECK-NOT: Add
1614
1615 /// CHECK-START: long Main.$inline$SpecialCaseForZeroLong(long) constant_folding$after_gvn (after)
1616 /// CHECK-NOT: Shl
1617
1618 /// CHECK-START: long Main.$inline$SpecialCaseForZeroLong(long) constant_folding$after_gvn (after)
1619 /// CHECK-DAG: <<Const:j\d+>> LongConstant 6
1620 /// CHECK-DAG: Return [<<Const>>]
1621 private static long $inline$SpecialCaseForZeroLong(long value) {
1622 if (value == 0L) {
1623 return (value + 2) * 3;
1624 }
1625 return value;
1626 }
1627
1628 /// CHECK-START: float Main.$noinline$SpecialCaseForZeroFloat(float) constant_folding$after_gvn (before)
1629 /// CHECK-DAG: Add
1630 /// CHECK-DAG: Mul
1631
1632 /// CHECK-START: float Main.$noinline$SpecialCaseForZeroFloat(float) constant_folding$after_gvn (after)
1633 /// CHECK-NOT: FloatConstant 6
1634
1635 /// CHECK-START: float Main.$noinline$SpecialCaseForZeroFloat(float) constant_folding$after_gvn (after)
1636 /// CHECK-DAG: Add
1637 /// CHECK-DAG: Mul
1638 private static float $noinline$SpecialCaseForZeroFloat(float value) {
1639 if (value == 0F) {
1640 return (value + 2F) * 3F;
1641 }
1642 return value;
1643 }
1644
1645 /// CHECK-START: double Main.$noinline$SpecialCaseForZeroDouble(double) constant_folding$after_gvn (before)
1646 /// CHECK-DAG: Add
1647 /// CHECK-DAG: Mul
1648
1649 /// CHECK-START: double Main.$noinline$SpecialCaseForZeroDouble(double) constant_folding$after_gvn (after)
1650 /// CHECK-NOT: DoubleConstant 6
1651
1652 /// CHECK-START: double Main.$noinline$SpecialCaseForZeroDouble(double) constant_folding$after_gvn (after)
1653 /// CHECK-DAG: Add
1654 /// CHECK-DAG: Mul
1655 private static double $noinline$SpecialCaseForZeroDouble(double value) {
1656 if (value == 0D) {
1657 return (value + 2D) * 3D;
1658 }
1659 return value;
1660 }
1661
1662 // Note that we have Add instead of sub since internally we do `Add(value, -1)`.
1663 /// CHECK-START: int Main.$noinline$NotEqualsPropagationInt(int) constant_folding$after_gvn (before)
1664 /// CHECK-DAG: Add
1665 /// CHECK-DAG: Div
1666
1667 /// CHECK-START: int Main.$noinline$NotEqualsPropagationInt(int) constant_folding$after_gvn (after)
1668 /// CHECK-NOT: Add
1669
1670 /// CHECK-START: int Main.$noinline$NotEqualsPropagationInt(int) constant_folding$after_gvn (after)
1671 /// CHECK-NOT: Div
1672
1673 /// CHECK-START: int Main.$noinline$NotEqualsPropagationInt(int) constant_folding$after_gvn (after)
1674 /// CHECK-DAG: <<Const:i\d+>> IntConstant 1
1675 /// CHECK-DAG: Return [<<Const>>]
1676 private static int $noinline$NotEqualsPropagationInt(int value) {
1677 if (value != 3) {
1678 return value;
1679 } else {
1680 return (value - 1) / 2;
1681 }
1682 }
1683
1684 /// CHECK-START: long Main.$noinline$NotEqualsPropagationLong(long) constant_folding$after_gvn (before)
1685 /// CHECK-DAG: Sub
1686 /// CHECK-DAG: Div
1687
1688 /// CHECK-START: long Main.$noinline$NotEqualsPropagationLong(long) constant_folding$after_gvn (after)
1689 /// CHECK-NOT: Sub
1690
1691 /// CHECK-START: long Main.$noinline$NotEqualsPropagationLong(long) constant_folding$after_gvn (after)
1692 /// CHECK-NOT: Div
1693
1694 /// CHECK-START: long Main.$noinline$NotEqualsPropagationLong(long) constant_folding$after_gvn (after)
1695 /// CHECK-DAG: <<Const:j\d+>> LongConstant 1
1696 /// CHECK-DAG: Return [<<Const>>]
1697 private static long $noinline$NotEqualsPropagationLong(long value) {
1698 if (value != 3L) {
1699 return value;
1700 } else {
1701 return (value - 1L) / 2L;
1702 }
1703 }
1704
1705 /// CHECK-START: float Main.$noinline$NotEqualsPropagationFloat(float) constant_folding$after_gvn (before)
1706 /// CHECK-DAG: Sub
1707 /// CHECK-DAG: Mul
1708
1709 /// CHECK-START: float Main.$noinline$NotEqualsPropagationFloat(float) constant_folding$after_gvn (after)
1710 /// CHECK-DAG: Sub
1711 /// CHECK-DAG: Mul
1712 private static float $noinline$NotEqualsPropagationFloat(float value) {
1713 if (value != 3F) {
1714 return value;
1715 } else {
1716 return (value - 1F) / 2F;
1717 }
1718 }
1719
1720 /// CHECK-START: double Main.$noinline$NotEqualsPropagationDouble(double) constant_folding$after_gvn (before)
1721 /// CHECK-DAG: Sub
1722 /// CHECK-DAG: Mul
1723
1724 /// CHECK-START: double Main.$noinline$NotEqualsPropagationDouble(double) constant_folding$after_gvn (after)
1725 /// CHECK-DAG: Sub
1726 /// CHECK-DAG: Mul
1727 private static double $noinline$NotEqualsPropagationDouble(double value) {
1728 if (value != 3D) {
1729 return value;
1730 } else {
1731 return (value - 1D) / 2D;
1732 }
1733 }
1734
1735 /// CHECK-START: int Main.$noinline$InlineCalleeWithSpecialCaseForZeroInt(int) constant_folding$after_gvn (before)
1736 /// CHECK-DAG: Add
1737 /// CHECK-DAG: Shl
1738 /// CHECK-DAG: Add
1739
1740 /// CHECK-START: int Main.$noinline$InlineCalleeWithSpecialCaseForZeroInt(int) constant_folding$after_gvn (after)
1741 /// CHECK-NOT: Add
1742
1743 /// CHECK-START: int Main.$noinline$InlineCalleeWithSpecialCaseForZeroInt(int) constant_folding$after_gvn (after)
1744 /// CHECK-NOT: Shl
1745
1746 /// CHECK-START: int Main.$noinline$InlineCalleeWithSpecialCaseForZeroInt(int) dead_code_elimination$after_gvn (after)
1747 /// CHECK-DAG: <<Param:i\d+>> ParameterValue
1748 /// CHECK-DAG: <<Const6:i\d+>> IntConstant 6
1749 /// CHECK-DAG: Return [<<Param>>]
1750 /// CHECK-DAG: Return [<<Const6>>]
1751 private static int $noinline$InlineCalleeWithSpecialCaseForZeroInt(int value) {
1752 if (value == 0) {
1753 return $inline$SpecialCaseForZeroInt(value);
1754 }
1755 return value;
1756 }
1757
1758 /// CHECK-START: long Main.$noinline$InlineCalleeWithSpecialCaseForZeroLong(long) constant_folding$after_gvn (before)
1759 /// CHECK-DAG: Add
1760 /// CHECK-DAG: Shl
1761 /// CHECK-DAG: Add
1762
1763 /// CHECK-START: long Main.$noinline$InlineCalleeWithSpecialCaseForZeroLong(long) constant_folding$after_gvn (after)
1764 /// CHECK-NOT: Add
1765
1766 /// CHECK-START: long Main.$noinline$InlineCalleeWithSpecialCaseForZeroLong(long) constant_folding$after_gvn (after)
1767 /// CHECK-NOT: Shl
1768
1769 /// CHECK-START: long Main.$noinline$InlineCalleeWithSpecialCaseForZeroLong(long) dead_code_elimination$after_gvn (after)
1770 /// CHECK-DAG: <<Param:j\d+>> ParameterValue
1771 /// CHECK-DAG: <<Const6:j\d+>> LongConstant 6
1772 /// CHECK-DAG: Return [<<Param>>]
1773 /// CHECK-DAG: Return [<<Const6>>]
1774 private static long $noinline$InlineCalleeWithSpecialCaseForZeroLong(long value) {
1775 if (value == 0L) {
1776 return $inline$SpecialCaseForZeroLong(value);
1777 }
1778 return value;
1779 }
1780
1781 // Check that don't propagate the value == 3 on `if not true` branch, as the `if true` branch also
1782 // flows into the same block.
1783 /// CHECK-START: int Main.$noinline$NotEqualsImplicitElseInt(int) constant_folding$after_gvn (before)
1784 /// CHECK-DAG: Add
1785 /// CHECK-DAG: Div
1786
1787 /// CHECK-START: int Main.$noinline$NotEqualsImplicitElseInt(int) constant_folding$after_gvn (after)
1788 /// CHECK-DAG: Add
1789 /// CHECK-DAG: Div
1790 private static int $noinline$NotEqualsImplicitElseInt(int value) {
1791 if (value != 3) {
1792 value++;
1793 }
1794 return (value - 1) / 2;
1795 }
1796
1797 /// CHECK-START: long Main.$noinline$NotEqualsImplicitElseLong(long) constant_folding$after_gvn (before)
1798 /// CHECK-DAG: Sub
1799 /// CHECK-DAG: Div
1800
1801 /// CHECK-START: long Main.$noinline$NotEqualsImplicitElseLong(long) constant_folding$after_gvn (after)
1802 /// CHECK-DAG: Sub
1803 /// CHECK-DAG: Div
1804 private static long $noinline$NotEqualsImplicitElseLong(long value) {
1805 if (value != 3L) {
1806 value += 1L;
1807 }
1808 return (value - 1L) / 2L;
1809 }
1810
1811 /// CHECK-START: float Main.$noinline$NotEqualsImplicitElseFloat(float) constant_folding$after_gvn (before)
1812 /// CHECK-DAG: Sub
1813 /// CHECK-DAG: Mul
1814
1815 /// CHECK-START: float Main.$noinline$NotEqualsImplicitElseFloat(float) constant_folding$after_gvn (after)
1816 /// CHECK-DAG: Sub
1817 /// CHECK-DAG: Mul
1818 private static float $noinline$NotEqualsImplicitElseFloat(float value) {
1819 if (value != 3F) {
1820 value += 1F;
1821 }
1822 return (value - 1F) / 2F;
1823 }
1824
1825 /// CHECK-START: double Main.$noinline$NotEqualsImplicitElseDouble(double) constant_folding$after_gvn (before)
1826 /// CHECK-DAG: Sub
1827 /// CHECK-DAG: Mul
1828
1829 /// CHECK-START: double Main.$noinline$NotEqualsImplicitElseDouble(double) constant_folding$after_gvn (after)
1830 /// CHECK-DAG: Sub
1831 /// CHECK-DAG: Mul
1832 private static double $noinline$NotEqualsImplicitElseDouble(double value) {
1833 if (value != 3D) {
1834 value += 1D;
1835 }
1836 return (value - 1D) / 2D;
1837 }
1838
1839 // By propagating the boolean we can eliminate some equality comparisons as we already know their
1840 // result. In turn, we also enable DeadCodeElimination to eliminate more code.
1841 /// CHECK-START: int Main.$noinline$PropagatingParameterValue(boolean, int) constant_folding$after_gvn (before)
1842 /// CHECK-DAG: <<Param:z\d+>> ParameterValue
1843 /// CHECK-DAG: Select [{{i\d+}},{{i\d+}},<<Param>>]
1844 /// CHECK-DAG: Select [{{i\d+}},{{i\d+}},<<Param>>]
1845
1846 /// CHECK-START: int Main.$noinline$PropagatingParameterValue(boolean, int) constant_folding$after_gvn (after)
1847 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0
1848 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1
1849 /// CHECK-DAG: Select [{{i\d+}},{{i\d+}},<<Const0>>]
1850 /// CHECK-DAG: Select [{{i\d+}},{{i\d+}},<<Const1>>]
1851
1852 /// CHECK-START: int Main.$noinline$PropagatingParameterValue(boolean, int) dead_code_elimination$after_gvn (before)
1853 /// CHECK-DAG: IntConstant 1
1854 /// CHECK-DAG: IntConstant 2
1855 /// CHECK-DAG: IntConstant 3
1856 /// CHECK-DAG: IntConstant 4
1857
1858 /// CHECK-START: int Main.$noinline$PropagatingParameterValue(boolean, int) dead_code_elimination$after_gvn (after)
1859 /// CHECK-DAG: IntConstant 1
1860 /// CHECK-DAG: IntConstant 4
1861
1862 /// CHECK-START: int Main.$noinline$PropagatingParameterValue(boolean, int) dead_code_elimination$after_gvn (after)
1863 /// CHECK-NOT: IntConstant 2
1864
1865 /// CHECK-START: int Main.$noinline$PropagatingParameterValue(boolean, int) dead_code_elimination$after_gvn (after)
1866 /// CHECK-NOT: IntConstant 3
1867 private static int $noinline$PropagatingParameterValue(boolean value, int initial_value) {
1868 if (value) {
1869 return value ? initial_value + 1 : initial_value + 2;
1870 } else {
1871 return value ? initial_value + 3 : initial_value + 4;
1872 }
1873 }
1874
1875 public static void main(String[] args) throws Exception {
1876 assertIntEquals(-42, IntNegation());
1877 assertLongEquals(-42L, LongNegation());
1878 assertFloatEquals(-42F, FloatNegation());
1879 assertDoubleEquals(-42D, DoubleNegation());
1880
1881 assertIntEquals(3, IntAddition1());
1882 assertIntEquals(14, IntAddition2());
1883 assertIntEquals(14, smaliIntAddition2());
1884 assertIntEquals(14, smaliIntAddition2AddAndMove());
1885 assertLongEquals(3L, LongAddition());
1886 assertFloatEquals(3F, FloatAddition());
1887 assertDoubleEquals(3D, DoubleAddition());
1888
1889 assertIntEquals(4, IntSubtraction());
1890 assertLongEquals(4L, LongSubtraction());
1891 assertFloatEquals(4F, FloatSubtraction());
1892 assertDoubleEquals(4D, DoubleSubtraction());
1893
1894 assertIntEquals(21, IntMultiplication());
1895 assertLongEquals(21L, LongMultiplication());
1896 assertFloatEquals(21F, FloatMultiplication());
1897 assertDoubleEquals(21D, DoubleMultiplication());
1898
1899 assertIntEquals(2, IntDivision());
1900 assertLongEquals(2L, LongDivision());
1901 assertFloatEquals(3.2F, FloatDivision());
1902 assertDoubleEquals(3.2D, DoubleDivision());
1903
1904 assertIntEquals(2, IntRemainder());
1905 assertLongEquals(2L, LongRemainder());
1906 assertFloatEquals(0.5F, FloatRemainder());
1907 assertDoubleEquals(0.5D, DoubleRemainder());
1908
1909 assertIntEquals(4, ShlIntLong());
1910 assertLongEquals(12L, ShlLongInt());
1911
1912 assertIntEquals(1, ShrIntLong());
1913 assertLongEquals(2L, ShrLongInt());
1914
1915 assertIntEquals(1073741822, UShrIntLong());
1916 assertLongEquals(4611686018427387901L, UShrLongInt());
1917
1918 assertLongEquals(2, AndIntLong());
1919 assertLongEquals(2, AndLongInt());
1920
1921 assertLongEquals(11, OrIntLong());
1922 assertLongEquals(11, OrLongInt());
1923
1924 assertLongEquals(9, XorIntLong());
1925 assertLongEquals(9, XorLongInt());
1926
1927 assertIntEquals(5, StaticCondition());
1928 assertIntEquals(5, StaticConditionNulls());
1929
1930 assertIntEquals(7, JumpsAndConditionals(true));
1931 assertIntEquals(3, JumpsAndConditionals(false));
1932 assertIntEquals(7, smaliJumpsAndConditionals(true));
1933 assertIntEquals(3, smaliJumpsAndConditionals(false));
1934
1935 int arbitrary = 123456; // Value chosen arbitrarily.
1936
1937 assertIntEquals(0, And0(arbitrary));
1938 assertLongEquals(0, Mul0(arbitrary));
1939 assertIntEquals(-1, OrAllOnes(arbitrary));
1940 assertLongEquals(0, Rem0(arbitrary));
1941 assertIntEquals(0, Rem1(arbitrary));
1942 assertIntEquals(0, Rem1(0));
1943 assertIntEquals(0, Rem1(-1));
1944 assertIntEquals(0, Rem1(Integer.MAX_VALUE));
1945 assertIntEquals(0, Rem1(Integer.MIN_VALUE));
1946 assertIntEquals(0, RemN1(arbitrary));
1947 assertIntEquals(0, RemN1(0));
1948 assertIntEquals(0, RemN1(-1));
1949 assertIntEquals(0, RemN1(Integer.MAX_VALUE));
1950 assertIntEquals(0, RemN1(Integer.MIN_VALUE));
1951 assertIntEquals(0, RemN1(arbitrary));
1952 assertLongEquals(0, Rem1((long)arbitrary));
1953 assertLongEquals(0, Rem1(0L));
1954 assertLongEquals(0, Rem1(-1L));
1955 assertLongEquals(0, Rem1(Long.MAX_VALUE));
1956 assertLongEquals(0, Rem1(Long.MIN_VALUE));
1957 assertLongEquals(0, RemN1(arbitrary));
1958 assertLongEquals(0, RemN1(0L));
1959 assertLongEquals(0, RemN1(-1L));
1960 assertLongEquals(0, RemN1(Long.MAX_VALUE));
1961 assertLongEquals(0, RemN1(Long.MIN_VALUE));
1962 assertIntEquals(0, Shl0(arbitrary));
1963 assertLongEquals(0, ShlLong0WithInt(arbitrary));
1964 assertLongEquals(0, Shr0(arbitrary));
1965 assertLongEquals(0, SubSameLong(arbitrary));
1966 assertIntEquals(0, UShr0(arbitrary));
1967 assertIntEquals(0, XorSameInt(arbitrary));
1968
1969 assertFalse(CmpFloatGreaterThanNaN(arbitrary));
1970 assertFalse(CmpDoubleLessThanNaN(arbitrary));
1971
1972 Main main = new Main();
1973 assertIntEquals(1, main.smaliCmpLongConstants());
1974 assertIntEquals(-1, main.smaliCmpGtFloatConstants());
1975 assertIntEquals(-1, main.smaliCmpLtFloatConstants());
1976 assertIntEquals(-1, main.smaliCmpGtDoubleConstants());
1977 assertIntEquals(-1, main.smaliCmpLtDoubleConstants());
1978
1979 assertIntEquals(0, main.smaliCmpLongSameConstant());
1980 assertIntEquals(0, main.smaliCmpGtFloatSameConstant());
1981 assertIntEquals(0, main.smaliCmpLtFloatSameConstant());
1982 assertIntEquals(0, main.smaliCmpGtDoubleSameConstant());
1983 assertIntEquals(0, main.smaliCmpLtDoubleSameConstant());
1984
1985 assertIntEquals(1, main.smaliCmpGtFloatConstantWithNaN());
1986 assertIntEquals(-1, main.smaliCmpLtFloatConstantWithNaN());
1987 assertIntEquals(1, main.smaliCmpGtDoubleConstantWithNaN());
1988 assertIntEquals(-1, main.smaliCmpLtDoubleConstantWithNaN());
1989
1990 assertIntEquals(33, ReturnInt33());
1991 assertIntEquals(2147483647, ReturnIntMax());
1992 assertIntEquals(0, ReturnInt0());
1993
1994 assertLongEquals(33, ReturnLong33());
1995 assertLongEquals(34, ReturnLong34());
1996 assertLongEquals(0, ReturnLong0());
1997
1998 assertFloatEquals(33, ReturnFloat33());
1999 assertFloatEquals(34, ReturnFloat34());
2000 assertFloatEquals(99.25f, ReturnFloat99P25());
2001
2002 assertDoubleEquals(33, ReturnDouble33());
2003 assertDoubleEquals(34, ReturnDouble34());
2004 assertDoubleEquals(99.25, ReturnDouble99P25());
2005
2006 // Tests for propagating known values due to if clauses.
2007
2008 // Propagating within the same method. These are marked $inline$ since we used them in
2009 // `InlineCalleeWithSpecialCaseForZeroInt`.
2010 assertIntEquals(6, $inline$SpecialCaseForZeroInt(0));
2011 assertIntEquals(3, $inline$SpecialCaseForZeroInt(3));
2012 assertLongEquals(6L, $inline$SpecialCaseForZeroLong(0L));
2013 assertLongEquals(3L, $inline$SpecialCaseForZeroLong(3L));
2014 // Floats and doubles we do not optimize (here and below). These methods are here to guarantee
2015 // that.
2016 assertFloatEquals(6F, $noinline$SpecialCaseForZeroFloat(0F));
2017 assertFloatEquals(3F, $noinline$SpecialCaseForZeroFloat(3F));
2018 assertDoubleEquals(6D, $noinline$SpecialCaseForZeroDouble(0D));
2019 assertDoubleEquals(3D, $noinline$SpecialCaseForZeroDouble(3D));
2020
2021 // Propagating within the same method, with not equals
2022 assertIntEquals(0, $noinline$NotEqualsPropagationInt(0));
2023 assertIntEquals(1, $noinline$NotEqualsPropagationInt(3));
2024 assertLongEquals(0L, $noinline$NotEqualsPropagationLong(0L));
2025 assertLongEquals(1L, $noinline$NotEqualsPropagationLong(3L));
2026 assertFloatEquals(0F, $noinline$NotEqualsPropagationFloat(0F));
2027 assertFloatEquals(1F, $noinline$NotEqualsPropagationFloat(3F));
2028 assertDoubleEquals(0D, $noinline$NotEqualsPropagationDouble(0D));
2029 assertDoubleEquals(1D, $noinline$NotEqualsPropagationDouble(3D));
2030
2031 // Propagating so that the inliner can use it.
2032 assertIntEquals(6, $noinline$InlineCalleeWithSpecialCaseForZeroInt(0));
2033 assertIntEquals(3, $noinline$InlineCalleeWithSpecialCaseForZeroInt(3));
2034 assertLongEquals(6L, $noinline$InlineCalleeWithSpecialCaseForZeroLong(0L));
2035 assertLongEquals(3L, $noinline$InlineCalleeWithSpecialCaseForZeroLong(3L));
2036
2037 // Propagating within the same method, with not equals
2038 assertIntEquals(0, $noinline$NotEqualsImplicitElseInt(0));
2039 assertIntEquals(1, $noinline$NotEqualsImplicitElseInt(3));
2040 assertLongEquals(0L, $noinline$NotEqualsImplicitElseLong(0L));
2041 assertLongEquals(1L, $noinline$NotEqualsImplicitElseLong(3L));
2042 assertFloatEquals(0F, $noinline$NotEqualsImplicitElseFloat(0F));
2043 assertFloatEquals(1F, $noinline$NotEqualsImplicitElseFloat(3F));
2044 assertDoubleEquals(0D, $noinline$NotEqualsImplicitElseDouble(0D));
2045 assertDoubleEquals(1D, $noinline$NotEqualsImplicitElseDouble(3D));
2046
2047 // Propagating parameters.
2048 assertIntEquals(1, $noinline$PropagatingParameterValue(true, 0));
2049 assertIntEquals(4, $noinline$PropagatingParameterValue(false, 0));
2050 }
2051
2052 Main() throws ClassNotFoundException {
2053 testCmp = Class.forName("TestCmp");
2054 }
2055
2056 private Class<?> testCmp;
2057 }