From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4592D3857341; Fri, 6 May 2022 07:40:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4592D3857341 From: "lh_mouse at 126 dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/105495] `__atomic_compare_exchange` prevents tail-call optimization Date: Fri, 06 May 2022 07:40:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 11.3.1 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: lh_mouse at 126 dot com X-Bugzilla-Status: NEW 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: 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: Fri, 06 May 2022 07:40:33 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105495 --- Comment #5 from LIU Hao --- This does not trigger the issue: ```c #define __atomic_compare_exchange(p,c,n,w,ms,mf) \ ({ int __temp; \ __builtin_memcpy(&__temp, c, sizeof(*c)); \ _Bool __r =3D __atomic_compare_exchange(p, (__typeof__(*(c))*) &__temp= , n, w, ms, mf); \ __builtin_memcpy(c, &__temp, sizeof(*c)); \ __r; }); typedef struct { int b; } cond; int __MCF_batch_release_common(cond* p, int c); int _MCF_cond_signal_some(cond* p, int x) { cond c =3D {x}, n =3D {2}; __atomic_compare_exchange(p, &c, &n, 1, 0, 0); return __MCF_batch_release_common(p, x); } ``` which results in (godbolt https://gcc.godbolt.org/z/n68T1c6oP): ```asm _MCF_cond_signal_some: mov edx, 2 mov eax, esi lock cmpxchg DWORD PTR [rdi], edx jmp __MCF_batch_release_common ``` Effectively, we are using a `int` to provide storage for a struct of `sizeof(int)`. But if we use a `long` to provide storage for the struct, such issue reappe= ars: ```c #define __atomic_compare_exchange(p,c,n,w,ms,mf) \ ({ long __temp; \ __builtin_memcpy(&__temp, c, sizeof(*c)); \ _Bool __r =3D __atomic_compare_exchange(p, (__typeof__(*(c))*) &__temp= , n, w, ms, mf); \ __builtin_memcpy(c, &__temp, sizeof(*c)); \ __r; }); typedef struct { int b; } cond; int __MCF_batch_release_common(cond* p, int c); int _MCF_cond_signal_some(cond* p, int x) { cond c =3D {x}, n =3D {2}; __atomic_compare_exchange(p, &c, &n, 1, 0, 0); return __MCF_batch_release_common(p, x); } ``` which results in (https://gcc.godbolt.org/z/PGof8nGd7) ```asm _MCF_cond_signal_some: mov edx, 2 mov eax, esi mov DWORD PTR [rsp-16], esi lock cmpxchg DWORD PTR [rdi], edx je .L2 mov DWORD PTR [rsp-16], eax .L2: jmp __MCF_batch_release_common ``` It is also notable that with this kind of hacks, GCC is finally able to per= form TCO on the return statement.=