public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/104588] memset loses alignment infomation in some cases
Date: Fri, 18 Feb 2022 07:36:02 +0000	[thread overview]
Message-ID: <bug-104588-4-doZLIwybTy@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-104588-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104588

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org
          Component|middle-end                  |c++

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> Confirmed,
> Reduced testcase:
> struct alignas(16) foo
> {
>   unsigned char a[32];
> };
> 
> foo* construct(foo* p)
> {
>   __builtin_memset(p, 0, sizeof(foo));
>   return p;
> }
> 
> ----- CUT ----
> That is it has nothing to do with the constructors at all but just memset.

There's a duplicate about this - in GIMPLE we do not assume that pointers are
aligned naturally, only memory accesses behave this way and memset itself has
no alignment constraints.

The memset for the original testcase is matched by loop distribution:

  <bb 2> [local count: 32534376]:
  MEM[(struct bar *)p_1(D) clique 2 base 1] ={v} {CLOBBER};
  MEM[(struct foo *)p_1(D) clique 3 base 1] ={v} {CLOBBER};
  # PT = nonlocal null
  _3 = &MEM[(struct foo *)p_1(D)].a;
  # USE = anything
  # CLB = anything
  __builtin_memset (_3, 0, 32);
  MEM[(struct bar *)p_1(D) clique 2 base 1].m = 0;
  MEM[(struct bar *)p_1(D) clique 2 base 1].n = 0;
  MEM[(struct bar *)p_1(D) clique 2 base 1].p = 0;
  MEM[(struct bar *)p_1(D) clique 2 base 1].q = 0;
  return p_1(D);

Note the C++ frontend emits

{
  <<cleanup_point <<< Unknown tree: expr_stmt
    (void) *(unsigned char[32] *)     unsigned char * D.2382;
    <<< Unknown tree: expr_stmt
      (void) (D.2382 = (unsigned char *) &((struct foo *) this)->a) >>>;
        unsigned char * D.2383;
    <<< Unknown tree: expr_stmt
      (void) (D.2383 = D.2382) >>>;
    TARGET_EXPR <D.2384, 31>;
    goto <D.2386>;
    <D.2387>:;
    <<cleanup_point <<< Unknown tree: expr_stmt
      *D.2383 = 0;,  --D.2384; >>>>>;
    <<< Unknown tree: expr_stmt
      (void)  ++D.2383 >>>;
    <D.2386>:;
    if (D.2384 >= 0) goto <D.2387>; else goto <D.2385>;
    <D.2385>:;
    D.2382 >>>>>;
}

which is a loop iterating with a char * pointer and thus performing
char * accesses which does not include alignment info of the base
as if it would do this->a[i] and use an array index IV.  So it's
first of all a C++ frontend issue which fails to expose the
maximum possible information at the stores it creates (those we
could preserve).

  parent reply	other threads:[~2022-02-18  7:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-18  5:24 [Bug c++/104588] New: user-defined constructor loses alignment information about `*this` lh_mouse at 126 dot com
2022-02-18  5:38 ` [Bug middle-end/104588] memset loses alignment infomation in some cases pinskia at gcc dot gnu.org
2022-02-18  7:36 ` rguenth at gcc dot gnu.org [this message]
2022-02-18 12:26 ` [Bug c++/104588] " lh_mouse at 126 dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104588-4-doZLIwybTy@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).