c++: Handle __builtin_clear_padding on non-trivially-copyable types [PR102586]
authorJakub Jelinek <jakub@redhat.com>
Thu, 7 Apr 2022 07:14:07 +0000 (09:14 +0200)
committerJakub Jelinek <jakub@redhat.com>
Thu, 7 Apr 2022 07:14:07 +0000 (09:14 +0200)
commit46c739d40c6c1028295931c53cf414d581519072
tree70ecb58f0520543a4b071225b890f17fc21c9da6
parent973a2ce71f8dab559fbbfc34b59e39e047df74a6
c++: Handle __builtin_clear_padding on non-trivially-copyable types [PR102586]

On Fri, Feb 11, 2022 at 07:55:50PM +0100, Jakub Jelinek via Gcc-patches wrote:
> Something like the https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102586#c16
> will still be needed with adjusted testcase from
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102586#c15 such that
> __builtin_clear_padding is called directly on var addresses rather than
> in separate functions.

Here is an updated version of the
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102586#c15
patch which uses FIELD_DECL in the langhook instead of its TREE_TYPE,
and the testcases have been adjusted for the builtin accepting
pointers to non-trivially-copyable types only if it is address of a
declaration.

2022-04-07  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/102586
gcc/
* langhooks.h (struct lang_hooks_for_types): Add classtype_as_base
langhook.
* langhooks-def.h (LANG_HOOKS_CLASSTYPE_AS_BASE): Define.
(LANG_HOOKS_FOR_TYPES_INITIALIZER): Add it.
* gimple-fold.cc (clear_padding_type): Use ftype instead of
TREE_TYPE (field) some more.  For artificial FIELD_DECLs without
name try the lang_hooks.types.classtype_as_base langhook and
if it returns non-NULL, use that instead of ftype for recursive call.
gcc/cp/
* cp-objcp-common.h (cp_classtype_as_base): Declare.
(LANG_HOOKS_CLASSTYPE_AS_BASE): Redefine.
* cp-objcp-common.cc (cp_classtype_as_base): New function.
gcc/testsuite/
* g++.dg/torture/builtin-clear-padding-5.C: New test.
* g++.dg/cpp2a/builtin-clear-padding1.C (bar): Uncomment one
call that is now accepted.
gcc/cp/cp-objcp-common.cc
gcc/cp/cp-objcp-common.h
gcc/gimple-fold.cc
gcc/langhooks-def.h
gcc/langhooks.h
gcc/testsuite/g++.dg/cpp2a/builtin-clear-padding1.C
gcc/testsuite/g++.dg/torture/builtin-clear-padding-5.C [new file with mode: 0644]