From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9C8D73858428; Sat, 2 Oct 2021 16:08:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9C8D73858428 From: "thiago at kde dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/102566] New: [i386] GCC should emit LOCK BTS for simple bit-test-and-set operations with std::atomic Date: Sat, 02 Oct 2021 16:08:46 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: thiago at kde dot org 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 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: Sat, 02 Oct 2021 16:08:46 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102566 Bug ID: 102566 Summary: [i386] GCC should emit LOCK BTS for simple bit-test-and-set operations with std::atomic Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: thiago at kde dot org Target Milestone: --- Simple test: $ cat test.cpp #include bool tbit(std::atomic &i) { return i.fetch_or(1, std::memory_order_relaxed) & 1; } The sequence x.fetch_or(singlebit_constant) & singlebit_constant can be implemented by a LOCK BTS sequence. The above should emit: lock bts $1, (%rdi) setb %al ret But instead it emits a cmpxchg loop - see https://gcc.godbolt.org/z/99enKaf= fa. This was found reviewing MariaDB lightweight-mutex code, which uses the sign bit to indicate a contended mutex. See this commit[1] by one of their maintainers for the removal of fetch_or because it emits an extra loop. Bonus: LOCK BTR can be used in the sequence x.fetch_and(~single_bit_constan= t) & single_bit_constant [1] https://github.com/dr-m/atomic_sync/commit/d5e22b2d42cdbac7a15d242bf1446377= 555c4041=