In the following test1() code the SUCCESS branch is never reached when I
optimize the code with -O2. If I add -O2 -fno-tree-vrp or just use -O0, the
SUCCESS branch is executed as expected.

int test1()
{
  int vint = 1, *v, i;
  QVector<int *> vect;

  vect.push_back(&vint);
  vect.push_back(&vint);
  vect.push_back(&vint);
  vect.push_back(&vint);

  for(i = 0; i < vect.size(); i++) {
    v = vect.at(i);

    if (i >= 3 && i <= 4) {
      printf("%s SUCCESS\n", __FUNCTION__);
      return 0;
    }

    printf("v: %p\n", v);
  }

  fprintf(stderr, "%s FAIL!\n", __FUNCTION__);
  return 0;
}

This test2() is a modified test case where the if - statement gets its
comparison parameter via an int variable. This works with -O2.

int test2()
{
  int vint = 1, *v, i, low = 3;
  QVector<int *> vect;

  vect.push_back(&vint);
  vect.push_back(&vint);
  vect.push_back(&vint);
  vect.push_back(&vint);

  for(i = 0; i < vect.size(); i++) {
    v = vect.at(i);

    if (i >= low && i <= 4) {
      printf("%s SUCCESS\n", __FUNCTION__);
      return 0;
    }

    printf("v: %p\n", v);
  }

  printf("%s FAIL!\n", __FUNCTION__);
  return 0;
}

Attached is the preprocessed code qvector.ii and the output of the command line
used to build it. If you compile qvector.ii with -O0 or -O2 you'll see the
results.


-- 
           Summary: c++ -O2 optimizes away a branch inside a loop (-ftree-
                    vrp maybe)
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: juha dot kallioinen at nokia dot com


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

Reply via email to