From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 981303857413; Mon, 31 Oct 2022 02:42:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 981303857413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667184167; bh=7QlW/pmuKMPMQQx/Z55RSnFBwEERJ3+1EaXlKGRzMkw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=jJ+e87xHUUO8rZaHcBxkr5Q/41ryyHHUc5BpFAKYL6VFiBD+sIn9IvLTi3SjUF8Q8 8ggVgk/F1no3rb5iaSIaJJaLjxo7qXt2D5jcmp00K/An3t3SEq82O7wn/e+BhUAlMK TJ0CAyM9pTx9Rrg8CWdh9EC3EhtSrRQP7a8xEAYs= From: "crazylht at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/102566] [i386] GCC should emit LOCK BTS for simple bit-test-and-set operations with std::atomic Date: Mon, 31 Oct 2022 02:42:45 +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: 12.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: crazylht at gmail 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102566 --- Comment #32 from Hongtao.liu --- (In reply to Marko M=C3=A4kel=C3=A4 from comment #31) > Much of this seems to work in GCC 12.2.0 as well as in clang++-15. For cl= ang > there is a related ticket https://github.com/llvm/llvm-project/issues/373= 22 >=20 > I noticed a missed optimization in both g++-12 and clang++-15: Some > operations involving bit 31 degrade to loops around lock cmpxchg. I compi= led 31 is sign bit, and c =3D a & 1U << 31; c =3D=3D 0 is optimized to (sign i= nt)a >=3D 0. The optimization we did in optimize_atomic_bit_test_and is supposed to matc= h a & 1U << 31, and it failed. I guess it could be extend to match (sign int)a = >=3D 0 when mask is 1U << 31. 7 : 8 : 9 _1 =3D __atomic_fetch_or_4 (v, 2147483648, 0); 10 _2 =3D (signed int) _1; 11 if (_2 >=3D 0) goto ; else goto ; 12 : 13 return; 14} 15=