From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id CD46E3858C53; Sat, 27 May 2023 20:26:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD46E3858C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685219189; bh=zu7qvv4d/k6I44oW3usQdl/0j4MIYqtAG9WKl/drlnw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=BiAqGAS3NnwAnxTNTtG24Ymqke06MujSP1IJXv/aGZWLQZCp2mWMi2xOac94xyuJw ExGGM39ILDP7UeQsbXxc49QBXNg3j65Pz2pl6taN+eRvRwrgYs+oRmLSrBN0mfzPh9 p/8nLkNSdXURWEVh14Kz3/wx6dysBWv0rAet/+jI= From: "richard.yao at alumni dot stonybrook.edu" To: gcc-bugs@gcc.gnu.org Subject: =?UTF-8?B?W0J1ZyBjLzExMDAwN10gSW1wbGVtZW50IHN1cHBvcnQgZm9yIENs?= =?UTF-8?B?YW5n4oCZcyBfX2J1aWx0aW5fdW5wcmVkaWN0YWJsZSgp?= Date: Sat, 27 May 2023 20:26:29 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: richard.yao at alumni dot stonybrook.edu 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: 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=3D110007 --- Comment #5 from Richard Yao = --- (In reply to Andrew Pinski from comment #4) > (In reply to Richard Yao from comment #3) > > (In reply to Andrew Pinski from comment #2) > > > (In reply to Richard Yao from comment #0) > > > > Having the ability to specify __builtin_unpredictable() as a hint to > > > > encourage the compiler to use cmov would be useful for implementing > > > > algorithms like binary search that have unpredictable branches. > > > > __builtin_expect_with_probability() looks like a possible substitut= e, but > > > > Clang does not support it and it does not always work as described = in > > > > #110001. > > >=20 > > > PR 110001 has nothing to do with __builtin_expect_with_probability and > >=20 > > I mentioned it briefly in PR 110001, but I guess I should make it expli= cit. > > See line 58 here: > >=20 > > https://gcc.godbolt.org/z/ef3Yfchzv > >=20 > > That is made into a jump, when all that is necessary is cmov, which is = what > > Clang generates. In the example I had posted in PR 110001, I had not us= ed > > __builtin_expect_with_probability() because it made no difference, but = here > > I am using it to show that cmov is not used. >=20 >=20 >=20 > If we look at the -fdump-tree-optimized-lineno dump we see why it was not > turned into a cmov: > [/app/example.c:58:12 discrim 2] if (a_111 <=3D key_128(D)) > goto ; [50.00%] > else > goto ; [50.00%] >=20 > [local count: 447392427]: > [/app/example.c:75:40] _268 =3D (long unsigned int) i_82; > [/app/example.c:75:40] _271 =3D _268 * 4; > [/app/example.c:75:34] _274 =3D array_89(D) + _271; > [/app/example.c:5:6] pretmp_277 =3D *_274; > goto ; [100.00%] >=20 >=20 > There is a load moved inside the conditional. Is executing an unpredictable branch cheaper than executing a redundant loa= d? This a patch against the assembly output from GCC 12.3 modifies this to use cmov: diff --git a/out.s b/out.s index d796087..f0f009c 100644 --- a/out.s +++ b/out.s @@ -317,15 +317,11 @@ custom_binary_search_fast: cmovle %ecx, %edx .L43: leal 1(%rdx), %ecx - movq %rcx, %rax - movl (%rdi,%rcx,4), %ecx - cmpl %esi, %ecx - jle .L15 + cmpl %esi, (%rdi,%rcx,4) + cmovle %ecx, %edx .L25: movl %edx, %eax movl (%rdi,%rax,4), %ecx - movl %edx, %eax -.L15: cmpl %ecx, %esi setl %cl setg %dl Micro-benchmarking the two suggests that the answer is yes on Zen 3, althou= gh I do not understand why.=