public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: [PATCH] tree-optimization/106904 - bogus -Wstringopt-overflow with vectors
@ 2022-12-11 13:34 Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2022-12-11 13:34 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jakub Jelinek

On Wed, 7 Dec 2022, Richard Biener wrote:

> The following avoids CSE of &ps->wp to &ps->wp.hwnd confusing
> -Wstringopt-overflow by making sure to produce addresses to the
> biggest container from vectorization.  For this I introduce
> strip_zero_offset_components which turns &ps->wp.hwnd into
> &(*ps) and use that to base the vector data references on.
> That will also work for addresses with variable components,
> alternatively emitting pointer arithmetic via calling
> get_inner_reference and gimplifying that would be possible
> but likely more intrusive.
> 
> This is by no means a complete fix for all of those issues
> (avoiding ADDR_EXPRs in favor of pointer arithmetic might be).
> Other passes will have similar issues.
> 
> In theory that might now cause false negatives.
> 
> Bootstrap and regtest running on x86_64-unknown-linux-gnu.
> 
> Any opinion?

I have now pushed this.

Richard.

> Thanks,
> Richard.
> 
> 	PR tree-optimization/106904
> 	* tree.h (strip_zero_offset_components): Declare.
> 	* tree.cc (strip_zero_offset_components): Define.
> 	* tree-vect-data-refs.cc (vect_create_addr_base_for_vector_ref):
> 	Strip zero offset components before building the address.
> 
> 	* gcc.dg/Wstringop-overflow-pr106904.c: New testcase.
> ---
>  .../gcc.dg/Wstringop-overflow-pr106904.c      | 30 +++++++++++++++++++
>  gcc/tree-vect-data-refs.cc                    | 12 ++++----
>  gcc/tree.cc                                   | 12 ++++++++
>  gcc/tree.h                                    |  1 +
>  4 files changed, 50 insertions(+), 5 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c
> 
> diff --git a/gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c b/gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c
> new file mode 100644
> index 00000000000..15e67c28c15
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c
> @@ -0,0 +1,30 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -Wstringop-overflow -fno-vect-cost-model" } */
> +
> +struct windowpos
> +{
> +  int hwnd;
> +  int hwnd2;
> +};
> +
> +struct packed_windowpos
> +{
> +  int hwnd;
> +  int pad1;
> +  int hwnd2;
> +  int pad2;
> +};
> +
> +struct packed_structs
> +{
> +  struct packed_windowpos wp;
> +};
> +
> +void func(struct packed_structs *ps)
> +{
> +  struct windowpos wp;
> +
> +  wp.hwnd = ps->wp.hwnd;
> +  wp.hwnd2 = ps->wp.hwnd2;
> +  __builtin_memcpy(&ps->wp, &wp, sizeof(wp)); /* { dg-bogus "into a region" } */
> +}
> diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc
> index 6c892791bd4..18b0f962670 100644
> --- a/gcc/tree-vect-data-refs.cc
> +++ b/gcc/tree-vect-data-refs.cc
> @@ -4845,11 +4845,13 @@ vect_create_addr_base_for_vector_ref (vec_info *vinfo, stmt_vec_info stmt_info,
>    if (loop_vinfo)
>      addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
>    else
> -    {
> -      addr_base = build1 (ADDR_EXPR,
> -			  build_pointer_type (TREE_TYPE (DR_REF (dr))),
> -			  unshare_expr (DR_REF (dr)));
> -    }
> +    addr_base = build1 (ADDR_EXPR,
> +			build_pointer_type (TREE_TYPE (DR_REF (dr))),
> +			/* Strip zero offset components since we don't need
> +			   them and they can confuse late diagnostics if
> +			   we CSE them wrongly.  See PR106904 for example.  */
> +			unshare_expr (strip_zero_offset_components
> +								(DR_REF (dr))));
>  
>    vect_ptr_type = build_pointer_type (TREE_TYPE (DR_REF (dr)));
>    dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
> diff --git a/gcc/tree.cc b/gcc/tree.cc
> index b40c95ae8c4..0a51f9ddb4d 100644
> --- a/gcc/tree.cc
> +++ b/gcc/tree.cc
> @@ -12014,6 +12014,18 @@ strip_invariant_refs (const_tree op)
>    return op;
>  }
>  
> +/* Strip handled components with zero offset from OP.  */
> +
> +tree
> +strip_zero_offset_components (tree op)
> +{
> +  while (TREE_CODE (op) == COMPONENT_REF
> +	 && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (op, 1)))
> +	 && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (op, 1))))
> +    op = TREE_OPERAND (op, 0);
> +  return op;
> +}
> +
>  static GTY(()) tree gcc_eh_personality_decl;
>  
>  /* Return the GCC personality function decl.  */
> diff --git a/gcc/tree.h b/gcc/tree.h
> index 1c810c0b21b..065ad527c3f 100644
> --- a/gcc/tree.h
> +++ b/gcc/tree.h
> @@ -5373,6 +5373,7 @@ extern bool tree_nop_conversion_p (const_tree, const_tree);
>  extern tree tree_strip_nop_conversions (tree);
>  extern tree tree_strip_sign_nop_conversions (tree);
>  extern const_tree strip_invariant_refs (const_tree);
> +extern tree strip_zero_offset_components (tree);
>  extern tree lhd_gcc_personality (void);
>  extern void assign_assembler_name_if_needed (tree);
>  extern bool warn_deprecated_use (tree, tree);
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg,
Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman;
HRB 36809 (AG Nuernberg)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] tree-optimization/106904 - bogus -Wstringopt-overflow with vectors
  2022-12-07 13:54 Richard Biener
