]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
tree-optimization/106904 - bogus -Wstringopt-overflow with vectors
authorRichard Biener <rguenther@suse.de>
Wed, 7 Dec 2022 13:42:24 +0000 (14:42 +0100)
committerRichard Biener <rguenther@suse.de>
Wed, 15 Mar 2023 09:04:00 +0000 (10:04 +0100)
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.

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.

(cherry picked from commit f8d136e50e6f82cba793483d910a2b2643108508)

gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c [new file with mode: 0644]
gcc/tree-vect-data-refs.cc
gcc/tree.cc
gcc/tree.h

diff --git a/gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c b/gcc/testsuite/gcc.dg/Wstringop-overflow-pr106904.c
new file mode 100644 (file)
index 0000000..15e67c2
--- /dev/null
@@ -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" } */
+}
index 09223baf71890b34ebed004560fafbd60691530d..4e615b80b3a6feb08acfc2a524c65f658d27943e 100644 (file)
@@ -4838,11 +4838,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);
index 8b3b37cd1203705de85bbc889595d74216aea230..e6593de87b63b4184d820cd2282c4f11325333c4 100644 (file)
@@ -11983,6 +11983,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.  */
index f3ae6d22f8eb84847eaf681099d08df7f8b91c31..3ff7732dc038bba152379e7ff83dbfc5baac7f5e 100644 (file)
@@ -5274,6 +5274,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);