From 6f8714ee197eb48923209299fd842f6757f0a945 Mon Sep 17 00:00:00 2001 From: Jasmine Karthikeyan Date: Wed, 4 Sep 2024 13:44:24 +0000 Subject: [PATCH] 8336860: x86: Change integer src operand for CMoveL of 0 and 1 to long Reviewed-by: epeter, chagedorn, shade, qamai, jbhateja --- src/hotspot/cpu/x86/x86_64.ad | 6 +- .../compiler/c2/irTests/CMoveLConstants.java | 71 +++++++++++++++++++ .../compiler/lib/ir_framework/IRNode.java | 15 ++++ .../bench/vm/compiler/x86/BasicRules.java | 21 +++++- 4 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 test/hotspot/jtreg/compiler/c2/irTests/CMoveLConstants.java diff --git a/src/hotspot/cpu/x86/x86_64.ad b/src/hotspot/cpu/x86/x86_64.ad index 3bc4cac2f06..e0740ad7f31 100644 --- a/src/hotspot/cpu/x86/x86_64.ad +++ b/src/hotspot/cpu/x86/x86_64.ad @@ -6388,7 +6388,7 @@ instruct cmovP_regUCF2_eq(cmpOpUCF2 cop, rFlagsRegUCF cr, rRegP dst, rRegP src) ins_pipe(pipe_cmov_reg); %} -instruct cmovL_imm_01(rRegL dst, immI_1 src, rFlagsReg cr, cmpOp cop) +instruct cmovL_imm_01(rRegL dst, immL1 src, rFlagsReg cr, cmpOp cop) %{ predicate(n->in(2)->in(2)->is_Con() && n->in(2)->in(2)->get_long() == 0); match(Set dst (CMoveL (Binary cop cr) (Binary src dst))); @@ -6426,7 +6426,7 @@ instruct cmovL_mem(cmpOp cop, rFlagsReg cr, rRegL dst, memory src) ins_pipe(pipe_cmov_mem); // XXX %} -instruct cmovL_imm_01U(rRegL dst, immI_1 src, rFlagsRegU cr, cmpOpU cop) +instruct cmovL_imm_01U(rRegL dst, immL1 src, rFlagsRegU cr, cmpOpU cop) %{ predicate(n->in(2)->in(2)->is_Con() && n->in(2)->in(2)->get_long() == 0); match(Set dst (CMoveL (Binary cop cr) (Binary src dst))); @@ -6452,7 +6452,7 @@ instruct cmovL_regU(cmpOpU cop, rFlagsRegU cr, rRegL dst, rRegL src) ins_pipe(pipe_cmov_reg); // XXX %} -instruct cmovL_imm_01UCF(rRegL dst, immI_1 src, rFlagsRegUCF cr, cmpOpUCF cop) +instruct cmovL_imm_01UCF(rRegL dst, immL1 src, rFlagsRegUCF cr, cmpOpUCF cop) %{ predicate(n->in(2)->in(2)->is_Con() && n->in(2)->in(2)->get_long() == 0); match(Set dst (CMoveL (Binary cop cr) (Binary src dst))); diff --git a/test/hotspot/jtreg/compiler/c2/irTests/CMoveLConstants.java b/test/hotspot/jtreg/compiler/c2/irTests/CMoveLConstants.java new file mode 100644 index 00000000000..e62e1adc8d0 --- /dev/null +++ b/test/hotspot/jtreg/compiler/c2/irTests/CMoveLConstants.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. 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. + */ + +package compiler.c2.irTests; + +import jdk.test.lib.Asserts; +import compiler.lib.ir_framework.*; + +/* + * @test + * bug 8336860 + * @summary Verify codegen for CMoveL with constants 0 and 1 + * @library /test/lib / + * @run driver compiler.c2.irTests.CMoveLConstants + */ +public class CMoveLConstants { + public static void main(String[] args) { + TestFramework.run(); + } + + @Test + @IR(applyIfPlatform = {"x64", "true"}, counts = {IRNode.X86_CMOVEL_IMM01, "1"}, phase = CompilePhase.FINAL_CODE) + public static long testSigned(int a, int b) { + return a > b ? 1L : 0L; + } + + @Test + @IR(applyIfPlatform = {"x64", "true"}, counts = {IRNode.X86_CMOVEL_IMM01U, "1"}, phase = CompilePhase.FINAL_CODE) + public static long testUnsigned(int a, int b) { + return Integer.compareUnsigned(a, b) > 0 ? 1L : 0L; + } + + @Test + @IR(applyIfPlatform = {"x64", "true"}, counts = {IRNode.X86_CMOVEL_IMM01UCF, "1"}, phase = CompilePhase.FINAL_CODE) + public static long testFloat(float a, float b) { + return a > b ? 1L : 0L; + } + + @DontCompile + public void assertResults(int a, int b) { + Asserts.assertEQ(a > b ? 1L : 0L, testSigned(a, b)); + Asserts.assertEQ(Integer.compareUnsigned(a, b) > 0 ? 1L : 0L, testUnsigned(a, b)); + Asserts.assertEQ((float) a > (float) b ? 1L : 0L, testFloat(a, b)); + } + + @Run(test = {"testSigned", "testUnsigned", "testFloat"}) + public void runMethod() { + assertResults(10, 20); + assertResults(20, 10); + } +} diff --git a/test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java b/test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java index 1b554df9e67..162f384dc07 100644 --- a/test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java +++ b/test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java @@ -2233,6 +2233,21 @@ public class IRNode { machOnlyNameRegex(X86_TESTL_REG, "testL_reg"); } + public static final String X86_CMOVEL_IMM01 = PREFIX + "X86_CMOVEL_IMM01" + POSTFIX; + static { + machOnlyNameRegex(X86_CMOVEL_IMM01, "cmovL_imm_01"); + } + + public static final String X86_CMOVEL_IMM01U = PREFIX + "X86_CMOVEL_IMM01U" + POSTFIX; + static { + machOnlyNameRegex(X86_CMOVEL_IMM01U, "cmovL_imm_01U"); + } + + public static final String X86_CMOVEL_IMM01UCF = PREFIX + "X86_CMOVEL_IMM01UCF" + POSTFIX; + static { + machOnlyNameRegex(X86_CMOVEL_IMM01UCF, "cmovL_imm_01UCF"); + } + /* * Utility methods to set up IR_NODE_MAPPINGS. */ diff --git a/test/micro/org/openjdk/bench/vm/compiler/x86/BasicRules.java b/test/micro/org/openjdk/bench/vm/compiler/x86/BasicRules.java index 35a346d3ed8..4b04e168f16 100644 --- a/test/micro/org/openjdk/bench/vm/compiler/x86/BasicRules.java +++ b/test/micro/org/openjdk/bench/vm/compiler/x86/BasicRules.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. 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 @@ -26,6 +26,8 @@ import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.infra.Blackhole; +import java.util.Random; + @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @State(Scope.Thread) @@ -38,6 +40,16 @@ public class BasicRules { static final int INT_IMM = 100; static final long LONG_IMM = 100; + @Setup(Level.Iteration) + public void setup() { + Random random = new Random(1000); + + for (int i = 0; i < 1024; i++) { + INT_ARRAY[i] = random.nextInt(); + LONG_ARRAY[i] = random.nextLong(); + } + } + @Benchmark public void andL_rReg_imm255(Blackhole bh) { for (int i = 0; i < LONG_ARRAY.length; i++) { @@ -125,5 +137,12 @@ public class BasicRules { bh.consume(LONG_ARRAY[i] - LONG_IMM); } } + + @Benchmark + public void cmovL_imm_01(Blackhole bh) { + for (int i = 0; i < INT_ARRAY.length; i++) { + bh.consume(INT_ARRAY[i] > 0 ? 1L : 0L); + } + } }