The following makes sure to perform the vector element extraction
using the element type and convert to the original, possibly
bit-precision, result afterwards.  I've also used gimple_build
for the BIT_FIELD_REF since that simplifies the code.

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.

        PR tree-optimization/122158
        * tree-vect-loop.cc (vect_create_epilog_for_reduction): Handle
        bit-precision result.

        * gcc.dg/vect/pr122158.c: New testcase.
---
 gcc/testsuite/gcc.dg/vect/pr122158.c | 15 +++++++++++++++
 gcc/tree-vect-loop.cc                | 14 +++++---------
 2 files changed, 20 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/pr122158.c

diff --git a/gcc/testsuite/gcc.dg/vect/pr122158.c 
b/gcc/testsuite/gcc.dg/vect/pr122158.c
new file mode 100644
index 00000000000..5d0f7dac7dc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr122158.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+
+int g_3, g_181, g_7;
+void func_54(unsigned int, int *);
+int func_24()
+{
+  int p_25;
+  int *l_49 = &g_3;
+  func_54(g_7, l_49);
+}
+void func_54(unsigned int p_55, int *p_56)
+{
+  for (g_181 = 27; g_181 != 9; --g_181)
+    p_55 || (*p_56 = 0);
+}
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 160cc37a0d6..003dc734c01 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -6055,8 +6055,6 @@ vect_create_epilog_for_reduction (loop_vec_info 
loop_vinfo,
                   Create:  va = vop <va, va'>
                 }  */
 
-          tree rhs;
-
           if (dump_enabled_p ())
             dump_printf_loc (MSG_NOTE, vect_location,
                             "Reduce using vector shifts\n");
@@ -6075,7 +6073,6 @@ vect_create_epilog_for_reduction (loop_vec_info 
loop_vinfo,
              new_temp = gimple_build (&stmts, code,
                                       vectype1, new_name, new_temp);
             }
-         gsi_insert_seq_before (&exit_gsi, stmts, GSI_SAME_STMT);
 
          /* 2.4  Extract the final scalar result.  Create:
             s_out3 = extract_field <v_out2, bitpos>  */
@@ -6084,12 +6081,11 @@ vect_create_epilog_for_reduction (loop_vec_info 
loop_vinfo,
            dump_printf_loc (MSG_NOTE, vect_location,
                             "extract scalar result\n");
 
-         rhs = build3 (BIT_FIELD_REF, scalar_type, new_temp,
-                       bitsize, bitsize_zero_node);
-         epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
-         new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
-         gimple_assign_set_lhs (epilog_stmt, new_temp);
-         gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
+         new_temp = gimple_build (&stmts, BIT_FIELD_REF, TREE_TYPE (vectype1),
+                                  new_temp, bitsize, bitsize_zero_node);
+         new_temp = gimple_build (&stmts, VIEW_CONVERT_EXPR,
+                                  scalar_type, new_temp);
+         gsi_insert_seq_before (&exit_gsi, stmts, GSI_SAME_STMT);
          scalar_results.safe_push (new_temp);
         }
       else
-- 
2.51.0

Reply via email to