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