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

            Bug ID: 104574
           Summary: GCC misses basic optimization for restricted pointers
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m...@nieper-wisskirchen.de
  Target Milestone: ---

The following TU

void g (void);

int 
f (int *restrict p)
{
  p[0] = 7;
  g ();
  return p[0];
}

is compiled by GCC at -O3 to

f:
        pushq   %rbx
        movq    %rdi, %rbx
        movl    $7, (%rdi)
        call    g
        movl    (%rbx), %eax
        popq    %rbx
        ret

Obviously, GCC seems to think that g may modify the data reachable through p.
However, if g did that it would cause undefined behavior anyway.

So GCC misses a simple optimization opportunity here; it doesn't have to reload
the memory contents after the call to g; in fact, the function will always
return 7.

For comparison, Clang compiles the TU to

f:
        pushq   %rax
        movl    $7, (%rdi)
        callq   g
        movl    $7, %eax
        popq    %rcx
        retq

Reply via email to