This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug tree-optimization/30590] New: tree-nrv optimization clobbers return variable


The following test case, when compiled with g++ -O, has a return
value of 1 (instead of the correct value of 0):

struct test
{
  int type;
  char buffer[4242]; /* should trigger pass-by-reference */
};

int flag = 0;

struct test
reset (void)
{
  struct test retval;
  retval.type = 1;
  return retval;
}

struct test
test (void)
{
  struct test result;
  result.type = 0;

  for (int i = 0; i < 2; ++i)
    {
      struct test candidate = reset ();
      if (flag)
        result = candidate;
    }

  return result;
}

int
main (void)
{
  struct test result = test ();
  return result.type;
}


The reason for this appears to be a bug in the tree-nrv (named
return value) optimization pass.   Before tree-nrv, the function
test looks like:

  struct test candidate;
  int i;
  int flag.0;

<bb 2>:
  <retval>.type = 0;
  flag.0 = flag;
  i = 0;

<L0>:;
  candidate = reset () [return slot optimization];
  if (flag.0 != 0) goto <L1>; else goto <L2>;

<L1>:;
  <retval> = candidate;

<L2>:;
  i = i + 1;
  if (i != 2) goto <L0>; else goto <L5>;

<L5>:;
  return <retval>;


After tree-nrv, we have:

  struct test candidate;
  int i;
  int flag.0;

<bb 2>:
  <retval>.type = 0;
  flag.0 = flag;
  i = 0;

<L0>:;
  <retval> = reset () [return slot optimization];
  if (flag.0 != 0) goto <L1>; else goto <L2>;

<L1>:;

<L2>:;
  i = i + 1;
  if (i != 2) goto <L0>; else goto <L5>;

<L5>:;
  return <retval>;


The return value of reset has been redirected directly
into the return value slot of test, instead of the local
variable candidate.   tree-nrv.c has some code that is
apparently intended to prevent this type of thing; I'm
not sure why that didn't work here.

The bug occurs (at least) in GCC 4.1.1 and current mainline.


-- 
           Summary: tree-nrv optimization clobbers return variable
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: uweigand at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30590


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]