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