public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "xry111 at gcc dot gnu.org" <gcc-bugzilla@gcc.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	[thread overview]
Message-ID: <bug-110557-4-t9CjBQYD4w@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110557-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110557

--- Comment #4 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
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 = (type_out) container;
    masked = container & (((1 << bitsize) - 1) << bitpos);
-   result = patt2 >> masked;
+   result = masked >> bitpos;

    Widening with shift first, mask last:
    container = (type_out) container;
@@ -2578,6 +2578,15 @@ vect_recog_widen_sum_pattern (vec_info *vinfo,
    result = masked >> bitpos;
    result = (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 = (type) container;
+   masked = container << (prec - bitsize - bitpos);
+   result = (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 != 0.

 */
@@ -2636,14 +2645,22 @@ vect_recog_bitfield_ref_pattern (vec_info *vinfo,
stmt_vec_info stmt_info,
   if (BYTES_BIG_ENDIAN)
     shift_n = prec - shift_n - mask_width;

+  bool sign_ext = !TYPE_UNSIGNED (TREE_TYPE (bf_ref)) &&
+                 TYPE_PRECISION (ret_type) > mask_width;
+  bool widening = ((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
-       = gimple_build_assign (vect_recog_temp_ssa_var (ret_type),
-                              NOP_EXPR, container);
+      tree type = widening ? ret_type : container_type;
+      if (sign_ext)
+       type = gimple_signed_type (type);
+      pattern_stmt = gimple_build_assign (vect_recog_temp_ssa_var (type),
+                                         NOP_EXPR, container);
       container = gimple_get_lhs (pattern_stmt);
       container_type = TREE_TYPE (container);
       prec = 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 = true;

   tree result;
-  if (shift_first)
+  if (shift_first && !sign_ext)
     {
       tree shifted = container;
       if (shift_n)
@@ -2694,14 +2711,27 @@ vect_recog_bitfield_ref_pattern (vec_info *vinfo,
stmt_vec_info stmt_info,
     }
   else
     {
-      tree mask = wide_int_to_tree (container_type,
-                                   wi::shifted_mask (shift_n, mask_width,
-                                                     false, prec));
-      pattern_stmt
-       = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
-                              BIT_AND_EXPR, container, mask);
-      tree masked = gimple_assign_lhs (pattern_stmt);
+      tree temp = vect_recog_temp_ssa_var (container_type);
+      if (!sign_ext)
+       {
+         tree mask = wide_int_to_tree (container_type,
+                                       wi::shifted_mask (shift_n,
+                                                         mask_width,
+                                                         false, prec));
+         pattern_stmt = gimple_build_assign (temp, BIT_AND_EXPR,
+                                             container, mask);
+       }
+      else
+       {
+         HOST_WIDE_INT shl = prec - shift_n - mask_width;
+         shift_n += shl;
+         pattern_stmt = gimple_build_assign (temp, LSHIFT_EXPR,
+                                             container,
+                                             build_int_cst (sizetype,
+                                                            shl));
+       }

+      tree masked = gimple_assign_lhs (pattern_stmt);
       append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
       pattern_stmt
        = gimple_build_assign (vect_recog_temp_ssa_var (container_type),

  parent reply	other threads:[~2023-07-06 11:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-05 11:10 [Bug target/110557] New: " xry111 at gcc dot gnu.org
2023-07-05 11:11 ` [Bug target/110557] [13/14 Regression] " xry111 at gcc dot gnu.org
2023-07-05 13:15 ` [Bug tree-optimization/110557] " pinskia at gcc dot gnu.org
2023-07-06  8:17 ` rguenth at gcc dot gnu.org
2023-07-06 10:17 ` avieira at gcc dot gnu.org
2023-07-06 10:21 ` xry111 at gcc dot gnu.org
2023-07-06 11:30 ` xry111 at gcc dot gnu.org [this message]
2023-07-06 14:58 ` avieira at gcc dot gnu.org
2023-07-06 16:18 ` xry111 at gcc dot gnu.org
2023-07-06 16:32 ` xry111 at gcc dot gnu.org
2023-07-10 10:36 ` cvs-commit at gcc dot gnu.org
2023-07-10 10:36 ` cvs-commit at gcc dot gnu.org
2023-07-10 10:38 ` xry111 at gcc dot gnu.org
2023-07-11  8:16 ` xry111 at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110557-4-t9CjBQYD4w@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).