From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3E6DB3858D20; Sun, 20 Nov 2022 18:48:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E6DB3858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668970095; bh=w5XYPdDorpwrG7Y/5BxQpRnsqgJarvl25fiT4vh7h0M=; h=From:To:Subject:Date:From; b=Uu9s7Fgsbkz0kR/6+1LtLXxwOWxJsxH/N0FUTAvukFzqu/eA3JimVlFrkC5AREK5+ bAmaj8kacNOPfZicbZ4S8oo5g8oFjnvg/7XFSRv6bI4UF717u2o4afERwLAmEvQie+ 4Ikh7nNtdZMO9fo7MNcBobBL3tH93dHO5T4Bzi4I= From: "avi at scylladb dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/107772] New: [missed optimization] function prologue generated even though it's only needed in an unlikely path Date: Sun, 20 Nov 2022 18:48:15 +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: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: avi at scylladb 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=3D107772 Bug ID: 107772 Summary: [missed optimization] function prologue generated even though it's only needed in an unlikely path Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: avi at scylladb dot com Target Milestone: --- Consider int g(int); void f(int* b, int* e) { while (b !=3D e) { if (__builtin_expect(*b !=3D 0, false)) [[unlikely]] { *b =3D g(*b); } ++b; } } If we believe the __builtin_expect and/or unlikely annotations (had both for extra safety), the loop usually does nothing. So we would expect any regist= er saving and restoring to be pushed to the unlikely section. Yet (-O3): f(int*, int*): cmp rdi, rsi je .L10 push rbp mov rbp, rsi push rbx mov rbx, rdi sub rsp, 8 .L4: mov edi, DWORD PTR [rbx] test edi, edi jne .L14 .L3: add rbx, 4 cmp rbp, rbx jne .L4 add rsp, 8 pop rbx pop rbp ret .L14: call g(int) mov DWORD PTR [rbx], eax jmp .L3 .L10: ret I count 8 instructions that could/should have been pushed to .L14.=