|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +package compiler.integerArithmetic; |
| 26 | + |
| 27 | +import compiler.lib.ir_framework.*; |
| 28 | +import java.util.Random; |
| 29 | +import jdk.test.lib.Utils; |
| 30 | +import jdk.test.lib.Asserts; |
| 31 | + |
| 32 | +/* |
| 33 | + * @test |
| 34 | + * @bug 8352893 |
| 35 | + * @summary Test that an or with all bits set is folded to all bits (x | -1 == -1). |
| 36 | + * @key randomness |
| 37 | + * @library / /test/lib |
| 38 | + * @run driver compiler.integerArithmetic.TestOrSaturate |
| 39 | + */ |
| 40 | + |
| 41 | +public class TestOrSaturate { |
| 42 | + public static void main(String[] args) { |
| 43 | + TestFramework.run(); |
| 44 | + } |
| 45 | + |
| 46 | + private static final Random random = Utils.getRandomInstance(); |
| 47 | + |
| 48 | + @Run(test = {"testL", "testI", "testDelayed"}) |
| 49 | + public static void check() { |
| 50 | + Asserts.assertEQ(-1L, testL(random.nextLong())); |
| 51 | + Asserts.assertEQ(-1, testI(random.nextInt())); |
| 52 | + Asserts.assertEQ(-1, testDelayed(random.nextInt())); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @IR(failOn = { IRNode.OR_L }) |
| 57 | + // Tests that the OrLNode is folded if one operand is -1. |
| 58 | + private static long testL(long x) { |
| 59 | + return x | -1L; |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @IR(failOn = { IRNode.OR_I }) |
| 64 | + // Tests that the OrINode is folded if one operand is -1. |
| 65 | + private static int testI(int x) { |
| 66 | + return x | -1; |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @IR(counts = {IRNode.OR_I, "1"}, |
| 71 | + phase = CompilePhase.AFTER_PARSING) |
| 72 | + @IR(failOn = { IRNode.OR_I }) |
| 73 | + // Tests that the OrI node is folded after parsing if one operand is -1. |
| 74 | + private static int testDelayed(int x) { |
| 75 | + int min1 = 42; |
| 76 | + int limit = 2; |
| 77 | + |
| 78 | + // Ensure that min1 == -1 is only known after some constant propagation. |
| 79 | + for (; limit < 4; limit *= 2) {} |
| 80 | + for (int i = 2; i < limit; i++) { |
| 81 | + min1 = -1; |
| 82 | + } |
| 83 | + |
| 84 | + // Will only be folded after min1 == -1 is established. |
| 85 | + return x | min1; |
| 86 | + } |
| 87 | +} |
0 commit comments