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