aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorUros Bizjak <ubizjak@gmail.com>2020-05-04 18:53:30 +0200
committerUros Bizjak <ubizjak@gmail.com>2020-05-04 18:53:30 +0200
commit9decd08b7b153a593a0b61e4f5373cb9574a1973 (patch)
treec5af1ef9949c4a9658cb54802bf8c280a9bcecc3 /gcc
parent97268c374a348a60c53366a4bee67626c840e4a1 (diff)
downloadgcc-9decd08b7b153a593a0b61e4f5373cb9574a1973.zip
gcc-9decd08b7b153a593a0b61e4f5373cb9574a1973.tar.gz
gcc-9decd08b7b153a593a0b61e4f5373cb9574a1973.tar.bz2
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<mode>_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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/config/i386/i386.md44
-rw-r--r--gcc/testsuite/gcc.target/i386/pr94795-1.c21
-rw-r--r--gcc/testsuite/gcc.target/i386/pr94795-2.c20
3 files changed, 82 insertions, 3 deletions
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index bd144ab..76c0086 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 0000000..c87a3dd
--- /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 0000000..87d7629
--- /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 } } */