From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 245143858D33; Wed, 18 Oct 2023 19:53:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 245143858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1697658798; bh=y/FMS3vKNVJKAhpsFVX1HUoTLcNmVEnhvyLNSoUc5bs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ZAS+6MUo0Wxs+ZrEnxV4KWwhtNvR5Eu7jCyBiB/AD25jhkFiNe+/8tvbxLt/OK5z+ sRPx4oc8B50Iho5mxdupq1drZMuM9fP0WsopVKRLjcVq+J0kp5pUPdTbDW355uudf1 Nj4XCFV4xzCf2HxupoykUX5KUEMQ3y1arf5PcP0c= From: "rdapp at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/111791] RISC-V: Strange loop vectorizaion on popcount function Date: Wed, 18 Oct 2023 19:53:17 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: rdapp at gcc dot gnu.org X-Bugzilla-Status: NEW 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: Message-ID: In-Reply-To: References: 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 --- Comment #4 from Robin Dapp --- This is a scalar popcount and as Kito already noted we will just emit cpop a0, a0 once the zbb extension is present. As to the question what is actually being vectorized here, I'm not so sure = :D It looks like we're generating a vectorized scalar popcount by something li= ke a reduction? But we already did the call to __popcountdi2? Analyzing loop at pr111791.c:8 pr111791.c:8:25: note: =3D=3D=3D analyze_loop_nest =3D=3D=3D pr111791.c:8:25: note: =3D=3D=3D vect_analyze_loop_form =3D=3D=3D pr111791.c:8:25: note: =3D=3D=3D get_loop_niters =3D=3D=3D Matching expression match.pd:1919, generic-match-8.cc:27 Applying pattern match.pd:1975, generic-match-2.cc:4670 Matching expression match.pd:2707, generic-match-4.cc:36 Matching expression match.pd:2710, generic-match-3.cc:53 Matching expression match.pd:2717, generic-match-2.cc:23 Matching expression match.pd:2707, generic-match-4.cc:36 Matching expression match.pd:2710, generic-match-3.cc:53 Matching expression match.pd:2717, generic-match-2.cc:23 Matching expression match.pd:2707, generic-match-4.cc:36 Matching expression match.pd:2710, generic-match-3.cc:53 Matching expression match.pd:2717, generic-match-2.cc:23 Matching expression match.pd:148, generic-match-10.cc:27 Matching expression match.pd:148, generic-match-10.cc:27 Applying pattern match.pd:4519, generic-match-4.cc:2923 Applying pattern match.pd:201, generic-match-4.cc:3103 Applying pattern match.pd:3393, generic-match-2.cc:182 pr111791.c:8:25: note: Symbolic number of iterations is (unsigned intD.4) __builtin_popcountlD.1952 (value_4(D)) Ah, interesting: ranger(?) recognizes that the loop runs "popcount" iterati= ons. Shouldn't that still be 64? Well, it probably knows better :) Regardless, we use this symbolic value as number of iterations: _5 =3D __builtin_popcountlD.1952 (value_4(D)); niters.4_9 =3D (unsigned intD.4) _5; _2 =3D __builtin_popcountlD.1952 (value_4(D)); bnd.5_3 =3D (unsigned intD.4) _2; _23 =3D (unsigned long) bnd.5_3; Then, it gets funnier: # nbits_11 =3D PHI # vect_vec_iv_.6_15 =3D PHI <_16(6), { 0, 1, 2, ... }(5)> # ivtmp_24 =3D PHI _26 =3D .SELECT_VL (ivtmp_24, POLY_INT_CST [4, 4]); _16 =3D vect_vec_iv_.6_15 + { POLY_INT_CST [4, 4], ... }; vect_nbits_7.7_18 =3D vect_vec_iv_.6_15 + { 1, ... }; # RANGE [irange] int [1, 65] nbits_7 =3D nbits_11 + 1; # RANGE [irange] long unsigned int [0, 18446744073709551614] _1 =3D value_10 + 18446744073709551615; # RANGE [irange] long unsigned int [0, 18446744073709551614] value_8 =3D _1 & value_10; ivtmp_25 =3D ivtmp_24 - _26; i.e. we have a vector IV that we add to the vectorized nbits. Finally we extract the niter-th (=3Dpopcount) element from that vector only to get - popcount :) Still not sure why that happens but a vector-mode popcount expander doesn't help here as everything is scalar. Maybe the explanation is simple in that= we would vectorize such a loop anyway and here it just looks particularly bad because we already know the result via ranger?=