1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6934604
27 * @summary enable parts of EliminateAutoBox by default
28 *
29 * @run main/othervm -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox
30 *                   compiler.eliminateAutobox.TestLongBoxing
31 * @run main/othervm -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox
32 *                   -XX:CompileCommand=exclude,compiler.eliminateAutobox.TestLongBoxing::dummy
33 *                   -XX:CompileCommand=exclude,compiler.eliminateAutobox.TestLongBoxing::foo
34 *                   -XX:CompileCommand=exclude,compiler.eliminateAutobox.TestLongBoxing::foob
35 *                   compiler.eliminateAutobox.TestLongBoxing
36 * @run main/othervm -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:-EliminateAutoBox
37 *                   -XX:CompileCommand=exclude,compiler.eliminateAutobox.TestLongBoxing::dummy
38 *                   -XX:CompileCommand=exclude,compiler.eliminateAutobox.TestLongBoxing::foo
39 *                   -XX:CompileCommand=exclude,compiler.eliminateAutobox.TestLongBoxing::foob
40 *                   compiler.eliminateAutobox.TestLongBoxing
41 */
42
43package compiler.eliminateAutobox;
44
45public class TestLongBoxing {
46
47  static final Long ibc = new Long(1);
48
49  //===============================================
50  // Non-inlined methods to test deoptimization info
51  static void dummy()     { }
52  static long  foo(long i)  { return i; }
53  static Long  foob(long i) { return Long.valueOf(i); }
54
55
56  static long simple(long i) {
57    Long ib = new Long(i);
58    return ib;
59  }
60
61  static long simpleb(long i) {
62    Long ib = Long.valueOf(i);
63    return ib;
64  }
65
66  static long simplec() {
67    Long ib = ibc;
68    return ib;
69  }
70
71  static long simplef(long i) {
72    Long ib = foob(i);
73    return ib;
74  }
75
76  static long simplep(Long ib) {
77    return ib;
78  }
79
80  static long simple2(long i) {
81    Long ib1 = new Long(i);
82    Long ib2 = new Long(i+1);
83    return ib1 + ib2;
84  }
85
86  static long simpleb2(long i) {
87    Long ib1 = Long.valueOf(i);
88    Long ib2 = Long.valueOf(i+1);
89    return ib1 + ib2;
90  }
91
92  static long simplem2(long i) {
93    Long ib1 = new Long(i);
94    Long ib2 = Long.valueOf(i+1);
95    return ib1 + ib2;
96  }
97
98  static long simplep2(long i, Long ib1) {
99    Long ib2 = Long.valueOf(i+1);
100    return ib1 + ib2;
101  }
102
103  static long simplec2(long i) {
104    Long ib1 = ibc;
105    Long ib2 = Long.valueOf(i+1);
106    return ib1 + ib2;
107  }
108
109  //===============================================
110  static long test(long i) {
111    Long ib = new Long(i);
112    if ((i&1) == 0)
113      ib = i+1;
114    return ib;
115  }
116
117  static long testb(long i) {
118    Long ib = i;
119    if ((i&1) == 0)
120      ib = (i+1);
121    return ib;
122  }
123
124  static long testm(long i) {
125    Long ib = i;
126    if ((i&1) == 0)
127      ib = new Long(i+1);
128    return ib;
129  }
130
131  static long testp(long i, Long ib) {
132    if ((i&1) == 0)
133      ib = new Long(i+1);
134    return ib;
135  }
136
137  static long testc(long i) {
138    Long ib = ibc;
139    if ((i&1) == 0)
140      ib = new Long(i+1);
141    return ib;
142  }
143
144  static long test2(long i) {
145    Long ib1 = new Long(i);
146    Long ib2 = new Long(i+1);
147    if ((i&1) == 0) {
148      ib1 = new Long(i+1);
149      ib2 = new Long(i+2);
150    }
151    return ib1+ib2;
152  }
153
154  static long testb2(long i) {
155    Long ib1 = i;
156    Long ib2 = i+1;
157    if ((i&1) == 0) {
158      ib1 = (i+1);
159      ib2 = (i+2);
160    }
161    return ib1+ib2;
162  }
163
164  static long testm2(long i) {
165    Long ib1 = new Long(i);
166    Long ib2 = i+1;
167    if ((i&1) == 0) {
168      ib1 = new Long(i+1);
169      ib2 = (i+2);
170    }
171    return ib1+ib2;
172  }
173
174  static long testp2(long i, Long ib1) {
175    Long ib2 = i+1;
176    if ((i&1) == 0) {
177      ib1 = new Long(i+1);
178      ib2 = (i+2);
179    }
180    return ib1+ib2;
181  }
182
183  static long testc2(long i) {
184    Long ib1 = ibc;
185    Long ib2 = i+1;
186    if ((i&1) == 0) {
187      ib1 = (ibc+1);
188      ib2 = (i+2);
189    }
190    return ib1+ib2;
191  }
192
193  //===============================================
194  static long sum(long[] a) {
195    long result = 1;
196    for (Long i : a)
197        result += i;
198    return result;
199  }
200
201  static long sumb(long[] a) {
202    Long result = 1l;
203    for (Long i : a)
204        result += i;
205    return result;
206  }
207
208  static long sumc(long[] a) {
209    Long result = ibc;
210    for (Long i : a)
211        result += i;
212    return result;
213  }
214
215  static long sumf(long[] a) {
216    Long result = foob(1);
217    for (Long i : a)
218        result += i;
219    return result;
220  }
221
222  static long sump(long[] a, Long result) {
223    for (Long i : a)
224        result += i;
225    return result;
226  }
227
228  static long sum2(long[] a) {
229    long result1 = 1;
230    long result2 = 1;
231    for (Long i : a) {
232        result1 += i;
233        result2 += i + 1;
234    }
235    return result1 + result2;
236  }
237
238  static long sumb2(long[] a) {
239    Long result1 = 1l;
240    Long result2 = 1l;
241    for (Long i : a) {
242        result1 += i;
243        result2 += i + 1;
244    }
245    return result1 + result2;
246  }
247
248  static long summ2(long[] a) {
249    Long result1 = 1l;
250    Long result2 = new Long(1);
251    for (Long i : a) {
252        result1 += i;
253        result2 += new Long(i + 1);
254    }
255    return result1 + result2;
256  }
257
258  static long sump2(long[] a, Long result2) {
259    Long result1 = 1l;
260    for (Long i : a) {
261        result1 += i;
262        result2 += i + 1;
263    }
264    return result1 + result2;
265  }
266
267  static long sumc2(long[] a) {
268    Long result1 = 1l;
269    Long result2 = ibc;
270    for (Long i : a) {
271        result1 += i;
272        result2 += i + ibc;
273    }
274    return result1 + result2;
275  }
276
277  //===============================================
278  static long remi_sum() {
279    Long j = new Long(1);
280    for (int i = 0; i< 1000; i++) {
281      j = new Long(j + 1);
282    }
283    return j;
284  }
285
286  static long remi_sumb() {
287    Long j = Long.valueOf(1);
288    for (int i = 0; i< 1000; i++) {
289      j = j + 1;
290    }
291    return j;
292  }
293
294  static long remi_sumf() {
295    Long j = foob(1);
296    for (int i = 0; i< 1000; i++) {
297      j = j + 1;
298    }
299    return j;
300  }
301
302  static long remi_sump(Long j) {
303    for (int i = 0; i< 1000; i++) {
304      j = new Long(j + 1);
305    }
306    return j;
307  }
308
309  static long remi_sumc() {
310    Long j = ibc;
311    for (int i = 0; i< 1000; i++) {
312      j = j + ibc;
313    }
314    return j;
315  }
316
317  static long remi_sum2() {
318    Long j1 = new Long(1);
319    Long j2 = new Long(1);
320    for (int i = 0; i< 1000; i++) {
321      j1 = new Long(j1 + 1);
322      j2 = new Long(j2 + 2);
323    }
324    return j1 + j2;
325  }
326
327  static long remi_sumb2() {
328    Long j1 = Long.valueOf(1);
329    Long j2 = Long.valueOf(1);
330    for (int i = 0; i< 1000; i++) {
331      j1 = j1 + 1;
332      j2 = j2 + 2;
333    }
334    return j1 + j2;
335  }
336
337  static long remi_summ2() {
338    Long j1 = new Long(1);
339    Long j2 = Long.valueOf(1);
340    for (int i = 0; i< 1000; i++) {
341      j1 = new Long(j1 + 1);
342      j2 = j2 + 2;
343    }
344    return j1 + j2;
345  }
346
347  static long remi_sump2(Long j1) {
348    Long j2 = Long.valueOf(1);
349    for (int i = 0; i< 1000; i++) {
350      j1 = new Long(j1 + 1);
351      j2 = j2 + 2;
352    }
353    return j1 + j2;
354  }
355
356  static long remi_sumc2() {
357    Long j1 = ibc;
358    Long j2 = Long.valueOf(1);
359    for (int i = 0; i< 1000; i++) {
360      j1 = j1 + ibc;
361      j2 = j2 + 2;
362    }
363    return j1 + j2;
364  }
365
366
367  //===============================================
368  // Safepointa and debug info for deoptimization
369  static long simple_deop(long i) {
370    Long ib = new Long(foo(i));
371    dummy();
372    return ib;
373  }
374
375  static long simpleb_deop(long i) {
376    Long ib = Long.valueOf(foo(i));
377    dummy();
378    return ib;
379  }
380
381  static long simplef_deop(long i) {
382    Long ib = foob(i);
383    dummy();
384    return ib;
385  }
386
387  static long simplep_deop(Long ib) {
388    dummy();
389    return ib;
390  }
391
392  static long simplec_deop(long i) {
393    Long ib = ibc;
394    dummy();
395    return ib;
396  }
397
398  static long test_deop(long i) {
399    Long ib = new Long(foo(i));
400    if ((i&1) == 0)
401      ib = foo(i+1);
402    dummy();
403    return ib;
404  }
405
406  static long testb_deop(long i) {
407    Long ib = foo(i);
408    if ((i&1) == 0)
409      ib = foo(i+1);
410    dummy();
411    return ib;
412  }
413
414  static long testf_deop(long i) {
415    Long ib = foob(i);
416    if ((i&1) == 0)
417      ib = foo(i+1);
418    dummy();
419    return ib;
420  }
421
422  static long testp_deop(long i, Long ib) {
423    if ((i&1) == 0)
424      ib = foo(i+1);
425    dummy();
426    return ib;
427  }
428
429  static long testc_deop(long i) {
430    Long ib = ibc;
431    if ((i&1) == 0)
432      ib = foo(i+1);
433    dummy();
434    return ib;
435  }
436
437  static long sum_deop(long[] a) {
438    long result = 1;
439    for (Long i : a)
440        result += foo(i);
441    dummy();
442    return result;
443  }
444
445  static long sumb_deop(long[] a) {
446    Long result = 1l;
447    for (Long i : a)
448        result += foo(i);
449    dummy();
450    return result;
451  }
452
453  static long sumf_deop(long[] a) {
454    Long result = 1l;
455    for (Long i : a)
456        result += foob(i);
457    dummy();
458    return result;
459  }
460
461  static long sump_deop(long[] a, Long result) {
462    for (Long i : a)
463        result += foob(i);
464    dummy();
465    return result;
466  }
467
468  static long sumc_deop(long[] a) {
469    Long result = ibc;
470    for (Long i : a)
471        result += foo(i);
472    dummy();
473    return result;
474  }
475
476  static long remi_sum_deop() {
477    Long j = new Long(foo(1));
478    for (int i = 0; i< 1000; i++) {
479      j = new Long(foo(j + 1));
480    }
481    dummy();
482    return j;
483  }
484
485  static long remi_sumb_deop() {
486    Long j = Long.valueOf(foo(1));
487    for (int i = 0; i< 1000; i++) {
488      j = foo(j + 1);
489    }
490    dummy();
491    return j;
492  }
493
494  static long remi_sumf_deop() {
495    Long j = foob(1);
496    for (int i = 0; i< 1000; i++) {
497      j = foo(j + 1);
498    }
499    dummy();
500    return j;
501  }
502
503  static long remi_sump_deop(Long j) {
504    for (int i = 0; i< 1000; i++) {
505      j = foo(j + 1);
506    }
507    dummy();
508    return j;
509  }
510
511  static long remi_sumc_deop() {
512    Long j = ibc;
513    for (int i = 0; i< 1000; i++) {
514      j = foo(j + 1);
515    }
516    dummy();
517    return j;
518  }
519
520  //===============================================
521  // Conditional increment
522  static long remi_sum_cond() {
523    Long j = new Long(1);
524    for (int i = 0; i< 1000; i++) {
525      if ((i&1) == 0) {
526        j = new Long(j + 1);
527      }
528    }
529    return j;
530  }
531
532  static long remi_sumb_cond() {
533    Long j = Long.valueOf(1);
534    for (int i = 0; i< 1000; i++) {
535      if ((i&1) == 0) {
536        j = j + 1;
537      }
538    }
539    return j;
540  }
541
542  static long remi_sumf_cond() {
543    Long j = foob(1);
544    for (int i = 0; i< 1000; i++) {
545      if ((i&1) == 0) {
546        j = j + 1;
547      }
548    }
549    return j;
550  }
551
552  static long remi_sump_cond(Long j) {
553    for (int i = 0; i< 1000; i++) {
554      if ((i&1) == 0) {
555        j = j + 1;
556      }
557    }
558    return j;
559  }
560
561  static long remi_sumc_cond() {
562    Long j = ibc;
563    for (int i = 0; i< 1000; i++) {
564      if ((i&1) == 0) {
565        j = j + ibc;
566      }
567    }
568    return j;
569  }
570
571  static long remi_sum2_cond() {
572    Long j1 = new Long(1);
573    Long j2 = new Long(1);
574    for (int i = 0; i< 1000; i++) {
575      if ((i&1) == 0) {
576        j1 = new Long(j1 + 1);
577      } else {
578        j2 = new Long(j2 + 2);
579      }
580    }
581    return j1 + j2;
582  }
583
584  static long remi_sumb2_cond() {
585    Long j1 = Long.valueOf(1);
586    Long j2 = Long.valueOf(1);
587    for (int i = 0; i< 1000; i++) {
588      if ((i&1) == 0) {
589        j1 = j1 + 1;
590      } else {
591        j2 = j2 + 2;
592      }
593    }
594    return j1 + j2;
595  }
596
597  static long remi_summ2_cond() {
598    Long j1 = new Long(1);
599    Long j2 = Long.valueOf(1);
600    for (int i = 0; i< 1000; i++) {
601      if ((i&1) == 0) {
602        j1 = new Long(j1 + 1);
603      } else {
604        j2 = j2 + 2;
605      }
606    }
607    return j1 + j2;
608  }
609
610  static long remi_sump2_cond(Long j1) {
611    Long j2 = Long.valueOf(1);
612    for (int i = 0; i< 1000; i++) {
613      if ((i&1) == 0) {
614        j1 = new Long(j1 + 1);
615      } else {
616        j2 = j2 + 2;
617      }
618    }
619    return j1 + j2;
620  }
621
622  static long remi_sumc2_cond() {
623    Long j1 = ibc;
624    Long j2 = Long.valueOf(1);
625    for (int i = 0; i< 1000; i++) {
626      if ((i&1) == 0) {
627        j1 = j1 + ibc;
628      } else {
629        j2 = j2 + 2;
630      }
631    }
632    return j1 + j2;
633  }
634
635
636  public static void main(String[] args) {
637    final int ntests = 70;
638
639    String[] test_name = new String[] {
640        "simple",      "simpleb",      "simplec",      "simplef",      "simplep",
641        "simple2",     "simpleb2",     "simplec2",     "simplem2",     "simplep2",
642        "simple_deop", "simpleb_deop", "simplec_deop", "simplef_deop", "simplep_deop",
643        "test",        "testb",        "testc",        "testm",        "testp",
644        "test2",       "testb2",       "testc2",       "testm2",       "testp2",
645        "test_deop",   "testb_deop",   "testc_deop",   "testf_deop",   "testp_deop",
646        "sum",         "sumb",         "sumc",         "sumf",         "sump",
647        "sum2",        "sumb2",        "sumc2",        "summ2",        "sump2",
648        "sum_deop",    "sumb_deop",    "sumc_deop",    "sumf_deop",    "sump_deop",
649        "remi_sum",       "remi_sumb",       "remi_sumc",       "remi_sumf",       "remi_sump",
650        "remi_sum2",      "remi_sumb2",      "remi_sumc2",      "remi_summ2",      "remi_sump2",
651        "remi_sum_deop",  "remi_sumb_deop",  "remi_sumc_deop",  "remi_sumf_deop",  "remi_sump_deop",
652        "remi_sum_cond",  "remi_sumb_cond",  "remi_sumc_cond",  "remi_sumf_cond",  "remi_sump_cond",
653        "remi_sum2_cond", "remi_sumb2_cond", "remi_sumc2_cond", "remi_summ2_cond", "remi_sump2_cond"
654    };
655
656    final long[] val = new long[] {
657       71994000,  71994000,    12000,  71994000,  71994000,
658      144000000, 144000000, 72018000, 144000000, 144000000,
659       71994000,  71994000,    12000,  71994000,  71994000,
660       72000000,  72000000, 36006000,  72000000,  72000000,
661      144012000, 144012000, 72030000, 144012000, 144012000,
662       72000000,  72000000, 36006000,  72000000,  72000000,
663         499501,    499501,   499501,    499501,    499501,
664        1000002,   1000002,  1000002,   1000002,   1000002,
665         499501,    499501,   499501,    499501,    499501,
666           1001,      1001,     1001,      1001,      1001,
667           3002,      3002,     3002,      3002,      3002,
668           1001,      1001,     1001,      1001,      1001,
669            501,       501,      501,       501,       501,
670           1502,      1502,     1502,      1502,      1502
671    };
672
673    long[] res = new long[ntests];
674    for (int i = 0; i < ntests; i++) {
675      res[i] = 0;
676    }
677
678
679    for (long i = 0; i < 12000; i++) {
680      res[0] += simple(i);
681      res[1] += simpleb(i);
682      res[2] += simplec();
683      res[3] += simplef(i);
684      res[4] += simplep(i);
685
686      res[5] += simple2(i);
687      res[6] += simpleb2(i);
688      res[7] += simplec2(i);
689      res[8] += simplem2(i);
690      res[9] += simplep2(i, i);
691
692      res[10] += simple_deop(i);
693      res[11] += simpleb_deop(i);
694      res[12] += simplec_deop(i);
695      res[13] += simplef_deop(i);
696      res[14] += simplep_deop(i);
697
698      res[15] += test(i);
699      res[16] += testb(i);
700      res[17] += testc(i);
701      res[18] += testm(i);
702      res[19] += testp(i, i);
703
704      res[20] += test2(i);
705      res[21] += testb2(i);
706      res[22] += testc2(i);
707      res[23] += testm2(i);
708      res[24] += testp2(i, i);
709
710      res[25] += test_deop(i);
711      res[26] += testb_deop(i);
712      res[27] += testc_deop(i);
713      res[28] += testf_deop(i);
714      res[29] += testp_deop(i, i);
715    }
716
717    long[] ia = new long[1000];
718    for (int i = 0; i < 1000; i++) {
719      ia[i] = i;
720    }
721
722    for (int i = 0; i < 100; i++) {
723      res[30] = sum(ia);
724      res[31] = sumb(ia);
725      res[32] = sumc(ia);
726      res[33] = sumf(ia);
727      res[34] = sump(ia, (long)1);
728
729      res[35] = sum2(ia);
730      res[36] = sumb2(ia);
731      res[37] = sumc2(ia);
732      res[38] = summ2(ia);
733      res[39] = sump2(ia, (long)1);
734
735      res[40] = sum_deop(ia);
736      res[41] = sumb_deop(ia);
737      res[42] = sumc_deop(ia);
738      res[43] = sumf_deop(ia);
739      res[44] = sump_deop(ia, (long)1);
740
741      res[45] = remi_sum();
742      res[46] = remi_sumb();
743      res[47] = remi_sumc();
744      res[48] = remi_sumf();
745      res[49] = remi_sump((long)1);
746
747      res[50] = remi_sum2();
748      res[51] = remi_sumb2();
749      res[52] = remi_sumc2();
750      res[53] = remi_summ2();
751      res[54] = remi_sump2((long)1);
752
753      res[55] = remi_sum_deop();
754      res[56] = remi_sumb_deop();
755      res[57] = remi_sumc_deop();
756      res[58] = remi_sumf_deop();
757      res[59] = remi_sump_deop((long)1);
758
759      res[60] = remi_sum_cond();
760      res[61] = remi_sumb_cond();
761      res[62] = remi_sumc_cond();
762      res[63] = remi_sumf_cond();
763      res[64] = remi_sump_cond((long)1);
764
765      res[65] = remi_sum2_cond();
766      res[66] = remi_sumb2_cond();
767      res[67] = remi_sumc2_cond();
768      res[68] = remi_summ2_cond();
769      res[69] = remi_sump2_cond((long)1);
770    }
771
772    int failed = 0;
773    for (int i = 0; i < ntests; i++) {
774      if (res[i] != val[i]) {
775        System.err.println(test_name[i] + ": " + res[i] + " != " + val[i]);
776        failed++;
777      }
778    }
779    if (failed > 0) {
780      System.err.println("Failed " + failed + " tests.");
781      throw new InternalError();
782    } else {
783      System.out.println("Passed.");
784    }
785  }
786}
787