public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction
@ 2020-09-07 23:40 msebor at gcc dot gnu.org
  2020-09-07 23:46 ` [Bug tree-optimization/96966] [8/9/10/11 Regression] " msebor at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-09-07 23:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96966
           Summary: redundant memcpy not eliminated after pointer
                    subtraction
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The second call to memcpy and mempcpy, respectively, is redundant in each of
the functions below and can be eliminated.  GCC doesn't notice the redundancy
and emits both pairs of copies.  Clang emits just one call to memcpy but it too
fails to eliminate the redundant call to mempcpy.

$ cat z.c && gcc -O2 -S -o /dev/stdout z.c
extern char a[32];

void f (const void *s)
{
  char *p = (char*)__builtin_memcpy (a, s, 16) + 16;
  __builtin_memcpy (p - 16, s, 16);
}


void g (const void *s)
{
  char *p = (char*)__builtin_mempcpy (a, s, 16);
  __builtin_mempcpy (p - 16, s, 16);
}
        .file   "z.c"
        .text
        .p2align 4
        .globl  f
        .type   f, @function
f:
.LFB0:
        .cfi_startproc
        movdqu  (%rdi), %xmm0
        movups  %xmm0, a(%rip)
        movdqu  (%rdi), %xmm1
        movups  %xmm1, a(%rip)
        ret
        .cfi_endproc
.LFE0:
        .size   f, .-f
        .p2align 4
        .globl  g
        .type   g, @function
g:
.LFB3:
        .cfi_startproc
        movdqu  (%rdi), %xmm0
        movups  %xmm0, a(%rip)
        movdqu  (%rdi), %xmm1
        movups  %xmm1, a(%rip)
        ret
        .cfi_endproc
.LFE3:
        .size   g, .-g
        .ident  "GCC: (GNU) 11.0.0 20200902 (experimental)"
        .section        .note.GNU-stack,"",@progbits

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [8/9/10/11 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
@ 2020-09-07 23:46 ` msebor at gcc dot gnu.org
  2020-09-08  7:00 ` rguenth at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-09-07 23:46 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|redundant memcpy not        |[8/9/10/11 Regression]
                   |eliminated after pointer    |redundant memcpy not
                   |subtraction                 |eliminated after pointer
                   |                            |subtraction
      Known to work|                            |8.1.0, 8.2.0
      Known to fail|                            |10.2.0, 11.0, 7.3.0, 8.3.0,
                   |                            |9.2.0
           Keywords|                            |missed-optimization

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
According to Godbolt, GCC 8.1 and 8.2 emit optimal code for both functions but
GCC 8.3 emits the less optimal code for f and has g jump to it.  Starting with
10.1, GCC emits the same suboptimal code for both functions.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [8/9/10/11 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
  2020-09-07 23:46 ` [Bug tree-optimization/96966] [8/9/10/11 Regression] " msebor at gcc dot gnu.org
@ 2020-09-08  7:00 ` rguenth at gcc dot gnu.org
  2021-01-14  9:20 ` rguenth at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-09-08  7:00 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-09-08
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Keywords|                            |alias
   Target Milestone|---                         |8.5

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #1)
> According to Godbolt, GCC 8.1 and 8.2 emit optimal code for both functions
> but GCC 8.3 emits the less optimal code for f and has g jump to it. 
> Starting with 10.1, GCC emits the same suboptimal code for both functions.

This is likely "caused" by 08dfb1d682a707f7319aafec28edda424395dae5, aka
the fix for PR91108 which was also backported.  In the IL

  <bb 2> :
  _5 = MEM <__int128 unsigned> [(char * {ref-all})s_4(D)];
  MEM <__int128 unsigned> [(char * {ref-all})&a] = _5;
  _8 = MEM <__int128 unsigned> [(char * {ref-all})s_4(D)];
  MEM <__int128 unsigned> [(char * {ref-all})&a] = _8;
  return;

we lost the information that MEM <__int128 unsigned> [(char * {ref-all})s_4(D)]
and MEM <__int128 unsigned> [(char * {ref-all})&a] do not partially overlap.
The memcpy call guaranteed that.  The way the aliasing code rules out
partial overlap is by using alignment which doesn't help us here.

That it worked in GCC 8.[12] was due to bad code in VN that ignored
the possibility of a partial overlap here.

