From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1251) id D755A3858D20; Mon, 28 Feb 2022 22:32:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D755A3858D20 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Roger Sayle To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-7417] PR tree-optimization/91384: peephole2 to eliminate testl after negl. X-Act-Checkin: gcc X-Git-Author: Roger Sayle X-Git-Refname: refs/heads/master X-Git-Oldrev: 7e5c6edeb1b2339e10f10bee270e61dbad985800 X-Git-Newrev: 28068d1115648adcc08ae57372170f3277915a0d Message-Id: <20220228223257.D755A3858D20@sourceware.org> Date: Mon, 28 Feb 2022 22:32:57 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Feb 2022 22:32:57 -0000 https://gcc.gnu.org/g:28068d1115648adcc08ae57372170f3277915a0d commit r12-7417-g28068d1115648adcc08ae57372170f3277915a0d Author: Roger Sayle Date: Mon Feb 28 22:30:27 2022 +0000 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 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. Diff: --- gcc/config/i386/i386.md | 13 +++++++++++++ gcc/testsuite/gcc.target/i386/pr91384.c | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index e7c54901d75..5e0a980e7ae 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -11011,6 +11011,19 @@ [(set_attr "type" "negnot") (set_attr "mode" "")]) +;; Optimize *negsi_1 followed by *cmpsi_ccno_1 (PR target/91384) +(define_peephole2 + [(set (match_operand:SWI 0 "general_reg_operand") + (match_operand:SWI 1 "general_reg_operand")) + (parallel [(set (match_dup 0) (neg:SWI (match_dup 0))) + (clobber (reg:CC FLAGS_REG))]) + (set (reg:CCZ FLAGS_REG) (compare:CCZ (match_dup 1) (const_int 0)))] + "" + [(set (match_dup 0) (match_dup 1)) + (parallel [(set (reg:CCZ FLAGS_REG) + (compare:CCZ (neg:SWI (match_dup 0)) (const_int 0))) + (set (match_dup 0) (neg:SWI (match_dup 0)))])]) + ;; Special expand pattern to handle integer mode abs (define_expand "abs2" diff --git a/gcc/testsuite/gcc.target/i386/pr91384.c b/gcc/testsuite/gcc.target/i386/pr91384.c new file mode 100644 index 00000000000..24a60a932ab --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr91384.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +void foo (void); +void bar (void); + +int +test (int a) +{ + int r; + + if (r = -a) + foo (); + else + bar (); + + return r; +} + +/* { dg-final { scan-assembler-not "testl" } } */