diff mbox series

[committed] i386: Use SBB more [PR94650]

Message ID CAFULd4ZGc-hBd_qzaRO1446Q4cNEB6b_eLwMaznJt9B+z8HpJA@mail.gmail.com
State New
Headers show
Series [committed] i386: Use SBB more [PR94650] | expand

Commit Message

Uros Bizjak May 4, 2020, 5:01 p.m. UTC
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.

2020-05-04  Uroš Bizjak  <ubizjak@gmail.com>

    PR target/94795
    * config/i386/i386.md (*neg<mode>_ccc): New insn pattern.
    (EQ compare->LTU compare splitter): New splitter.
    (NE compare->NEG splitter): Ditto.

testsuite/ChangeLog:

2020-05-04  Uroš Bizjak  <ubizjak@gmail.com>

    PR target/94795
    * gcc.target/i386/pr94795-1.c: New test.
    * gcc.target/i386/pr94795-2.c: Ditto.

Patch was bootstrapped and regression tested on x86_64-linux-gnu {,-m32}.

Committed to mainine.

Uros.
diff mbox series

Patch

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<mode>_ccc"
+  [(set (reg:CCC FLAGS_REG)
+	(ne:CCC
+	  (match_operand:SWI 1 "nonimmediate_operand" "0")
+	  (const_int 0)))
+   (clobber (match_scratch:SWI 0 "=<r>"))]
+  ""
+  "neg{<imodesuffix>}\t%0"
+  [(set_attr "type" "negnot")
+   (set_attr "mode" "<MODE>")])
+
 ;; Negate with jump on overflow.
 (define_expand "negv<mode>3"
   [(parallel [(set (reg:CCO FLAGS_REG)
@@ -18015,9 +18026,9 @@ 
    (set_attr "length_immediate" "0")])
 
 (define_insn "*x86_mov<mode>cc_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" "=<r>")
+	(neg:SWI (match_operator 1 "ix86_carry_flag_operator"
+		  [(reg FLAGS_REG) (const_int 0)])))
    (clobber (reg:CC FLAGS_REG))]
   ""
   "sbb{<imodesuffix>}\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 "*mov<mode>cc_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 } } */