We eventually could lower memcpy (a, s, 16) to load + store with noting
they are independent using MR_DEPENDENCE_CLIQUE/BASE but this may cause
depleting of the clique resource on artificial testcases quickly
(we only have 16bits for clique).  Shifting bit allocation between
clique and base might be a possibility there, but at least clique
overflow mitigation would need to be put in place.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [8/9/10/11 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
  2020-09-07 23:46 ` [Bug tree-optimization/96966] [8/9/10/11 Regression] " msebor at gcc dot gnu.org
  2020-09-08  7:00 ` rguenth at gcc dot gnu.org
@ 2021-01-14  9:20 ` rguenth at gcc dot gnu.org
  2021-02-12  0:40 ` msebor at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-01-14  9:20 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [8/9/10/11 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-01-14  9:20 ` rguenth at gcc dot gnu.org
@ 2021-02-12  0:40 ` msebor at gcc dot gnu.org
  2021-02-18 10:06 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-02-12  0:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
No change in GCC 11.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [8/9/10/11 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2021-02-12  0:40 ` msebor at gcc dot gnu.org
@ 2021-02-18 10:06 ` jakub at gcc dot gnu.org
  2021-02-19  9:24 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-02-18 10:06 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Even
extern char a[32];

void f (const void *s)
{
  char *p = (char*)__builtin_memcpy (a, s, 16);
  __builtin_memcpy (p, s, 16);
}


void g (const void *s)
{
  __builtin_memcpy (a, s, 16);
  __builtin_memcpy (a, s, 16);
}
used to be optimized just in 8.1/8.2 and not in earlier or later GCC versions.
Perhaps delaying the lowering of memcpy a tiny bit and trying to optimize it
when it is still not lowered?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [8/9/10/11 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2021-02-18 10:06 ` jakub at gcc dot gnu.org
@ 2021-02-19  9:24 ` rguenth at gcc dot gnu.org
  2021-05-14  9:53 ` [Bug tree-optimization/96966] [9/10/11/12 " jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-02-19  9:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #4)
> Even
> extern char a[32];
> 
> void f (const void *s)
> {
>   char *p = (char*)__builtin_memcpy (a, s, 16);
>   __builtin_memcpy (p, s, 16);
> }
> 
> 
> void g (const void *s)
> {
>   __builtin_memcpy (a, s, 16);
>   __builtin_memcpy (a, s, 16);
> }
> used to be optimized just in 8.1/8.2 and not in earlier or later GCC
> versions.
> Perhaps delaying the lowering of memcpy a tiny bit and trying to optimize it
> when it is still not lowered?

lowering early is quite an important thing since it enables better initial
into-SSA rewriting and early inline costing.  Note even w/o lowering FRE
would not optimize this.  Even the strlen pass doesn't:

extern char a[32];
void __GIMPLE (ssa,startwith("fre1")) g (const void *s)
 {
__BB(2):
   __builtin_memcpy (&a[0], s_1(D), _Literal (__SIZE_TYPE__) 16);
   __builtin_memcpy (&a[0], s_1(D), _Literal (__SIZE_TYPE__) 16);
   return;
 }

has this survive until .fab if you do -O2 -fno-tree-forwprop -fno-tree-vrp

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [9/10/11/12 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2021-02-19  9:24 ` rguenth at gcc dot gnu.org
@ 2021-05-14  9:53 ` jakub at gcc dot gnu.org
  2021-06-01  8:18 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-05-14  9:53 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|8.5                         |9.4

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 8 branch is being closed.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [9/10/11/12 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2021-05-14  9:53 ` [Bug tree-optimization/96966] [9/10/11/12 " jakub at gcc dot gnu.org
@ 2021-06-01  8:18 ` rguenth at gcc dot gnu.org
  2022-05-27  9:43 ` [Bug tree-optimization/96966] [10/11/12/13 " rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-06-01  8:18 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|9.4                         |9.5

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 9.4 is being released, retargeting bugs to GCC 9.5.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [10/11/12/13 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2021-06-01  8:18 ` rguenth at gcc dot gnu.org
@ 2022-05-27  9:43 ` rguenth at gcc dot gnu.org
  2022-06-28 10:41 ` jakub at gcc dot gnu.org
  2023-07-07 10:38 ` [Bug tree-optimization/96966] [11/12/13/14 " rguenth at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-27  9:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|9.5                         |10.4

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 9 branch is being closed

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [10/11/12/13 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2022-05-27  9:43 ` [Bug tree-optimization/96966] [10/11/12/13 " rguenth at gcc dot gnu.org
@ 2022-06-28 10:41 ` jakub at gcc dot gnu.org
  2023-07-07 10:38 ` [Bug tree-optimization/96966] [11/12/13/14 " rguenth at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-28 10:41 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.4                        |10.5

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 10.4 is being released, retargeting bugs to GCC 10.5.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/96966] [11/12/13/14 Regression] redundant memcpy not eliminated after pointer subtraction
  2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2022-06-28 10:41 ` jakub at gcc dot gnu.org
@ 2023-07-07 10:38 ` rguenth at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-07 10:38 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.5                        |11.5

--- Comment #10 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 10 branch is being closed.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-07-07 10:38 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-07 23:40 [Bug tree-optimization/96966] New: redundant memcpy not eliminated after pointer subtraction msebor at gcc dot gnu.org
2020-09-07 23:46 ` [Bug tree-optimization/96966] [8/9/10/11 Regression] " msebor at gcc dot gnu.org
2020-09-08  7:00 ` rguenth at gcc dot gnu.org
2021-01-14  9:20 ` rguenth at gcc dot gnu.org
2021-02-12  0:40 ` msebor at gcc dot gnu.org
2021-02-18 10:06 ` jakub at gcc dot gnu.org
2021-02-19  9:24 ` rguenth at gcc dot gnu.org
2021-05-14  9:53 ` [Bug tree-optimization/96966] [9/10/11/12 " jakub at gcc dot gnu.org
2021-06-01  8:18 ` rguenth at gcc dot gnu.org
2022-05-27  9:43 ` [Bug tree-optimization/96966] [10/11/12/13 " rguenth at gcc dot gnu.org
2022-06-28 10:41 ` jakub at gcc dot gnu.org
2023-07-07 10:38 ` [Bug tree-optimization/96966] [11/12/13/14 " rguenth at gcc dot gnu.org

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).