Vectorization and Loops
Types of Vectorized Loops
- addition (+)
- subtraction (-)
- multiplication (*)
- division (/)
- Countable:The loop trip count must be known at entry to the loop at runtime, though it need not be known at compile time (that is, the trip count can be a variable but the variable must remain constant for the duration of the loop). This implies that exit from the loop must not be data-dependent.
- Single entry and single exit:as is implied by stating that the loop must be countable.
- Contain straight-line code:SIMD instruction perform the same operation on data elements from multiple iterations of the original loop, therefore, it is not possible for different iterations to have different control flow; that is, they must not branch. It follows thatswitchstatements are not allowed. However,ifstatements are allowed if they can be implemented as masked assignments, which is usually the case. The calculation is performed for all data elements but the result is stored only for those elements for which the mask evaluates to true.
- Innermost loop of a nest:The only exception is if an original outer loop is transformed into an inner loop as a result of some other prior optimization phase, such as unrolling, loop collapsing or interchange,or an original outermost loop is transformed to an innermost loop due to loop materialization.
- Without function calls:Even anon-standard loop is not a vectorization candidate. The two major exceptions are for intrinsic math functions and for functions that may be inlined.