diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 76f55ec..3090cea 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -7603,6 +7603,31 @@ "sub{l}\t{%2, %1|%1, %2}" [(set_attr "type" "alu") (set_attr "mode" "SI")]) + +;; PR 107991: Use peephole2 to avoid suffling before subtraction. +;; ax = si; cx = dx; dx = ax; dx -= cx where both si and cx +;; are dead becomes ax = si; si -= dx; dx = si. +(define_peephole2 + [(set (match_operand:SWI 0 "general_reg_operand") + (match_operand:SWI 1 "general_reg_operand")) + (set (match_operand:SWI 2 "general_reg_operand") + (match_operand:SWI 3 "general_reg_operand")) + (set (match_dup 3) (match_dup 0)) + (parallel + [(set (match_dup 3) (minus:SWI (match_dup 3) (match_dup 2))) + (clobber (reg:CC FLAGS_REG))])] + "REGNO (operands[0]) != REGNO (operands[1]) + && REGNO (operands[0]) != REGNO (operands[2]) + && REGNO (operands[0]) != REGNO (operands[3]) + && REGNO (operands[1]) != REGNO (operands[2]) + && REGNO (operands[1]) != REGNO (operands[3]) + && REGNO (operands[2]) != REGNO (operands[3]) + && peep2_reg_dead_p (1, operands[1]) + && peep2_reg_dead_p (4, operands[2])" + [(set (match_dup 0) (match_dup 1)) + (parallel [(set (match_dup 1) (minus:SWI (match_dup 1) (match_dup 3))) + (clobber (reg:CC FLAGS_REG))]) + (set (match_dup 3) (match_dup 1))]) ;; Add with carry and subtract with borrow diff --git a/gcc/testsuite/gcc.target/i386/pr107991.c b/gcc/testsuite/gcc.target/i386/pr107991.c new file mode 100644 index 0000000..9d0d9b6 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr107991.c @@ -0,0 +1,16 @@ +/* { dg-do compile { target { ! ia32 } } } */ +/* { dg-options "-O2" } */ + +int foo(_Bool b, int i, int j) { + return b ? i - j : i; +} + +int bar(_Bool b, int i, int j) { + return i + (b ? -j : 0); +} + +int baz(_Bool b, int i, int j) { + return i - (b ? j : 0); +} + +/* { dg-final { scan-assembler-times "movl" 3 } } */