https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111791
Bug ID: 111791 Summary: RISC-V: Strange loop vectorizaion on popcount function Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: kito at gcc dot gnu.org Target Milestone: --- Symptom: A typical popcount implementation with Brian Kernighan’s algorithm, vectorizer has recognized that as popcount, but...come with strange vectorization result, I know that might because I add -fno-vect-cost-model, but I still don't understand why it vectorized, so I guess maybe it's something worth to report. NOTE: Those bad/strange code gen will gone once scalar popcount instruction available. Case: ``` int popcount(unsigned long value) { int nbits; for (nbits = 0; value != 0; value &= value - 1) nbits++; return nbits; } ``` Command to reproduce: ``` $ riscv64-unknown-linux-gnu-gcc x.c -march=rv64gcv -o - -S -fno-vect-cost-model -O3 ``` Sha1: g:faae30c49560f1481f036061fa2f894b0f7257f8 (some random point of top of trunk) Current output: ``` .globl popcount .type popcount, @function popcount: .LFB0: .cfi_startproc beq a0,zero,.L4 addi sp,sp,-16 .cfi_def_cfa_offset 16 sd ra,8(sp) .cfi_offset 1, -8 call __popcountdi2 csrr a2,vlenb sext.w a0,a0 srli a2,a2,2 vsetvli a3,zero,e32,m1,ta,ma vid.v v1 .L3: vsetvli a5,a0,e8,mf4,ta,ma sub a0,a0,a5 vsetvli a3,zero,e32,m1,ta,ma vmv1r.v v3,v1 vmv.v.x v2,a2 vadd.vv v1,v1,v2 bne a0,zero,.L3 ld ra,8(sp) .cfi_restore 1 addi a5,a5,-1 vadd.vi v3,v3,1 vslidedown.vx v3,v3,a5 addi sp,sp,16 .cfi_def_cfa_offset 0 vmv.x.s a0,v3 jr ra .L4: li a0,0 ret .cfi_endproc .LFE0: .size popcount, .-popcount ```