From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6EB413858D1E; Thu, 28 Mar 2024 18:09:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6EB413858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1711649363; bh=FRwWG6gP17q68ymluqKHc2FAqyyyjiiHKYsYzj2kphk=; h=From:To:Subject:Date:From; b=ooT/seZ5EiN+g/qF7Vkn8dAF7gr+EFIf/i7q/oSY4JAKA+0mj1wjtE8iDjUViEDlW g+VW6miPmS3IiXOb5KZv8W1MXD1GwQQIWjGiBI/2GsMUSEJ8HtsjSYn26pYlTp8x88 LFwuZbV3yVgLL7eG5OA99HB6nbBDmqmyovLkKg+g= From: "goon.pri.low at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/114524] New: Use less expensive expression when expressions are equal Date: Thu, 28 Mar 2024 18:09:23 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: goon.pri.low at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114524 Bug ID: 114524 Summary: Use less expensive expression when expressions are equal Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: goon.pri.low at gmail dot com Target Milestone: --- When we know multiple expressions are equal, we should only evaluate the le= ast expensive one. Example functions: int a(int b, int *restrict c) { if (b !=3D *c) __builtin_unreachable(); return *c; } int b(int b, int c) { if (b !=3D c + 1) __builtin_unreachable(); return c + 1; } int c(int b, struct {int a; int b;} c) { if (b !=3D c.b) __builtin_unreachable(); return c.b; } int d(int b, int c) { if (b * 44 !=3D c / 10) __builtin_unreachable(); return c / 10; } Generated assembly: a: mov eax, DWORD PTR [rsi] ret b: lea eax, [rsi+1] ret c: shr rsi, 32 mov eax, esi ret d: movsx rax, esi sar esi, 31 imul rax, rax, 1717986919 sar rax, 34 sub eax, esi ret Optimal assembly (clang) a: # @a mov eax, edi ret b: # @b mov eax, edi ret c: # @c mov eax, edi ret d: # @d imul eax, edi, 44 ret=