https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110449
Bug ID: 110449 Summary: Vect: use a small step to calculate the loop induction if the loop is unrolled during loop vectorization Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: hliu at amperecomputing dot com Target Milestone: --- This is inspired by clang. Compile the follwing case with "-mcpu=neoverse-n2 -O3": void foo(int *arr, int val, int step) { for (int i = 0; i < 1024; i++) { arr[i] = val; val += step; } } It will be unrolled by 2 during vectorization. GCC generates code: fmov s29, w2 # step shl v27.2s, v29.2s, 3 # 8*step shl v28.2s, v29.2s, 2 # 4*step ... .L2: mov v30.16b, v31.16b add v31.4s, v31.4s, v27.4s # += 8*step add v29.4s, v30.4s, v28.4s # += 4*step stp q30, q29, [x0] add x0, x0, 32 cmp x1, x0 bne .L2 The v27 (i.e. "8*step") is actually not necessary. We can use v29 + v28 (i.e. "+ 4*step") and generate simpler code: fmov s29, w2 # step shl v28.2s, v29.2s, 2 # 4*step ... .L2: add v29.4s, v30.4s, v28.4s # += 4*step stp q30, q29, [x0] add x0, x0, 32 add v30.4s, v29.4s, v28.4s # += 4*step cmp x1, x0 bne .L2 This has two benefits: (1) Save 1 vector register and one "mov" instructon (2) For floating point, the result value of small step should be closer to the original scalar result value than large step. I.e. "A + 4*step + ... + 4*step" should be closer to "A + step + ... + step" than "A + 8*step + ... 8*step". Do you think if this is reasonable? I have a simple patch to enhance the tree-vect-loop.cc "vectorizable_induction()" to achieve this. Will send out the patch for code review later.