Vectorizable Data References

For any data reference, either as an array element or pointer reference, take care to ensure that there are no potential dependence or alias constraints preventing vectorization; intuitively, an expression in one iteration must not depend on the value computed in a previous iteration and pointer variables must provably point to distinct locations. Use of the ivdep pragma and the restrict keyword can be used to tell the compiler to ignore assumed dependences. See also the examples in the Data Alignment section.

Arrays

Vectorizable data in a loop may be expressed as uses of array elements, provided that the array references are not non-unit stride or loop invariant. Non-unit stride references are not vectorized by default; the vector pragma can be used to override this. The compiler uses an efficiency heuristic that decides whether the vectorization of non-unit strides is profitable (checks number of units vs. non-units).

Pointers

Vectorizable data can also be expressed using pointers, subject to the same constraints as uses of array elements: You cannot vectorize references that are non-unit stride or loop invariant.

Invariants

Vectorizable data can also include loop invariant references on the right hand inside an expression, either as variables or numeric constants. The loop in the "Vectorizable Loop Invariant Reference" example will vectorize:

Vectorizable Loop Invariant Reference

If vectorizable data is provably aligned, the compiler will generate aligned instructions. This is the case for locally declared data and data declared using the alignment declspec. Where data alignment is not known, unaligned references will be used unless a pragma or command-line switch is used to override this as described in Alignment with declspec.

Common Errors in Making Code Vectorization-Compatible

To make your code vectorizable, you will often need to make some changes to your loops. However, you should make only the changes needed to enable vectorization and no others. In particular, you should avoid these common changes: