From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id EFB723851C2A; Thu, 10 Dec 2020 12:40:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EFB723851C2A From: "ubizjak at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/91384] [8/9/10/11 Regression] Compare with negation is not eliminated Date: Thu, 10 Dec 2020 12:40:15 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 9.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: ubizjak at gmail dot com X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 8.5 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Dec 2020 12:40:16 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D91384 --- Comment #8 from Uro=C5=A1 Bizjak --- (In reply to Jakub Jelinek from comment #1) > Started with r223689. Though, generally that change looks like a useful > GIMPLE canonicalization. How about we amend the above change to: diff --git a/gcc/match.pd b/gcc/match.pd index 43bacb4f68e..1370ba7302d 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4506,6 +4506,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cmp (negate @0) CONSTANT_CLASS_P@1) (if (FLOAT_TYPE_P (TREE_TYPE (@0)) || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && integer_nonzerop (@0) && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) (with { tree tem =3D const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); } (if (tem && !TREE_OVERFLOW (tem)) so we simplify (cmp (negate @0) CONSTANT_CLASS_P@1)) only for non-zero inte= ger arguments. For the original testcase, the compare is eliminated: test: negl %edi pushq %r12 movl %edi, %r12d je .L2 call foo movl %r12d, %eax popq %r12 ret .L2: call bar movl %r12d, %eax popq %r12 ret Please also note that for the slightly changed original testcase: --cut here-- void f1 (void); void f2 (void); void foo (int a) { int r =3D -a; if (r) f1 (); else f2 (); } --cut here-- combine RTL pass manages to remove the negation without problems: Trying 6 -> 7: 6: {r84:SI=3D-r85:SI;clobber flags:CC;} REG_DEAD r85:SI REG_UNUSED flags:CC 7: flags:CCZ=3Dcmp(r84:SI,0) REG_DEAD r84:SI Successfully matched this instruction: (set (reg:CCZ 17 flags) (compare:CCZ (reg:SI 85) (const_int 0 [0]))) so the compilation (-O2) results in the same assembly: foo: testl %edi, %edi je .L2 jmp f1 .L2: jmp f2=