@ 2022-12-17  0:36 ` Jeff Law
  0 siblings, 0 replies; 3+ messages in thread
From: Jeff Law @ 2022-12-17  0:36 UTC (permalink / raw)
  To: Richard Biener, gcc-patches; +Cc: Jakub Jelinek



On 12/7/22 06:54, Richard Biener via Gcc-patches wrote:
> The following avoids CSE of &ps->wp to &ps->wp.hwnd confusing
> -Wstringopt-overflow by making sure to produce addresses to the
> biggest container from vectorization.  For this I introduce
> strip_zero_offset_components which turns &ps->wp.hwnd into
> &(*ps) and use that to base the vector data references on.
> That will also work for addresses with variable components,
> alternatively emitting pointer arithmetic via calling
> get_inner_reference and gimplifying that would be possible
> but likely more intrusive.
> 
> This is by no means a complete fix for all of those issues
> (avoiding ADDR_EXPRs in favor of pointer arithmetic might be).
> Other passes will have similar issues.
> 
> In theory that might now cause false negatives.
> 
> Bootstrap and regtest running on x86_64-unknown-linux-gnu.
> 
> Any opinion?
> 
> Thanks,
> Richard.
> 
> 	PR tree-optimization/106904
> 	* tree.h (strip_zero_offset_components): Declare.
> 	* tree.cc (strip_zero_offset_components): Define.
> 	* tree-vect-data-refs.cc (vect_create_addr_base_for_vector_ref):
> 	Strip zero offset components before building the address.
> 
> 	* gcc.dg/Wstringop-overflow-pr106904.c: New testcase.
So you're just canonicalizing to the widest container for a zero offset 
access.  While it may not fix everything, that seems like a good thing 
in general when we can do so.  I wouldn't be surprised if other passes 
could do the same thing to fix some of the missed CSE opportunities.

jeff

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] tree-optimization/106904 - bogus -Wstringopt-overflow with vectors
@ 2022-12-07 13:54 Richard Biener
  2022-12-17  0:36 ` Jeff Law
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Biener @ 2022-12-07 13:54 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jakub Jelinek

The following avoids CSE of &ps->wp to &ps->wp.hwnd confusing
-Wstringopt-overflow by making sure to produce addresses to the
biggest container from vectorization.  For this I introduce
strip_zero_offset_components which turns &ps->wp.hwnd into
&(*ps) and use that to base the vector data references on.
That will also work for addresses with variable components,
alternatively emitting pointer arithmetic via calling
get_inner_reference and gimplifying that would be possible
but likely more intrusive.

This is by no means a complete fix for all of those issues
(avoiding ADDR_EXPRs in favor of pointer arithmetic might be).
Other passes will have similar issues.

In theory that might now cause false negatives.

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

Any opinion?

Thanks,
Richard.

	PR tree-optimization/106904
	* tree.h (strip_zero_offset_components): Declare.
	* tree.cc (strip_zero_offset_components): Define.
	* tree-vect-data-refs.cc (vect_create_addr_base_for_vector_ref):
	Strip zero offset components before building the address.

	* gcc.dg/Wstringop-overflow-pr106904.c: New testcase.
---
 .../gcc.dg/Wstringop-overflow-pr106904.c      | 30 +++++++++++++++++++
 gcc/tree-vect-data-refs.cc                    | 12 ++++----
 gcc/tree.cc                                   | 12 ++++++++
 gcc/tree.h                                    |  1 +
 4 files changed, 50 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c

diff --git a/gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c b/gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c
new file mode 100644
index 00000000000..15e67c28c15
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wstringop-overflow -fno-vect-cost-model" } */
+
+struct windowpos
+{
+  int hwnd;
+  int hwnd2;
+};
+
+struct packed_windowpos
+{
+  int hwnd;
+  int pad1;
+  int hwnd2;
+  int pad2;
+};
+
+struct packed_structs
+{
+  struct packed_windowpos wp;
+};
+
+void func(struct packed_structs *ps)
+{
+  struct windowpos wp;
+
+  wp.hwnd = ps->wp.hwnd;
+  wp.hwnd2 = ps->wp.hwnd2;
+  __builtin_memcpy(&ps->wp, &wp, sizeof(wp)); /* { dg-bogus "into a region" } */
+}
diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc
index 6c892791bd4..18b0f962670 100644
--- a/gcc/tree-vect-data-refs.cc
+++ b/gcc/tree-vect-data-refs.cc
@@ -4845,11 +4845,13 @@ vect_create_addr_base_for_vector_ref (vec_info *vinfo, stmt_vec_info stmt_info,
   if (loop_vinfo)
     addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
   else
-    {
-      addr_base = build1 (ADDR_EXPR,
-			  build_pointer_type (TREE_TYPE (DR_REF (dr))),
-			  unshare_expr (DR_REF (dr)));
-    }
+    addr_base = build1 (ADDR_EXPR,
+			build_pointer_type (TREE_TYPE (DR_REF (dr))),
+			/* Strip zero offset components since we don't need
+			   them and they can confuse late diagnostics if
+			   we CSE them wrongly.  See PR106904 for example.  */
+			unshare_expr (strip_zero_offset_components
+								(DR_REF (dr))));
 
   vect_ptr_type = build_pointer_type (TREE_TYPE (DR_REF (dr)));
   dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
diff --git a/gcc/tree.cc b/gcc/tree.cc
index b40c95ae8c4..0a51f9ddb4d 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -12014,6 +12014,18 @@ strip_invariant_refs (const_tree op)
   return op;
 }
 
+/* Strip handled components with zero offset from OP.  */
+
+tree
+strip_zero_offset_components (tree op)
+{
+  while (TREE_CODE (op) == COMPONENT_REF
+	 && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (op, 1)))
+	 && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (op, 1))))
+    op = TREE_OPERAND (op, 0);
+  return op;
+}
+
 static GTY(()) tree gcc_eh_personality_decl;
 
 /* Return the GCC personality function decl.  */
diff --git a/gcc/tree.h b/gcc/tree.h
index 1c810c0b21b..065ad527c3f 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -5373,6 +5373,7 @@ extern bool tree_nop_conversion_p (const_tree, const_tree);
 extern tree tree_strip_nop_conversions (tree);
 extern tree tree_strip_sign_nop_conversions (tree);
 extern const_tree strip_invariant_refs (const_tree);
+extern tree strip_zero_offset_components (tree);
 extern tree lhd_gcc_personality (void);
 extern void assign_assembler_name_if_needed (tree);
 extern bool warn_deprecated_use (tree, tree);
-- 
2.35.3

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-17  0:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-11 13:34 [PATCH] tree-optimization/106904 - bogus -Wstringopt-overflow with vectors Richard Biener
  -- strict thread matches above, loose matches on Subject: below --
2022-12-07 13:54 Richard Biener
2022-12-17  0:36 ` Jeff Law

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).