|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Red Hat 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 | +package compiler.c2; |
| 25 | + |
| 26 | +import compiler.lib.ir_framework.*; |
| 27 | +import compiler.lib.ir_framework.Test; |
| 28 | + |
| 29 | +import java.util.Random; |
| 30 | + |
| 31 | +/* |
| 32 | + * @test |
| 33 | + * @bug 8332442 |
| 34 | + * @summary Test that DIV and MOD nodes are converted into DIVMOD where possible |
| 35 | + * @library /test/lib / |
| 36 | + * @run driver compiler.c2.TestDivModNodes |
| 37 | + */ |
| 38 | +public class TestDivModNodes { |
| 39 | + private static final Random RANDOM = AbstractInfo.getRandom(); |
| 40 | + |
| 41 | + private static int intQuotient; |
| 42 | + private static int intRemainder; |
| 43 | + private static long longQuotient; |
| 44 | + private static long longRemainder; |
| 45 | + |
| 46 | + public static void main(String[] args) { |
| 47 | + TestFramework.runWithFlags("-XX:-UseDivMod"); |
| 48 | + TestFramework.runWithFlags("-XX:+UseDivMod"); |
| 49 | + } |
| 50 | + |
| 51 | + private static int nextNonZeroInt() { |
| 52 | + int i; |
| 53 | + do { |
| 54 | + i = RANDOM.nextInt(); |
| 55 | + } while (i == 0); |
| 56 | + return i; |
| 57 | + } |
| 58 | + |
| 59 | + private static long nextNonZeroLong() { |
| 60 | + long i; |
| 61 | + do { |
| 62 | + i = RANDOM.nextLong(); |
| 63 | + } while (i == 0); |
| 64 | + return i; |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @IR(applyIf = {"UseDivMod", "true"}, applyIfPlatform = {"x64", "true"}, |
| 69 | + counts = {IRNode.DIV_MOD_I, "1"}, |
| 70 | + failOn = {IRNode.DIV_I, IRNode.MOD_I}) |
| 71 | + @IR(applyIf = {"UseDivMod", "true"}, applyIfPlatform = {"aarch64", "true"}, |
| 72 | + counts = {IRNode.DIV_I, "1", IRNode.MUL_I, "1", IRNode.SUB_I, "1"}, |
| 73 | + failOn = {IRNode.MOD_I}) |
| 74 | + @IR(applyIf = {"UseDivMod", "false"}, |
| 75 | + counts = {IRNode.DIV_I, "1", IRNode.MOD_I, "1"}) |
| 76 | + private static void testSignedIntDivMod(int dividend, int divisor) { |
| 77 | + intQuotient = dividend / divisor; |
| 78 | + intRemainder = dividend % divisor; |
| 79 | + } |
| 80 | + |
| 81 | + @Run(test = "testSignedIntDivMod") |
| 82 | + private static void runSignedIntDivMod() { |
| 83 | + int dividend = RANDOM.nextInt(); |
| 84 | + int divisor = nextNonZeroInt(); |
| 85 | + testSignedIntDivMod(dividend, divisor); |
| 86 | + |
| 87 | + verifyResult(dividend, divisor, |
| 88 | + intQuotient, intRemainder, |
| 89 | + dividend / divisor, dividend % divisor); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + @Test |
| 94 | + @IR(applyIf = {"UseDivMod", "true"}, applyIfPlatform = {"x64", "true"}, |
| 95 | + counts = {IRNode.DIV_MOD_L, "1"}, |
| 96 | + failOn = {IRNode.DIV_L, IRNode.MOD_L}) |
| 97 | + @IR(applyIf = {"UseDivMod", "true"}, applyIfPlatform = {"aarch64", "true"}, |
| 98 | + counts = {IRNode.DIV_L, "1", IRNode.MUL_L, "1", IRNode.SUB_L, "1"}, |
| 99 | + failOn = {IRNode.MOD_L}) |
| 100 | + @IR(applyIf = {"UseDivMod", "false"}, |
| 101 | + counts = {IRNode.DIV_L, "1", IRNode.MOD_L, "1"}) |
| 102 | + private static void testSignedLongDivMod(long dividend, long divisor) { |
| 103 | + longQuotient = dividend / divisor; |
| 104 | + longRemainder = dividend % divisor; |
| 105 | + } |
| 106 | + |
| 107 | + @Run(test = "testSignedLongDivMod") |
| 108 | + private static void runSignedLongDivMod() { |
| 109 | + long dividend = RANDOM.nextLong(); |
| 110 | + long divisor = nextNonZeroLong(); |
| 111 | + testSignedLongDivMod(dividend, divisor); |
| 112 | + |
| 113 | + verifyResult(dividend, divisor, |
| 114 | + longQuotient, longRemainder, |
| 115 | + dividend / divisor, dividend % divisor); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + @IR(applyIf = {"UseDivMod", "true"}, applyIfPlatform = {"x64", "true"}, |
| 120 | + counts = {IRNode.UDIV_MOD_I, "1"}, |
| 121 | + failOn = {IRNode.UDIV_I, IRNode.UMOD_I}) |
| 122 | + @IR(applyIf = {"UseDivMod", "true"}, applyIfPlatform = {"aarch64", "true"}, |
| 123 | + counts = {IRNode.UDIV_I, "1", IRNode.MUL_I, "1", IRNode.SUB_I, "1"}, |
| 124 | + failOn = {IRNode.UMOD_I}) |
| 125 | + @IR(applyIf = {"UseDivMod", "false"}, |
| 126 | + counts = {IRNode.UDIV_I, "1", IRNode.UMOD_I, "1"}) |
| 127 | + private static void testUnsignedIntDivMod(int dividend, int divisor) { |
| 128 | + intQuotient = Integer.divideUnsigned(dividend, divisor); // intrinsified on x86 |
| 129 | + intRemainder = Integer.remainderUnsigned(dividend, divisor); // intrinsified on x86 |
| 130 | + } |
| 131 | + |
| 132 | + @Run(test = "testUnsignedIntDivMod") |
| 133 | + private static void runUnsignedIntDivMod() { |
| 134 | + int dividend = RANDOM.nextInt(); |
| 135 | + int divisor = nextNonZeroInt(); |
| 136 | + testUnsignedIntDivMod(dividend, divisor); |
| 137 | + |
| 138 | + verifyResult(dividend, divisor, |
| 139 | + intQuotient, intRemainder, |
| 140 | + Integer.divideUnsigned(dividend, divisor), Integer.remainderUnsigned(dividend, divisor)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + @IR(applyIf = {"UseDivMod", "true"}, applyIfPlatform = {"x64", "true"}, |
| 145 | + counts = {IRNode.UDIV_MOD_L, "1"}, |
| 146 | + failOn = {IRNode.UDIV_L, IRNode.UMOD_L}) |
| 147 | + @IR(applyIf = {"UseDivMod", "true"}, applyIfPlatform = {"aarch64", "true"}, |
| 148 | + counts = {IRNode.UDIV_L, "1", IRNode.MUL_L, "1", IRNode.SUB_L, "1"}, |
| 149 | + failOn = {IRNode.MOD_L}) |
| 150 | + @IR(applyIf = {"UseDivMod", "false"}, |
| 151 | + counts = {IRNode.UDIV_L, "1", IRNode.UMOD_L, "1"}) |
| 152 | + private static void testUnsignedLongDivMod(long dividend, long divisor) { |
| 153 | + longQuotient = Long.divideUnsigned(dividend, divisor); // intrinsified on x86 |
| 154 | + longRemainder = Long.remainderUnsigned(dividend, divisor); // intrinsified on x86 |
| 155 | + } |
| 156 | + |
| 157 | + @Run(test = "testUnsignedLongDivMod") |
| 158 | + private static void runUnsignedLongDivMod() { |
| 159 | + long dividend = RANDOM.nextLong(); |
| 160 | + long divisor = nextNonZeroLong(); |
| 161 | + testUnsignedLongDivMod(dividend, divisor); |
| 162 | + |
| 163 | + verifyResult(dividend, divisor, |
| 164 | + longQuotient, longRemainder, |
| 165 | + Long.divideUnsigned(dividend, divisor), Long.remainderUnsigned(dividend, divisor)); |
| 166 | + } |
| 167 | + |
| 168 | + private static <T extends Number> void verifyResult(T dividend, T divisor, |
| 169 | + T quotient, T remainder, |
| 170 | + T expectedQ, T expectedR) { |
| 171 | + if (!expectedQ.equals(quotient) || !expectedR.equals(remainder)) { |
| 172 | + throw new AssertionError(String.format("Mismatched result from %d / %d. " + |
| 173 | + "Expected: quotient = %d remainder = %d, " + |
| 174 | + "but got: quotient = %d remainder = %d", |
| 175 | + dividend, divisor, expectedQ, expectedR, quotient, remainder)); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments