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 /**
  26  * @test
  27  * @bug 6443505
  28  * @summary Some cases for CmpLTMask missed; also wrong code.
  29  *
  30  * @run main/othervm -Xcomp
  31  *      -XX:CompileCommand=compiler.c2.Test6443505::compiled
  32  *      compiler.c2.Test6443505
  33  */
  34 
  35 package compiler.c2;
  36 
  37 public class Test6443505 {
  38 
  39     public static void main(String[] args) throws InterruptedException {
  40         test(Integer.MIN_VALUE, 0);
  41         test(0, Integer.MIN_VALUE);
  42         test(Integer.MIN_VALUE, -1);
  43         test(-1, Integer.MIN_VALUE);
  44         test(Integer.MIN_VALUE, 1);
  45         test(1, Integer.MIN_VALUE);
  46 
  47         test(Integer.MAX_VALUE, 0);
  48         test(0, Integer.MAX_VALUE);
  49         test(Integer.MAX_VALUE, -1);
  50         test(-1, Integer.MAX_VALUE);
  51         test(Integer.MAX_VALUE, 1);
  52         test(1, Integer.MAX_VALUE);
  53 
  54         test(Integer.MIN_VALUE, Integer.MAX_VALUE);
  55         test(Integer.MAX_VALUE, Integer.MIN_VALUE);
  56 
  57         test(1, -1);
  58         test(1, 0);
  59         test(1, 1);
  60         test(-1, -1);
  61         test(-1, 0);
  62         test(-1, 1);
  63         test(0, -1);
  64         test(0, 0);
  65         test(0, 1);
  66     }
  67 
  68     public static void test(int a, int b) throws InterruptedException {
  69         int C = compiled(4, a, b);
  70         int I = interpreted(4, a, b);
  71         if (C != I) {
  72             System.err.println("#1 C = " + C + ", I = " + I);
  73             System.err.println("#1 C != I, FAIL");
  74             System.exit(97);
  75         }
  76 
  77         C = compiled(a, b, q, 4);
  78         I = interpreted(a, b, q, 4);
  79         if (C != I) {
  80             System.err.println("#2 C = " + C + ", I = " + I);
  81             System.err.println("#2 C != I, FAIL");
  82             System.exit(97);
  83         }
  84 
  85     }
  86 
  87     static int q = 4;
  88 
  89     // If improperly compiled, uses carry/borrow bit, which is wrong.
  90     // with -XX:+PrintOptoAssembly, look for cadd_cmpLTMask
  91     static int compiled(int p, int x, int y) {
  92         return (x < y) ? q + (x - y) : (x - y);
  93     }
  94 
  95     // interpreted reference
  96     static int interpreted(int p, int x, int y) {
  97         return (x < y) ? q + (x - y) : (x - y);
  98     }
  99 
 100     // Test new code with a range of cases
 101     // with -XX:+PrintOptoAssembly, look for and_cmpLTMask
 102     static int compiled(int x, int y, int q, int p) {
 103         return (x < y) ? p + q : q;
 104     }
 105 
 106     // interpreted reference
 107     static int interpreted(int x, int y, int q, int p) {
 108         return (x < y) ? p + q : q;
 109     }
 110 
 111 }