Fix wrong code with non-linear induction vectorization
Corrects vectorization of non-linear induction variables with shifts to avoid incorrect code generation.
A bug in GCC’s vectorization pass could generate incorrect code for non-linear induction variables involving shifts. The issue stemmed from an incomplete check of the shift amount, which only considered loop iteration counts (nunits) instead of vector factor counts (VF). This commit corrects the validation logic to properly account for the vector width, ensuring accurate code generation for these patterns.
In Details
The vectorizable_nonlinear_induction function in tree-vect-loop.cc incorrectly validated shift amounts for non-linear induction variables. It only checked against nunits (number of loop units) rather than VF (vectorization factor). This commit fixes the check to use VF for shift amount validation, preventing incorrect code generation when vectorizing loops with non-linear induction variables and shifts.
- vectorization
- An optimization technique where a single instruction operates on multiple data elements simultaneously. This is achieved by using special vector registers and instructions.
- induction variable
- A variable in a loop whose value is updated by adding a constant or an expression involving loop-invariant variables in each iteration. It's commonly used for array indexing.
- non-linear induction variable
- An induction variable whose update step is not a simple constant addition, but rather a more complex expression, such as a multiplication or a shift, that might depend on the loop iteration count in a non-additive way.
- VF
- Vectorization Factor. The number of data elements that can be processed in parallel by a single vector instruction. It corresponds to the width of the vector registers and the chosen vectorization strategy.