|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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.irTests; |
| 24 | + |
| 25 | +import jdk.test.lib.Asserts; |
| 26 | +import compiler.lib.ir_framework.*; |
| 27 | +import java.util.Random; |
| 28 | +import jdk.test.lib.Utils; |
| 29 | + |
| 30 | +/* |
| 31 | + * @test |
| 32 | + * @bug 8341781 |
| 33 | + * @summary Test identities of MinNodes and MaxNodes. |
| 34 | + * @key randomness |
| 35 | + * @library /test/lib / |
| 36 | + * @run driver compiler.c2.irTests.TestMinMaxIdentities |
| 37 | + */ |
| 38 | + |
| 39 | +public class TestMinMaxIdentities { |
| 40 | + private static final Random RANDOM = Utils.getRandomInstance(); |
| 41 | + |
| 42 | + public static void main(String[] args) { |
| 43 | + TestFramework.run(); |
| 44 | + } |
| 45 | + |
| 46 | + @Run(test = { "intMinMin", "intMinMax", "intMaxMin", "intMaxMax", |
| 47 | + "longMinMin", "longMinMax", "longMaxMin", "longMaxMax", |
| 48 | + "floatMinMin", "floatMaxMax", "doubleMinMin", "doubleMaxMax", |
| 49 | + "floatMinMax", "floatMaxMin", "doubleMinMax", "doubleMaxMin" }) |
| 50 | + public void runMethod() { |
| 51 | + assertResult(10, 20, 10L, 20L, 10.f, 20.f, 10.0, 20.0); |
| 52 | + assertResult(20, 10, 20L, 10L, 20.f, 10.f, 20.0, 10.0); |
| 53 | + |
| 54 | + assertResult(RANDOM.nextInt(), RANDOM.nextInt(), RANDOM.nextLong(), RANDOM.nextLong(), RANDOM.nextFloat(), RANDOM.nextFloat(), RANDOM.nextDouble(), RANDOM.nextDouble()); |
| 55 | + assertResult(RANDOM.nextInt(), RANDOM.nextInt(), RANDOM.nextLong(), RANDOM.nextLong(), RANDOM.nextFloat(), RANDOM.nextFloat(), RANDOM.nextDouble(), RANDOM.nextDouble()); |
| 56 | + |
| 57 | + assertResult(Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE, Float.POSITIVE_INFINITY, Float.NaN, Double.POSITIVE_INFINITY, Double.NaN); |
| 58 | + assertResult(Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE, Float.NaN, Float.POSITIVE_INFINITY, Double.NaN, Double.POSITIVE_INFINITY); |
| 59 | + } |
| 60 | + |
| 61 | + @DontCompile |
| 62 | + public void assertResult(int iA, int iB, long lA, long lB, float fA, float fB, double dA, double dB) { |
| 63 | + Asserts.assertEQ(Math.min(iA, Math.min(iA, iB)), intMinMin(iA, iB)); |
| 64 | + Asserts.assertEQ(Math.min(iA, Math.max(iA, iB)), intMinMax(iA, iB)); |
| 65 | + Asserts.assertEQ(Math.max(iA, Math.min(iA, iB)), intMaxMin(iA, iB)); |
| 66 | + Asserts.assertEQ(Math.max(iA, Math.max(iA, iB)), intMaxMax(iA, iB)); |
| 67 | + |
| 68 | + Asserts.assertEQ(Math.min(lA, Math.min(lA, lB)), longMinMin(lA, lB)); |
| 69 | + Asserts.assertEQ(Math.min(lA, Math.max(lA, lB)), longMinMax(lA, lB)); |
| 70 | + Asserts.assertEQ(Math.max(lA, Math.min(lA, lB)), longMaxMin(lA, lB)); |
| 71 | + Asserts.assertEQ(Math.max(lA, Math.max(lA, lB)), longMaxMax(lA, lB)); |
| 72 | + |
| 73 | + Asserts.assertEQ(Math.min(fA, Math.min(fA, fB)), floatMinMin(fA, fB)); |
| 74 | + Asserts.assertEQ(Math.max(fA, Math.max(fA, fB)), floatMaxMax(fA, fB)); |
| 75 | + |
| 76 | + Asserts.assertEQ(Math.min(dA, Math.min(dA, dB)), doubleMinMin(dA, dB)); |
| 77 | + Asserts.assertEQ(Math.max(dA, Math.max(dA, dB)), doubleMaxMax(dA, dB)); |
| 78 | + |
| 79 | + // Due to NaN, these identities cannot be simplified. |
| 80 | + |
| 81 | + Asserts.assertEQ(Math.min(fA, Math.max(fA, fB)), floatMinMax(fA, fB)); |
| 82 | + Asserts.assertEQ(Math.max(fA, Math.min(fA, fB)), floatMaxMin(fA, fB)); |
| 83 | + Asserts.assertEQ(Math.min(dA, Math.max(dA, dB)), doubleMinMax(dA, dB)); |
| 84 | + Asserts.assertEQ(Math.max(dA, Math.min(dA, dB)), doubleMaxMin(dA, dB)); |
| 85 | + } |
| 86 | + |
| 87 | + // Integers |
| 88 | + |
| 89 | + @Test |
| 90 | + @IR(counts = { IRNode.MIN_I, "1" }) |
| 91 | + public int intMinMin(int a, int b) { |
| 92 | + return Math.min(a, Math.min(a, b)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @IR(failOn = { IRNode.MIN_I, IRNode.MAX_I }) |
| 97 | + public int intMinMax(int a, int b) { |
| 98 | + return Math.min(a, Math.max(a, b)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @IR(failOn = { IRNode.MIN_I, IRNode.MAX_I }) |
| 103 | + public int intMaxMin(int a, int b) { |
| 104 | + return Math.max(a, Math.min(a, b)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @IR(counts = { IRNode.MAX_I, "1" }) |
| 109 | + public int intMaxMax(int a, int b) { |
| 110 | + return Math.max(a, Math.max(a, b)); |
| 111 | + } |
| 112 | + |
| 113 | + // Longs |
| 114 | + |
| 115 | + // As Math.min/max(LL) is not intrinsified, it first needs to be transformed into CMoveL and then MinL/MaxL before |
| 116 | + // the identity can be matched. However, the outer min/max is not transformed into CMove because of the CMove cost model. |
| 117 | + // As JDK-8307513 adds intrinsics for the methods, the tests will be updated then. |
| 118 | + |
| 119 | + @Test |
| 120 | + @IR(applyIfPlatform = { "riscv64", "false" }, phase = { CompilePhase.BEFORE_MACRO_EXPANSION }, counts = { IRNode.MIN_L, "1" }) |
| 121 | + public long longMinMin(long a, long b) { |
| 122 | + return Math.min(a, Math.min(a, b)); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @IR(applyIfPlatform = { "riscv64", "false" }, phase = { CompilePhase.BEFORE_MACRO_EXPANSION }, counts = { IRNode.MIN_L, "1" }) |
| 127 | + public long longMinMax(long a, long b) { |
| 128 | + return Math.min(a, Math.max(a, b)); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + @IR(applyIfPlatform = { "riscv64", "false" }, phase = { CompilePhase.BEFORE_MACRO_EXPANSION }, counts = { IRNode.MAX_L, "1" }) |
| 133 | + public long longMaxMin(long a, long b) { |
| 134 | + return Math.max(a, Math.min(a, b)); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + @IR(applyIfPlatform = { "riscv64", "false" }, phase = { CompilePhase.BEFORE_MACRO_EXPANSION }, counts = { IRNode.MAX_L, "1" }) |
| 139 | + public long longMaxMax(long a, long b) { |
| 140 | + return Math.max(a, Math.max(a, b)); |
| 141 | + } |
| 142 | + |
| 143 | + // Floats |
| 144 | + |
| 145 | + @Test |
| 146 | + @IR(applyIfCPUFeatureOr = {"avx", "true", "asimd", "true", "rvv", "true"}, counts = { IRNode.MIN_F, "1" }) |
| 147 | + public float floatMinMin(float a, float b) { |
| 148 | + return Math.min(a, Math.min(a, b)); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + @IR(applyIfCPUFeatureOr = {"avx", "true", "asimd", "true", "rvv", "true"}, counts = { IRNode.MAX_F, "1" }) |
| 153 | + public float floatMaxMax(float a, float b) { |
| 154 | + return Math.max(a, Math.max(a, b)); |
| 155 | + } |
| 156 | + |
| 157 | + // Doubles |
| 158 | + |
| 159 | + @Test |
| 160 | + @IR(applyIfCPUFeatureOr = {"avx", "true", "asimd", "true", "rvv", "true"}, counts = { IRNode.MIN_D, "1" }) |
| 161 | + public double doubleMinMin(double a, double b) { |
| 162 | + return Math.min(a, Math.min(a, b)); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + @IR(applyIfCPUFeatureOr = {"avx", "true", "asimd", "true", "rvv", "true"}, counts = { IRNode.MAX_D, "1" }) |
| 167 | + public double doubleMaxMax(double a, double b) { |
| 168 | + return Math.max(a, Math.max(a, b)); |
| 169 | + } |
| 170 | + |
| 171 | + // Float and double identities that cannot be simplified due to NaN |
| 172 | + |
| 173 | + @Test |
| 174 | + @IR(applyIfCPUFeatureOr = {"avx", "true", "asimd", "true", "rvv", "true"}, counts = { IRNode.MIN_F, "1", IRNode.MAX_F, "1" }) |
| 175 | + public float floatMinMax(float a, float b) { |
| 176 | + return Math.min(a, Math.max(a, b)); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + @IR(applyIfCPUFeatureOr = {"avx", "true", "asimd", "true", "rvv", "true"}, counts = { IRNode.MIN_F, "1", IRNode.MAX_F, "1" }) |
| 181 | + public float floatMaxMin(float a, float b) { |
| 182 | + return Math.max(a, Math.min(a, b)); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + @IR(applyIfCPUFeatureOr = {"avx", "true", "asimd", "true", "rvv", "true"}, counts = { IRNode.MIN_D, "1", IRNode.MAX_D, "1" }) |
| 187 | + public double doubleMinMax(double a, double b) { |
| 188 | + return Math.min(a, Math.max(a, b)); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + @IR(applyIfCPUFeatureOr = {"avx", "true", "asimd", "true", "rvv", "true"}, counts = { IRNode.MIN_D, "1", IRNode.MAX_D, "1" }) |
| 193 | + public double doubleMaxMin(double a, double b) { |
| 194 | + return Math.max(a, Math.min(a, b)); |
| 195 | + } |
| 196 | +} |
0 commit comments