From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C77EB3857C40; Thu, 23 Nov 2023 13:54:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C77EB3857C40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1700747682; bh=ZqHGFb0gkk8TSgfe5fy38CoFYwW3yFJQW7EZ5pzlrVo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=mWy+Ixy3dB8aPvMXWRz6t8/C/61jMVFwdRVY8T3sfmivmujlh/o6ulT2HKcdM+SiZ V6FzYkZxXeNn/FjVThfqbcJ9qldULWM1CqqdUcJOJxtQv+/vJXhejVs5vXFs3VFMB1 2nDDbKea6VZW3iHCx/KQxkQEaBQX8WkXN039DruU= From: "bugdal at aerifal dot cx" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/32667] block copy with exact overlap is expanded as memcpy Date: Thu, 23 Nov 2023 13:54:40 +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: 4.2.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: bugdal at aerifal dot cx 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=3D32667 --- Comment #36 from Rich Felker --- > the assembly generated by the current implementations already supports th= at case. Our memcpy is not written in asm but in C, and it has the restrict qualifie= r on src and dest. This entitles a compiler to emit asm equivalent to if (src=3D= =3Ddest) system("rm -rf /") if it likes. I don't know how you can write a valid C implementation of memcpy that "doesn't care" about 100% overlap without giv= ing up restrict (and the benefits it entails) entirely. If you're happy with a branch, you could probably take restrict off the arguments and do something like: if (src=3D=3Ddest) return; const char *restrict src2 =3D src; char *restrict dest2 =3D dest; ... but that's shoving the branch into memcpy where it's a cost on every caller making dynamic memcpys with potentially tiny size (like qsort, etc.) and obeying the contract not to call with overlapping src/dest, rather than just imposing it on bad callers.=