From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 876363858D37; Thu, 6 Jul 2023 11:30:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 876363858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688643035; bh=JKJNiW9AAQkyR1XfZRPg6grcunn+SdJF7qFGjlatYmE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ksnECjDX4V0tTW76FkyxeSeaUkFOZRG/9fkLA/RwjgTyh+6rblIrq7XmakUFP0qGT oNu5AjnwlVdCniPkjCrdKd+9xMgkeakBCc0/1sfUe4CPaeMdAjpaDsnfcHldEa7h9h b6kZ7JTqoATLxZjSMzNE+WMERrgR3kSrFbUIauEc= From: "xry111 at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/110557] [13/14 Regression] Wrong code for x86_64-linux-gnu with -O3 -mavx2: vectorized loop mishandles signed bit-fields Date: Thu, 06 Jul 2023 11:30:35 +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: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: xry111 at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: avieira at gcc dot gnu.org X-Bugzilla-Target-Milestone: 13.3 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=3D110557 --- Comment #4 from Xi Ruoyao --- Untested patch: diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc index de20e9d59cb..01df568ee61 100644 --- a/gcc/tree-vect-patterns.cc +++ b/gcc/tree-vect-patterns.cc @@ -2566,7 +2566,7 @@ vect_recog_widen_sum_pattern (vec_info *vinfo, Widening with mask first, shift later: container =3D (type_out) container; masked =3D container & (((1 << bitsize) - 1) << bitpos); - result =3D patt2 >> masked; + result =3D masked >> bitpos; Widening with shift first, mask last: container =3D (type_out) container; @@ -2578,6 +2578,15 @@ vect_recog_widen_sum_pattern (vec_info *vinfo, result =3D masked >> bitpos; result =3D (type_out) result; + If the bitfield is signed and the its width is greater than the width + of type_out, we need to perform a sign-extension: + container =3D (type) container; + masked =3D container << (prec - bitsize - bitpos); + result =3D (type_out) (masked >> (prec - bitsize)); + + Here type is the signed variant of the wider of type_out and the type + of container. + The shifting is always optional depending on whether bitpos !=3D 0. */ @@ -2636,14 +2645,22 @@ vect_recog_bitfield_ref_pattern (vec_info *vinfo, stmt_vec_info stmt_info, if (BYTES_BIG_ENDIAN) shift_n =3D prec - shift_n - mask_width; + bool sign_ext =3D !TYPE_UNSIGNED (TREE_TYPE (bf_ref)) && + TYPE_PRECISION (ret_type) > mask_width; + bool widening =3D ((TYPE_PRECISION (TREE_TYPE (container)) < + TYPE_PRECISION (ret_type)) + && !useless_type_conversion_p (TREE_TYPE (container), + ret_type)); + /* We move the conversion earlier if the loaded type is smaller than the return type to enable the use of widening loads. */ - if (TYPE_PRECISION (TREE_TYPE (container)) < TYPE_PRECISION (ret_type) - && !useless_type_conversion_p (TREE_TYPE (container), ret_type)) + if (sign_ext || widening) { - pattern_stmt - =3D gimple_build_assign (vect_recog_temp_ssa_var (ret_type), - NOP_EXPR, container); + tree type =3D widening ? ret_type : container_type; + if (sign_ext) + type =3D gimple_signed_type (type); + pattern_stmt =3D gimple_build_assign (vect_recog_temp_ssa_var (type), + NOP_EXPR, container); container =3D gimple_get_lhs (pattern_stmt); container_type =3D TREE_TYPE (container); prec =3D tree_to_uhwi (TYPE_SIZE (container_type)); @@ -2671,7 +2688,7 @@ vect_recog_bitfield_ref_pattern (vec_info *vinfo, stmt_vec_info stmt_info, shift_first =3D true; tree result; - if (shift_first) + if (shift_first && !sign_ext) { tree shifted =3D container; if (shift_n) @@ -2694,14 +2711,27 @@ vect_recog_bitfield_ref_pattern (vec_info *vinfo, stmt_vec_info stmt_info, } else { - tree mask =3D wide_int_to_tree (container_type, - wi::shifted_mask (shift_n, mask_width, - false, prec)); - pattern_stmt - =3D gimple_build_assign (vect_recog_temp_ssa_var (container_type), - BIT_AND_EXPR, container, mask); - tree masked =3D gimple_assign_lhs (pattern_stmt); + tree temp =3D vect_recog_temp_ssa_var (container_type); + if (!sign_ext) + { + tree mask =3D wide_int_to_tree (container_type, + wi::shifted_mask (shift_n, + mask_width, + false, prec)); + pattern_stmt =3D gimple_build_assign (temp, BIT_AND_EXPR, + container, mask); + } + else + { + HOST_WIDE_INT shl =3D prec - shift_n - mask_width; + shift_n +=3D shl; + pattern_stmt =3D gimple_build_assign (temp, LSHIFT_EXPR, + container, + build_int_cst (sizetype, + shl)); + } + tree masked =3D gimple_assign_lhs (pattern_stmt); append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype); pattern_stmt =3D gimple_build_assign (vect_recog_temp_ssa_var (container_type),=