|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 | +package compiler.c2.gvn; |
| 24 | + |
| 25 | +import compiler.lib.generators.Generators; |
| 26 | +import compiler.lib.ir_framework.*; |
| 27 | +import jdk.test.lib.Asserts; |
| 28 | + |
| 29 | +/* |
| 30 | + * @test |
| 31 | + * @bug 8375653 |
| 32 | + * @summary Test that Value computations of CmpUNode are being performed as expected. |
| 33 | + * @library /test/lib / |
| 34 | + * @run driver ${test.main.class} |
| 35 | + */ |
| 36 | +public class CmpUNodeValueTests { |
| 37 | + public static void main(String[] args) { |
| 38 | + TestFramework.run(); |
| 39 | + } |
| 40 | + |
| 41 | + @DontInline |
| 42 | + public static int one() { |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + // Move to a separate method to prevent javac folding the constant comparison |
| 47 | + @ForceInline |
| 48 | + public static int oneInline() { |
| 49 | + return 1; |
| 50 | + } |
| 51 | + |
| 52 | + @Run(test = {"testIntControl", "testIntLT", "testIntLE", "testIntGT", "testIntGE", "testIntEQ", "testIntNE", |
| 53 | + "testLongControl", "testLongLT", "testLongLE", "testLongGT", "testLongGE", "testLongEQ", "testLongNE"}) |
| 54 | + public void run() { |
| 55 | + var stream = Generators.G.longs(); |
| 56 | + long x = stream.next(); |
| 57 | + long y = stream.next(); |
| 58 | + |
| 59 | + Asserts.assertEQ(0, testIntControl(false, false)); |
| 60 | + Asserts.assertEQ(0, testIntControl(false, true)); |
| 61 | + Asserts.assertEQ(0, testIntControl(true, false)); |
| 62 | + Asserts.assertEQ(1, testIntControl(true, true)); |
| 63 | + Asserts.assertEQ(0, testIntLT((int) x)); |
| 64 | + Asserts.assertEQ(0, testIntLE(false, false)); |
| 65 | + Asserts.assertEQ(0, testIntLE(false, true)); |
| 66 | + Asserts.assertEQ(0, testIntLE(true, false)); |
| 67 | + Asserts.assertEQ(0, testIntLE(true, true)); |
| 68 | + Asserts.assertEQ(0, testIntGT(false, false)); |
| 69 | + Asserts.assertEQ(0, testIntGT(false, true)); |
| 70 | + Asserts.assertEQ(0, testIntGT(true, false)); |
| 71 | + Asserts.assertEQ(0, testIntGT(true, true)); |
| 72 | + Asserts.assertEQ(0, testIntGE((int) y)); |
| 73 | + Asserts.assertEQ(0, testIntEQ()); |
| 74 | + Asserts.assertEQ(0, testIntNE(false, false)); |
| 75 | + Asserts.assertEQ(0, testIntNE(false, true)); |
| 76 | + Asserts.assertEQ(0, testIntNE(true, false)); |
| 77 | + Asserts.assertEQ(0, testIntNE(true, true)); |
| 78 | + |
| 79 | + Asserts.assertEQ(0, testLongControl(false, false)); |
| 80 | + Asserts.assertEQ(0, testLongControl(false, true)); |
| 81 | + Asserts.assertEQ(0, testLongControl(true, false)); |
| 82 | + Asserts.assertEQ(1, testLongControl(true, true)); |
| 83 | + Asserts.assertEQ(0, testLongLT((int) x, false)); |
| 84 | + Asserts.assertEQ(0, testLongLT((int) x, true)); |
| 85 | + Asserts.assertEQ(0, testLongLE(false, false)); |
| 86 | + Asserts.assertEQ(0, testLongLE(false, true)); |
| 87 | + Asserts.assertEQ(0, testLongLE(true, false)); |
| 88 | + Asserts.assertEQ(0, testLongLE(true, true)); |
| 89 | + Asserts.assertEQ(0, testLongGT(false, false)); |
| 90 | + Asserts.assertEQ(0, testLongGT(false, true)); |
| 91 | + Asserts.assertEQ(0, testLongGT(true, false)); |
| 92 | + Asserts.assertEQ(0, testLongGT(true, true)); |
| 93 | + Asserts.assertEQ(0, testLongGE((int) y)); |
| 94 | + Asserts.assertEQ(0, testLongEQ()); |
| 95 | + Asserts.assertEQ(0, testLongNE(false, false)); |
| 96 | + Asserts.assertEQ(0, testLongNE(false, true)); |
| 97 | + Asserts.assertEQ(0, testLongNE(true, false)); |
| 98 | + Asserts.assertEQ(0, testLongNE(true, true)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, counts = {IRNode.CMP_U, "1"}) |
| 103 | + int testIntControl(boolean b1, boolean b2) { |
| 104 | + int v1 = b1 ? 1 : -1; |
| 105 | + int v2 = b2 ? 1 : 0; |
| 106 | + return Integer.compareUnsigned(v1, v2) > 0 ? 0 : one(); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_U, IRNode.CALL}) |
| 111 | + int testIntLT(int x) { |
| 112 | + int v1 = x & Integer.MAX_VALUE; // Unset the highest bit, make v1 non-negative |
| 113 | + int v2 = Integer.MIN_VALUE; |
| 114 | + return Integer.compareUnsigned(v1, v2) < 0 ? 0 : one(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_U, IRNode.CALL}) |
| 119 | + int testIntLE(boolean b1, boolean b2) { |
| 120 | + int v1 = b1 ? 2 : 0; |
| 121 | + int v2 = b2 ? -1 : 2; |
| 122 | + return Integer.compareUnsigned(v1, v2) <= 0 ? 0 : one(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_U, IRNode.CALL}) |
| 127 | + int testIntGT(boolean b1, boolean b2) { |
| 128 | + int v1 = b1 ? Integer.MIN_VALUE : Integer.MAX_VALUE; |
| 129 | + int v2 = b2 ? 0 : 2; |
| 130 | + return Integer.compareUnsigned(v1, v2) > 0 ? 0 : one(); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_U, IRNode.CALL}) |
| 135 | + int testIntGE(int y) { |
| 136 | + int v2 = y & Integer.MAX_VALUE; // Unset the highest bit, make v2 non-negative |
| 137 | + return Integer.compareUnsigned(Integer.MIN_VALUE, v2) >= 0 ? 0 : one(); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_U, IRNode.CALL}) |
| 142 | + int testIntEQ() { |
| 143 | + return Integer.compareUnsigned(oneInline(), 1) == 0 ? 0 : one(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_U, IRNode.CALL}) |
| 148 | + int testIntNE(boolean b1, boolean b2) { |
| 149 | + int v1 = b1 ? 1 : -1; |
| 150 | + int v2 = b2 ? 0 : 2; |
| 151 | + return Integer.compareUnsigned(v1, v2) != 0 ? 0 : one(); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, counts = {IRNode.CMP_UL, "1"}) |
| 156 | + int testLongControl(boolean b1, boolean b2) { |
| 157 | + long v1 = b1 ? 1 : -1; |
| 158 | + long v2 = b2 ? 1 : 0; |
| 159 | + return Long.compareUnsigned(v1, v2) > 0 ? 0 : one(); |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + @Test |
| 164 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_UL, IRNode.CALL}) |
| 165 | + int testLongLT(int x, boolean b2) { |
| 166 | + long v1 = Integer.toUnsignedLong(x); |
| 167 | + long v2 = Integer.MIN_VALUE; |
| 168 | + return Long.compareUnsigned(v1, v2) < 0 ? 0 : one(); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_UL, IRNode.CALL}) |
| 173 | + int testLongLE(boolean b1, boolean b2) { |
| 174 | + long v1 = b1 ? 2 : 0; |
| 175 | + long v2 = b2 ? -1 : 2; |
| 176 | + return Long.compareUnsigned(v1, v2) <= 0 ? 0 : one(); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_UL, IRNode.CALL}) |
| 181 | + int testLongGT(boolean b1, boolean b2) { |
| 182 | + long v1 = b1 ? Long.MIN_VALUE : Long.MAX_VALUE; |
| 183 | + long v2 = b2 ? 0 : 2; |
| 184 | + return Long.compareUnsigned(v1, v2) > 0 ? 0 : one(); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_UL, IRNode.CALL}) |
| 189 | + int testLongGE(int y) { |
| 190 | + long v2 = y & Long.MAX_VALUE; // Unset the highest bit, make v2 non-negative |
| 191 | + return Long.compareUnsigned(Long.MIN_VALUE, v2) >= 0 ? 0 : one(); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_UL, IRNode.CALL}) |
| 196 | + int testLongEQ() { |
| 197 | + return Long.compareUnsigned(oneInline(), 1L) == 0 ? 0 : one(); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + @IR(applyIfPlatformOr = {"x64", "true", "aarch64", "true"}, failOn = {IRNode.CMP_UL, IRNode.CALL}) |
| 202 | + int testLongNE(boolean b1, boolean b2) { |
| 203 | + long v1 = b1 ? 1 : -1; |
| 204 | + long v2 = b2 ? 0 : 2; |
| 205 | + return Long.compareUnsigned(v1, v2) != 0 ? 0 : one(); |
| 206 | + } |
| 207 | +} |
0 commit comments