From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id ED1DE385701A; Thu, 30 Nov 2023 19:31:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ED1DE385701A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1701372710; bh=4ZsQAjzA6PEjptnlCNU0hyAZklTVEhx26Y/xLN7d6/0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=xrdEdfPTckjn86jaYvn/OmuU/nLPVG2kmIzXNcYNZlJgdnc0JDqexqhYTGNyLvMhn Mq6pYz/OFXN3c1LdE2wBgusg1eejRc093pm1NX3yK+Du7bzMxmOF1xlhkzVOR1ABA5 t0Fe1DhGUtfJtNUTdEU6DvRR7DavPrHgR3k+OVLM= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug sanitizer/112748] memmove(ptr, ptr, n) call optimized out even at -O0 with -fsanitize=undefined Date: Thu, 30 Nov 2023 19:31:50 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: sanitizer X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: documentation X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub 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: 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=3D112748 --- Comment #4 from Jakub Jelinek --- So, do we want something like --- gcc/gimple-fold.cc.jj 2023-11-02 12:15:12.205998817 +0100 +++ gcc/gimple-fold.cc 2023-11-30 20:24:01.092095623 +0100 @@ -5083,12 +5083,16 @@ gimple_fold_builtin (gimple_stmt_iterato return gimple_fold_builtin_bzero (gsi); case BUILT_IN_MEMSET: + if (!optimize) + return false; return gimple_fold_builtin_memset (gsi, gimple_call_arg (stmt, 1), gimple_call_arg (stmt, 2)); case BUILT_IN_MEMCPY: case BUILT_IN_MEMPCPY: case BUILT_IN_MEMMOVE: + if (!optimize) + return false; return gimple_fold_builtin_memory_op (gsi, gimple_call_arg (stmt, 0), gimple_call_arg (stmt, 1), fcod= e); case BUILT_IN_SPRINTF_CHK: (and repeat for many other builtins)? I'm afraid we can't do if (!optimize) return false; for all builtins in gimple_fold_builtin, because some builtins by design must be always folded = and never expand. E.g. __builtin_clear_padding, __builtin_{clz,ctz,clrsb,ffs,popcount,parity}g, __builtin_{add,sub,mul}_overflow{,_p} and many others. expand_builtin has /* When not optimizing, generate calls to library functions for a certain set of builtins. */ if (!optimize && !called_as_built_in (fndecl) && fcode !=3D BUILT_IN_FORK && fcode !=3D BUILT_IN_EXECL && fcode !=3D BUILT_IN_EXECV && fcode !=3D BUILT_IN_EXECLP && fcode !=3D BUILT_IN_EXECLE && fcode !=3D BUILT_IN_EXECVP && fcode !=3D BUILT_IN_EXECVE && fcode !=3D BUILT_IN_CLEAR_CACHE && !ALLOCA_FUNCTION_CODE_P (fcode) && fcode !=3D BUILT_IN_FREE && (fcode !=3D BUILT_IN_MEMSET || !(flag_inline_stringops & ILSOP_MEMSET)) && (fcode !=3D BUILT_IN_MEMCPY || !(flag_inline_stringops & ILSOP_MEMCPY)) && (fcode !=3D BUILT_IN_MEMMOVE || !(flag_inline_stringops & ILSOP_MEMMOVE)) && (fcode !=3D BUILT_IN_MEMCMP || !(flag_inline_stringops & ILSOP_MEMCMP))) return expand_call (exp, target, ignore); Perhaps just a general if (!optimize && !called_as_built_in (fndecl)) return false; at the start of gimple_fold_builtin? Or do we want to let some exceptions? Do we also apply GIMPLE match.pd simplification at -O0?=