From 9decd08b7b153a593a0b61e4f5373cb9574a1973 Mon Sep 17 00:00:00 2001 From: Uros Bizjak Date: Mon, 4 May 2020 18:53:30 +0200 Subject: [PATCH] i386: Use SBB more [PR94650] When returning 0 or -1, "SBB reg,reg" instruction that borrows carry flag can be used. Carry flag can be generated by converting compare with zero to a LTU compare with one, so e.g. return -(x == 0) generates: cmpq $1, %rdi sbbq %rax, %rax instead of: xorl %eax, %eax testq %rdi, %rdi sete %al negq %rax A similar conversion can be used for return -(x != 0) where NEG insn can be used instead of compare. According to x86 ISA, NEG insn sets carry flag when the source operand is != 0, resulting in: negq %rdi sbbq %rax, %rax The conversion avoids partial register stall with SETcc instructions. PR target/94795 * config/i386/i386.md (*neg_ccc): New insn pattern. (EQ compare->LTU compare splitter): New splitter. (NE compare->NEG splitter): Ditto. testsuite/ChangeLog: PR target/94795 * gcc.target/i386/pr94795-1.c: New test. * gcc.target/i386/pr94795-2.c: New test. --- gcc/config/i386/i386.md | 44 +++++++++++++++++++++-- gcc/testsuite/gcc.target/i386/pr94795-1.c | 21 +++++++++++ gcc/testsuite/gcc.target/i386/pr94795-2.c | 20 +++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr94795-1.c create mode 100644 gcc/testsuite/gcc.target/i386/pr94795-2.c diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index bd144ab3d5e..76c00867231 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -9900,6 +9900,17 @@ [(set_attr "type" "negnot") (set_attr "mode" "SI")]) +(define_insn "*neg_ccc" + [(set (reg:CCC FLAGS_REG) + (ne:CCC + (match_operand:SWI 1 "nonimmediate_operand" "0") + (const_int 0))) + (clobber (match_scratch:SWI 0 "="))] + "" + "neg{}\t%0" + [(set_attr "type" "negnot") + (set_attr "mode" "")]) + ;; Negate with jump on overflow. (define_expand "negv3" [(parallel [(set (reg:CCO FLAGS_REG) @@ -18015,9 +18026,9 @@ (set_attr "length_immediate" "0")]) (define_insn "*x86_movcc_0_m1_neg" - [(set (match_operand:SWI48 0 "register_operand" "=r") - (neg:SWI48 (match_operator 1 "ix86_carry_flag_operator" - [(reg FLAGS_REG) (const_int 0)]))) + [(set (match_operand:SWI 0 "register_operand" "=") + (neg:SWI (match_operator 1 "ix86_carry_flag_operator" + [(reg FLAGS_REG) (const_int 0)]))) (clobber (reg:CC FLAGS_REG))] "" "sbb{}\t%0, %0" @@ -18045,6 +18056,33 @@ (clobber (reg:CC FLAGS_REG))])] "operands[2] = GEN_INT (INTVAL (operands[2]) + 1);") +(define_split + [(set (match_operand:SWI 0 "register_operand") + (neg:SWI + (eq:SWI + (match_operand 1 "int_nonimmediate_operand") + (const_int 0))))] + "" + [(set (reg:CC FLAGS_REG) (compare:CC (match_dup 1) (const_int 1))) + (parallel [(set (match_dup 0) + (neg:SWI (ltu:SWI (reg:CC FLAGS_REG) (const_int 0)))) + (clobber (reg:CC FLAGS_REG))])]) + +(define_split + [(set (match_operand:SWI 0 "register_operand") + (neg:SWI + (ne:SWI + (match_operand 1 "int_nonimmediate_operand") + (const_int 0))))] + "" + [(parallel [(set (reg:CCC FLAGS_REG) + (ne:CCC (match_dup 1) (const_int 0))) + (clobber (match_dup 2))]) + (parallel [(set (match_dup 0) + (neg:SWI (ltu:SWI (reg:CCC FLAGS_REG) (const_int 0)))) + (clobber (reg:CC FLAGS_REG))])] + "operands[2] = gen_rtx_SCRATCH (GET_MODE (operands[1]));") + (define_insn "*movcc_noc" [(set (match_operand:SWI248 0 "register_operand" "=r,r") (if_then_else:SWI248 (match_operator 1 "ix86_comparison_operator" diff --git a/gcc/testsuite/gcc.target/i386/pr94795-1.c b/gcc/testsuite/gcc.target/i386/pr94795-1.c new file mode 100644 index 00000000000..c87a3dd4030 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr94795-1.c @@ -0,0 +1,21 @@ +/* PR target/94795 */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +char fooc (char x) +{ + return x ? -1 : 0; +} + +short foos (short x) +{ + return x ? -1 : 0; +} + +long fooi (long x) +{ + return x ? -1 : 0; +} + +/* { dg-final { scan-assembler-not "test|cmp" } } */ +/* { dg-final { scan-assembler-times "sbb" 3 } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr94795-2.c b/gcc/testsuite/gcc.target/i386/pr94795-2.c new file mode 100644 index 00000000000..87d76299a8a --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr94795-2.c @@ -0,0 +1,20 @@ +/* PR target/94795 */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +char fooc (char x) +{ + return -(x == 0); +} + +short foos (short x) +{ + return -(x == 0); +} + +long fooi (long x) +{ + return -(x == 0); +} + +/* { dg-final { scan-assembler-times "sbb" 3 } } */ -- 2.30.2