From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 44AD53858C5E; Thu, 12 Oct 2023 19:56:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 44AD53858C5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1697140586; bh=d7QLkphP/0c3Zw2YkpF+IgkXw9oING6loYtgg1w3w/s=; h=From:To:Subject:Date:From; b=Aek8QiwfOuiNalz/js4Zlw3tixxarorjcrmuFAOvhPiAAslExZWSs2jClGFljVFX3 L+7gQ1F55aPLsicc4HGlrpXoWXRi90W1SI+Vz8a/QDa1PFtr8VegnRfvWVDSO+rM0M vvNnWPfK/I3I5jjFE+i2tFAeczzZ7fvX8WILzuQs= From: "kito at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/111791] New: RISC-V: Strange loop vectorizaion on popcount function Date: Thu, 12 Oct 2023 19:56:25 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: kito at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D111791 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=E2=80=99s algorithm,= vectorizer has recognized that as popcount, but...come with strange vectorization resu= lt, 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 repo= rt. NOTE: Those bad/strange code gen will gone once scalar popcount instruction available.=20 Case: ``` int popcount(unsigned long value) { int nbits; for (nbits =3D 0; value !=3D 0; value &=3D value - 1) nbits++; return nbits; } ``` Command to reproduce: ``` $ riscv64-unknown-linux-gnu-gcc x.c -march=3Drv64gcv -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 ```=