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

            Bug ID: 57380
           Summary: GCC 4.9.0 will not vectorize std::max and similar
                    functions
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jewillco at osl dot iu.edu

It appears that having a function that returns a const reference to one of its
arguments causes vectorization of calls to that function to fail.  Here is a
simple test program:

struct my_array {
  int data[4] __attribute__((aligned(16)));
};

const int& my_max(const int& a, const int& b) {
  return a < b ? b : a;
}

int f(my_array a, my_array b) {
  int res = 0;
  for (int i = 0; i < 4; ++i) {
    res += my_max(a.data[i], b.data[i]);
  }
  return res;
}

The signature of my_max is a specialization of the one of std::max; std::max
itself has similar problems.  The loop will vectorize without trouble if my_max
returns "int".  The main errors from the vectorization report seem to be:

vec_min_max.cpp:11: note: not vectorized: not suitable for gather load _6 =
*iftmp.0_12;

vec_min_max.cpp:11: note: bad data references.

Other variants of the code get "control flow in loop" instead.  The flags I am
using are:

-ftree-vectorizer-verbose=4 -Ofast -march=nocona

but the code should be able to vectorize under SSE2.  The GCC version I am
using is "g++ (GCC) 4.9.0 20130519 (experimental)" on x86-64.  GCC 4.7.2 has a
similar error, while "4.4.7 20120313 (Red Hat 4.4.7-3)" can vectorize it
without problems.

Reply via email to