From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 411C33858401; Sat, 27 May 2023 03:20:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 411C33858401 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685157635; bh=U6+2C1ToL0vDPhxm3zIaXKTrFeexy3Xk7FxIZxM76+M=; h=From:To:Subject:Date:From; b=scukXoZ0RhVvowwPq7xVwnBqoC6mLplhU9WedrEeEp7mJwgNWdiJvzlCr7JDsTOmE cSO1XnMlMQPs8ykf2UrZuW+CqHOMDToax1OzZPk7UV2k0jhFwZGWnZNdIPgMr/OQy9 SmBwEdAlEgaqagmsmcBuZdytZvyOWG1x0cJ1drug= From: "richard.yao at alumni dot stonybrook.edu" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/110001] New: [13 regression] Suboptimal code generation for branchless binary search Date: Sat, 27 May 2023 03:20:34 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 13.1.0 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: 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=3D110001 Bug ID: 110001 Summary: [13 regression] Suboptimal code generation for branchless binary search Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: richard.yao at alumni dot stonybrook.edu Target Milestone: --- GCC 12.3 generated beautiful code for this, with all but the last of the unrolled loop iterations using only 3 instructions: https://gcc.godbolt.org/z/eGbEj9YKd Currently, GCC generates 4 instructions: https://gcc.godbolt.org/z/Ebczq8jjx This probably does not make a huge difference given the data hazard, but th= ere is something awe-inspiring from seeing GCC generate only 3 instructions per unrolled loop iteration for binary search. It would be nice if future versi= ons went back to generating three instructions. This function was inspired by this D code: https://godbolt.org/z/5En7xajzc The bsearch1000() function is entirely branchless and has no more than 2 instructions for every cmov, excluding ret. I wrote a more general version = in C that can handle variable array sizes, and to my pleasant surprise, GCC 12.3 generated a similar 3 instruction sequence for all but the last of the unro= lled loop iterations. I was saddened when I saw the output from GCC 13.1 and tru= nk. Anyway, all recent versions of GCC that I cared to check generate a branch = for the last unrolled iteration on line 58. That branch is unpredictable, so GCC would generate better code here if it used predication to avoid the branch.= I had been able to give GCC a hint to avoid a similar branch at the end using __builtin_expect_with_probability(), but that trick did not work for line 5= 8. Also, if anyone is interested in where that D code originated, here is the source: https://muscar.eu/shar-binary-search-meta.html=