From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DF6F03858C3A; Thu, 28 Sep 2023 11:35:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DF6F03858C3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1695900949; bh=wlEzQSMJJ7lYatsP9rap0NSOzLND5Kf0FGms5M4ioE4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Y1mvzTWz5hGsmpVOXG67lR2yWrS5fg2XQUHyFKz84/v1v0jnAtqtk4iTnnIoYGU08 XnjU8iB5q3x+ttiGJvr80yhHNves29CWUisD2F5fMc73y/cfG2N4COj2C6cTfLbaTN GoMUWtojVPB61S7TMz+obiX1uJ/SY8wQ9r+rAiu4= From: "wilco at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/104611] memcmp/strcmp/strncmp can be optimized when the result is tested for [in]equality with 0 on aarch64 Date: Thu, 28 Sep 2023 11:35:48 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: wilco at gcc dot gnu.org 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: everconfirmed bug_status cf_reconfirmed_on 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=3D104611 Wilco changed: What |Removed |Added ---------------------------------------------------------------------------- Ever confirmed|0 |1 Status|UNCONFIRMED |NEW Last reconfirmed| |2023-09-28 --- Comment #5 from Wilco --- (In reply to Mathias Stearn from comment #4) > clang has already been using the optimized memcmp code since v16, even at > -O1: https://www.godbolt.org/z/qEd768TKr. Older versions (at least since = v9) > were still branch-free, but via a less optimal sequence of instructions. >=20 > GCC's code gets even more ridiculous at 32 bytes, because it does a branch > after every 8-byte compare, while the clang code is fully branch-free (not > that branch-free is always better, but it seems clearly so in this case). >=20 > Judging by the codegen, there seems to be three deficiencies in GCC: 1) an > inability to take advantage of the load-pair instructions to load 16-bytes > at a time, and 2) an inability to use ccmp to combine comparisons. 3) usi= ng > branching rather than cset to fill the output register. Ideally these cou= ld > all be done in the general case by the low level instruction optimizer, b= ut > even getting them special cased for memcmp (and friends) would be an > improvement. I think 1, 2 and 3 are all related due to not having a TImode compare patte= rn, so GCC splits things into 8-byte chunks using branches. We could add that a= nd see whether the result is better or add a backend expander for memcmp simil= ar to memset and memcpy. Note what LLVM does is terrible, a 64-byte memcmp is ridiculously inefficie= nt due to long dependency chains, loading and comparing every byte even if the= re is a mismatch in byte 0. So it's obviously better to use branches.=