changeset 50866:929cd9246fc9

8204479: Bitwise AND on byte value sometimes produces wrong result Reviewed-by: kvn, thartmann
author shade
date Fri, 08 Jun 2018 11:41:43 +0200
parents 1f49c9794ad8
children 102ae98c917c
files src/hotspot/cpu/x86/x86_64.ad test/hotspot/jtreg/compiler/c2/TestUnsignedByteCompare.java
diffstat 2 files changed, 97 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/cpu/x86/x86_64.ad	Fri Jun 08 11:11:06 2018 +0200
+++ b/src/hotspot/cpu/x86/x86_64.ad	Fri Jun 08 11:41:43 2018 +0200
@@ -11607,9 +11607,9 @@
   ins_pipe(ialu_cr_reg_imm);
 %}
 
-instruct compUB_mem_imm(rFlagsReg cr, memory mem, immU8 imm)
-%{
-  match(Set cr (CmpI (LoadUB mem) imm));
+instruct compB_mem_imm(rFlagsReg cr, memory mem, immI8 imm)
+%{
+  match(Set cr (CmpI (LoadB mem) imm));
 
   ins_cost(125);
   format %{ "cmpb    $mem, $imm" %}
@@ -11617,26 +11617,6 @@
   ins_pipe(ialu_cr_reg_mem);
 %}
 
-instruct compB_mem_imm(rFlagsReg cr, memory mem, immI8 imm)
-%{
-  match(Set cr (CmpI (LoadB mem) imm));
-
-  ins_cost(125);
-  format %{ "cmpb    $mem, $imm" %}
-  ins_encode %{ __ cmpb($mem$$Address, $imm$$constant); %}
-  ins_pipe(ialu_cr_reg_mem);
-%}
-
-instruct testUB_mem_imm(rFlagsReg cr, memory mem, immU8 imm, immI0 zero)
-%{
-  match(Set cr (CmpI (AndI (LoadUB mem) imm) zero));
-
-  ins_cost(125);
-  format %{ "testb   $mem, $imm" %}
-  ins_encode %{ __ testb($mem$$Address, $imm$$constant); %}
-  ins_pipe(ialu_cr_reg_mem);
-%}
-
 instruct testB_mem_imm(rFlagsReg cr, memory mem, immI8 imm, immI0 zero)
 %{
   match(Set cr (CmpI (AndI (LoadB mem) imm) zero));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/compiler/c2/TestUnsignedByteCompare.java	Fri Jun 08 11:41:43 2018 +0200
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2018, Red Hat Inc. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8204479
+ * @summary Bitwise AND on byte value sometimes produces wrong result
+ *
+ * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation
+ *      -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -Xcomp -XX:-Inline
+ *      compiler.c2.TestUnsignedByteCompare
+ */
+
+package compiler.c2;
+
+public class TestUnsignedByteCompare {
+
+    static int p, n;
+
+    static void report(byte[] ba, int i, boolean failed) {
+        // Enable for debugging:
+        // System.out.println((failed ? "Failed" : "Passed") + " with: " + ba[i] + " at " + i);
+    }
+
+    static void m1(byte[] ba) {
+        for (int i = 0; i < ba.length; i++) {
+            if ((ba[i] & 0xFF) < 0x10) {
+               p++;
+               report(ba, i, true);
+            } else {
+               n++;
+               report(ba, i, false);
+            }
+        }
+    }
+
+    static void m2(byte[] ba) {
+        for (int i = 0; i < ba.length; i++) {
+            if (((ba[i] & 0xFF) & 0x80) < 0) {
+               p++;
+               report(ba, i, true);
+            } else {
+               n++;
+               report(ba, i, false);
+            }
+        }
+    }
+
+    static public void main(String[] args) {
+        final int tries = 1_000;
+        final int count = 1_000;
+
+        byte[] ba = new byte[count];
+
+        for (int i = 0; i < count; i++) {
+            int v = -(i % 126 + 1);
+            ba[i] = (byte)v;
+        }
+
+        for (int t = 0; t < tries; t++) {
+            m1(ba);
+            if (p != 0) {
+                throw new IllegalStateException("m1 error: p = " + p + ", n = " + n);
+            }
+        }
+
+        for (int t = 0; t < tries; t++) {
+            m2(ba);
+            if (p != 0) {
+                throw new IllegalStateException("m2 error: p = " + p + ", n = " + n);
+            }
+        }
+    }
+}