1 /*
   2  * Copyright (c) 2011, 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 /**
  26  * @test
  27  * @bug 7044738
  28  * @summary Loop unroll optimization causes incorrect result
  29  *
  30  * @run main/othervm -Xbatch compiler.loopopts.Test7044738
  31  */
  32 
  33 package compiler.loopopts;
  34 
  35 public class Test7044738 {
  36 
  37     private static final int INITSIZE = 10000;
  38     public int d[] = {1, 2, 3, 4};
  39     public int i, size;
  40 
  41     private static int iter = 5;
  42 
  43     boolean done() {
  44         return (--iter > 0);
  45     }
  46 
  47     public static void main(String args[]) {
  48         Test7044738 t = new Test7044738();
  49         t.test();
  50     }
  51 
  52     int test() {
  53 
  54         while (done()) {
  55             size = INITSIZE;
  56 
  57             for (i = 0; i < size; i++) {
  58                 d[0] = d[1]; // 2
  59                 d[1] = d[2]; // 3
  60                 d[2] = d[3]; // 4
  61                 d[3] = d[0]; // 2
  62 
  63                 d[0] = d[1]; // 3
  64                 d[1] = d[2]; // 4
  65                 d[2] = d[3]; // 2
  66                 d[3] = d[0]; // 3
  67 
  68                 d[0] = d[1]; // 4
  69                 d[1] = d[2]; // 2
  70                 d[2] = d[3]; // 3
  71                 d[3] = d[0]; // 4
  72 
  73                 d[0] = d[1]; // 2
  74                 d[1] = d[2]; // 3
  75                 d[2] = d[3]; // 4
  76                 d[3] = d[0]; // 2
  77 
  78                 d[0] = d[1]; // 3
  79                 d[1] = d[2]; // 4
  80                 d[2] = d[3]; // 2
  81                 d[3] = d[0]; // 3
  82 
  83                 d[0] = d[1]; // 4
  84                 d[1] = d[2]; // 2
  85                 d[2] = d[3]; // 3
  86                 d[3] = d[0]; // 4
  87 
  88                 d[0] = d[1]; // 2
  89                 d[1] = d[2]; // 3
  90                 d[2] = d[3]; // 4
  91                 d[3] = d[0]; // 2
  92 
  93                 d[0] = d[1]; // 3
  94                 d[1] = d[2]; // 4
  95                 d[2] = d[3]; // 2
  96                 d[3] = d[0]; // 3
  97             }
  98 
  99             // try to defeat dead code elimination
 100             if (d[0] == d[1]) {
 101                 System.out.println("test failed: iter=" + iter + "  i=" + i + " d[] = { " + d[0] + ", " + d[1] + ", " + d[2] + ", " + d[3] + " } ");
 102                 System.exit(97);
 103             }
 104         }
 105         return d[3];
 106     }
 107 }