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

            Bug ID: 80740
           Summary: Aliasing with the return value
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Keywords: alias, missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: glisse at gcc dot gnu.org
  Target Milestone: ---

struct A {
  int i;
  A():i(0){}
  A(A const&a):i(a.i){}
};

A f(A&a){
  A ret;
  ret.i=a.i;
  return ret;
}

We do not manage to remove the 0 initialization because of a possible aliasing
between ret and a

  ret_3(D)->i = 0;
  _1 = a_4(D)->i;
  ret_3(D)->i = _1;
  return ret_3(D);

Clang does remove it.
Aliasing would be something like

int g(){
  A x=f(x);
  return x.i;
}

which we optimize to return 0; and clang optimizes it to just return; without a
warning, although their static analysis tool says
/tmp/x.cc:9:8: warning: Assigned value is garbage or undefined
  ret.i=a.i;
       ^~~~

Clang has the opposite bug report, saying that they are wrong to optimize
because g is valid, but they seem to consider that the bug is in the standard
for not forbidding it more clearly...
https://bugs.llvm.org/show_bug.cgi?id=11470

Reply via email to