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

            Bug ID: 113422
           Summary: Missed optimizations in the presence of pointer chains
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carnet at student dot ethz.ch
  Target Milestone: ---

https://godbolt.org/z/hdWKn4bjc

All three functions write to the same variable (b). Clang is able to optimize
this. GCC -O3 cannot always do this.
Assembly for foo is writing to **d instead to directly write 1 to b. There is
similar behavior for bar and baz. Clang optimizes the code to directly write to
b.
Baz is fully optimized only if the foo and bar are removed.

int b = 0;
static int *c = &b;
static int **d = &c;
static int ***e = &d;

void foo() {***e = 1;}
void bar() {**d = 1;}
void baz() {*c = 1;}

Assembly code:
foo:
        movq    d(%rip), %rax
        movq    (%rax), %rax
        movl    $1, (%rax)
        ret

Reply via email to