1 /*
   2  * Copyright (c) 2015, 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 8085832
  27  * @bug 8135069
  28  * @summary x <= 0 || x > 0 wrongly folded as (x-1) >u -1 and x < 0 || x > -1 wrongly folded as x >u -1
  29  *
  30  * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
  31  *                   compiler.rangechecks.TestBadFoldCompare
  32  */
  33 
  34 package compiler.rangechecks;
  35 
  36 public class TestBadFoldCompare {
  37 
  38     static boolean test1_taken;
  39 
  40     static void helper1(int i, int a, int b, boolean flag) {
  41         if (flag) {
  42             if (i <= a || i > b) {
  43                 test1_taken = true;
  44             }
  45         }
  46     }
  47 
  48     static void test1(int i, boolean flag) {
  49         helper1(i, 0, 0, flag);
  50     }
  51 
  52     static boolean test2_taken;
  53 
  54     static void helper2(int i, int a, int b, boolean flag) {
  55         if (flag) {
  56             if (i > b || i <= a) {
  57                 test2_taken = true;
  58             }
  59         }
  60     }
  61 
  62     static void test2(int i, boolean flag) {
  63         helper2(i, 0, 0, flag);
  64     }
  65 
  66     static boolean test3_taken;
  67 
  68     static void helper3(int i, int a, int b, boolean flag) {
  69         if (flag) {
  70             if (i < a || i > b - 1) {
  71                 test3_taken = true;
  72             }
  73         }
  74     }
  75 
  76     static void test3(int i, boolean flag) {
  77         helper3(i, 0, 0, flag);
  78     }
  79 
  80     static boolean test4_taken;
  81 
  82     static void helper4(int i, int a, int b, boolean flag) {
  83         if (flag) {
  84             if (i > b - 1 || i < a) {
  85                 test4_taken = true;
  86             }
  87         }
  88     }
  89 
  90     static void test4(int i, boolean flag) {
  91         helper4(i, 0, 0, flag);
  92     }
  93 
  94     static public void main(String[] args) {
  95         boolean success = true;
  96 
  97         for (int i = 0; i < 20000; i++) {
  98             helper1(5, 0,  10, (i%2)==0);
  99             helper1(-1, 0,  10, (i%2)==0);
 100             helper1(15, 0,  10, (i%2)==0);
 101             test1(0, false);
 102         }
 103         test1_taken = false;
 104         test1(0, true);
 105         if (!test1_taken) {
 106             System.out.println("Test1 failed");
 107             success = false;
 108         }
 109 
 110         for (int i = 0; i < 20000; i++) {
 111             helper2(5, 0,  10, (i%2)==0);
 112             helper2(-1, 0,  10, (i%2)==0);
 113             helper2(15, 0,  10, (i%2)==0);
 114             test2(0, false);
 115         }
 116         test2_taken = false;
 117         test2(0, true);
 118 
 119         if (!test2_taken) {
 120             System.out.println("Test2 failed");
 121             success = false;
 122         }
 123 
 124         for (int i = 0; i < 20000; i++) {
 125             helper3(5, 0,  10, (i%2)==0);
 126             helper3(-1, 0,  10, (i%2)==0);
 127             helper3(15, 0,  10, (i%2)==0);
 128             test3(0, false);
 129         }
 130         test3_taken = false;
 131         test3(0, true);
 132 
 133         if (!test3_taken) {
 134             System.out.println("Test3 failed");
 135             success = false;
 136         }
 137 
 138         for (int i = 0; i < 20000; i++) {
 139             helper4(5, 0,  10, (i%2)==0);
 140             helper4(-1, 0,  10, (i%2)==0);
 141             helper4(15, 0,  10, (i%2)==0);
 142             test4(0, false);
 143         }
 144         test4_taken = false;
 145         test4(0, true);
 146 
 147         if (!test4_taken) {
 148             System.out.println("Test4 failed");
 149             success = false;
 150         }
 151 
 152         if (!success) {
 153             throw new RuntimeException("Some tests failed");
 154         }
 155     }
 156 }