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

            Bug ID: 68764
           Summary: C frontend does not fold away trivial expressions that
                    refer to const variables
           Product: gcc
           Version: 5.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ppalka at gcc dot gnu.org
  Target Milestone: ---

In the following snippet, one would expect foo and bar to get folded to the
same code, but that is not the case.

void dummy (const void *);

int
foo (void)
{
  const int x = 7;
  dummy (&x);
  return x + 0;
}

int
bar (void)
{
  const int x = 7;
  dummy (&x);
  return x;
}


While the C FE does fold "return x + 0;" to "return 7;", it does not fold
"return x;" to "return 7;". And the load of x is the function bar() eventually
survives all optimization passes and is present in the final optimized code:

bar ()
{
  const int x;
  int _4;

  <bb 2>:
  x = 7;
  dummy (&x);
  _4 = x;
  x ={v} {CLOBBER};
  return _4;

}

The C++ frontend is able to fully fold both return statements.

Reply via email to