Why isn't the Visual C++ auto-vectorizer vectorizing this simple loop?
c, c++, loops, vectorization, visual-c++
Solution
I finally found how to fix it... seems like the multiplication in `n * j` is the culprit. Hoisting it outside as `int nj = n * j;` and using `nj` in the inner loop instead fixes the problem.
I still don't know why this happens though.
If anyone knows, please post it!
Problem
I can't figure out why Visual C++ can't auto-vectorize this loop... any ideas? I get: ``` testvec.cpp:12: info C5002: loop not vectorized due to reason '1200' ``` where reason code 1200 is: Loop contains loop-carried data dependences that prevent vectorization. Different iterations of the loop interfere with each other such that vectorizing the loop would produce wrong answers, and the auto-vectorizer cannot prove to itself that there are no such data dependences. But why? ``` #include <stdlib.h> int main(int argc, char *argv[]) { int const n = argc; double *const p1 = (double *)malloc(n * n * sizeof(*p1)), *const p2 = (double *)malloc(n * n * sizeof(*p2)); for (int j = 0; j < n; ++j) { double const sj = p1[n * j]; for (int i = 0; i < n; ++i) { double const sum = p1[i] + sj, old = p1[i + n * j]; p2[i + n * j] = sum < old ? sum : old; } } } ```