1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. 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 8190375
  27  * @summary Bad range for IV phi when exit condition is a not equal test
  28  * @run main/othervm -XX:-TieredCompilation TestCountedLoopBadIVRange
  29  *
  30  */
  31 
  32 
  33 public class TestCountedLoopBadIVRange {
  34 
  35     static int test1(int[] arr) {
  36         int j = 0;
  37         int res = 0;
  38         for (int i = 0; i < 2; i++) {
  39             // When entered with j == 10, exit condition never
  40             // succeeds so range of values for j can't be computed
  41             // from exit condition
  42             for (; j != 5; j++) {
  43                 if (j >= 20) {
  44                     break;
  45                 }
  46                 res += arr[j];
  47             }
  48             j = 10;
  49         }
  50         return res;
  51     }
  52 
  53     static int test2(int[] arr) {
  54         int j = 10;
  55         int res = 0;
  56         for (int i = 0; i < 2; i++) {
  57             // Same as above but loop variable is decreasing
  58             for (; j != 5; j--) {
  59                 if (j < 0) {
  60                     break;
  61                 }
  62                 res += arr[j];
  63             }
  64             j = 1;
  65         }
  66         return res;
  67     }
  68 
  69     public static void main(String[] args) {
  70         int[] arr = new int[20];
  71         for (int i = 0; i < arr.length; i++) {
  72             arr[i] = i;
  73         }
  74         for (int i = 0; i < 20_000; i++) {
  75             int res = test1(arr);
  76             if (res != 155) {
  77                 throw new RuntimeException("Incorrect result " + res);
  78             }
  79             res = test2(arr);
  80             if (res != 41) {
  81                 throw new RuntimeException("Incorrect result " + res);
  82             }
  83         }
  84     }
  85 }