From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 890AF3858039; Wed, 30 Aug 2023 09:57:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 890AF3858039 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693389478; bh=P8L/tEUQLDqvPYr/PqlZFK5iorld2HrfBEKHOaY8GvM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=qbwTin93iKvzAS1MM2QzZoedKO9e0J0CUjEeYKArIqhmLpkJBpxz3HL8wZ4k5saVJ KEAqQ443LtJpHsZg0zM6hmu5+z/0K31VLoK90na02E7QrVZh3ws8w8K2E/vvxMt0nS N2g4/+V/9Ku45P4ijDITdqrKYRgcl16rKgvxl9yQ= From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/110914] [11/12/13/14 Regression] Optimization eliminating necessary assignment before 0-byte memcpy since r10-5451 Date: Wed, 30 Aug 2023 09:57:58 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 11.4.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: jakub at gcc dot gnu.org X-Bugzilla-Target-Milestone: 11.5 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=3D110914 --- Comment #8 from CVS Commits --- The releases/gcc-11 branch has been updated by Jakub Jelinek : https://gcc.gnu.org/g:cf3aa538317d6c525739f339b79010ae82dc20f5 commit r11-10969-gcf3aa538317d6c525739f339b79010ae82dc20f5 Author: Jakub Jelinek Date: Wed Aug 30 11:21:45 2023 +0200 tree-ssa-strlen: Fix up handling of conditionally zero memcpy [PR110914] The following testcase is miscompiled since r279392 aka r10-5451-gef29b12cfbb4979 The strlen pass has adjust_last_stmt function, which performs mainly st= rcat or strcat-like optimizations (say strcpy (x, "abcd"); strcat (x, p); or equivalent memcpy (x, "abcd", strlen ("abcd") + 1); char *q =3D strc= hr (x, 0); memcpy (x, p, strlen (p)); etc. where the first stmt stores '\0' charac= ter at the end but next immediately overwrites it and so the first memcpy c= an be adjusted to store 1 fewer bytes. handle_builtin_memcpy called this function in two spots, the first one guarded like: if (olddsi !=3D NULL && tree_fits_uhwi_p (len) && !integer_zerop (len)) adjust_last_stmt (olddsi, stmt, false); i.e. only for constant non-zero length. The other spot can call it even for non-constant length but in that case we punt before that if that le= ngth isn't length of some string + 1, so again non-zero. The r279392 change I assume wanted to add some warning stuff and change= d it like if (olddsi !=3D NULL - && tree_fits_uhwi_p (len) && !integer_zerop (len)) - adjust_last_stmt (olddsi, stmt, false); + { + maybe_warn_overflow (stmt, len, rvals, olddsi, false, true); + adjust_last_stmt (olddsi, stmt, false); + } While maybe_warn_overflow possibly handles non-constant length fine, adjust_last_stmt really relies on length to be non-zero, which !integer_zerop (len) alone doesn't guarantee. While we could for len being SSA_NAME ask the ranger or tree_expr_nonzero_p, I think adjust_last_stmt will not benefit from it much, so the following patch just restores the above condition/previous behavior for the adjust_last_stmt call only. 2023-08-30 Jakub Jelinek PR tree-optimization/110914 * tree-ssa-strlen.c (strlen_pass::handle_builtin_memcpy): Don't call adjust_last_stmt unless len is known constant. * gcc.c-torture/execute/pr110914.c: New test. (cherry picked from commit 398842e7038ea0f34054f0f694014d0ecd656846)=