PR tree-optimization/91384: peephole2 to eliminate testl after negl.
authorRoger Sayle <roger@nextmovesoftware.com>
Mon, 28 Feb 2022 22:30:27 +0000 (22:30 +0000)
committerRoger Sayle <roger@nextmovesoftware.com>
Mon, 28 Feb 2022 22:32:12 +0000 (22:32 +0000)
commit28068d1115648adcc08ae57372170f3277915a0d
treee396a3e29860a7f6ce889b1f4197910706de3ad9
parent7e5c6edeb1b2339e10f10bee270e61dbad985800
PR tree-optimization/91384: peephole2 to eliminate testl after negl.

This patch is my proposed solution to PR tree-optimization/91384 which is
a missed-optimization/code quality regression on x86_64.  The problematic
idiom is "if (r = -a)" which is equivalent to both "r = -a; if (r != 0)"
and alternatively "r = -a; if (a != 0)".  In this particular case, on
x86_64, we prefer to use the condition codes from the negation, rather
than require an explicit testl instruction.

Unfortunately, combine can't help, as it doesn't attempt to merge pairs
of instructions that share the same operand(s), only pairs/triples of
instructions where the result of each instruction feeds the next.  But
I doubt there's sufficient benefit to attempt this kind of "combination"
(that wouldn't already be caught by the tree-ssa passes).

Fortunately, it's relatively easy to fix this up (addressing the
regression) during peephole2 to eliminate the unnecessary testl in:

        movl    %edi, %ebx
        negl    %ebx
        testl   %edi, %edi
        je      .L2

2022-02-28  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
PR tree-optimization/91384
* config/i386/i386.md (peephole2): Eliminate final testl insn
from the sequence *movsi_internal, *negsi_1, *cmpsi_ccno_1 by
transforming using *negsi_2 for the negation.

gcc/testsuite/ChangeLog
PR tree-optimization/91384
* gcc.target/i386/pr91384.c: New test case.
gcc/config/i386/i386.md
gcc/testsuite/gcc.target/i386/pr91384.c [new file with mode: 0644]