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

--- Comment #14 from qinzhao at gcc dot gnu.org ---
All the 3 testing cases can be resolved by adding the following patch (check
whether the type has padding before adding call to __builtin_clear_padding):

I believe that this is a safe partial fix for this bug. We might need to put
this partial fix in first. 

diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index 7fcfef41f72..e100f5bd1f5 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -4651,6 +4651,31 @@ clear_padding_type_may_have_padding_p (tree type)
     }
 }

+/* Return true if TYPE contains any padding bits.  */
+
+bool
+clear_padding_type_has_padding_p (tree type)
+{
+  bool has_padding = false;
+  if (BITS_PER_UNIT == 8
+      && CHAR_BIT == 8
+      && clear_padding_type_may_have_padding_p (type))
+    {
+      HOST_WIDE_INT sz = int_size_in_bytes (type), i;
+      gcc_assert (sz > 0);
+      unsigned char *buf = XALLOCAVEC (unsigned char, sz);
+      memset (buf, ~0, sz);
+      clear_type_padding_in_mask (type, buf);
+      for (i = 0; i < sz; i++)
+      if (buf[i] != (unsigned char) ~0)
+       {
+         has_padding = true;
+         break;
+       }
+    }
+  return has_padding;
+}
+
 /* Emit a runtime loop:
    for (; buf.base != end; buf.base += sz)
      __builtin_clear_padding (buf.base);  */
diff --git a/gcc/gimple-fold.h b/gcc/gimple-fold.h
index 397f4aeb7cf..eb750a68eca 100644
--- a/gcc/gimple-fold.h
+++ b/gcc/gimple-fold.h
@@ -37,6 +37,7 @@ extern tree maybe_fold_and_comparisons (tree, enum tree_code,
tree, tree,
 extern tree maybe_fold_or_comparisons (tree, enum tree_code, tree, tree,
                                       enum tree_code, tree, tree);
 extern bool clear_padding_type_may_have_padding_p (tree);
+extern bool clear_padding_type_has_padding_p (tree);
 extern void clear_type_padding_in_mask (tree, unsigned char *);
 extern bool optimize_atomic_compare_exchange_p (gimple *);
 extern void fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *);
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index d8e4b139349..62684dd6338 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -1955,7 +1955,8 @@ gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
             In order to make the paddings as zeroes for pattern init, We
             should add a call to __builtin_clear_padding to clear the
             paddings to zero in compatiple with CLANG.  */
-         if (flag_auto_var_init == AUTO_INIT_PATTERN)
+         if (flag_auto_var_init == AUTO_INIT_PATTERN
+             && clear_padding_type_has_padding_p (TREE_TYPE (decl)))
            gimple_add_padding_init_for_auto_var (decl, is_vla, seq_p);
        }
     }
@@ -5390,6 +5391,7 @@ gimplify_init_constructor (tree *expr_p, gimple_seq
*pre_p, gimple_seq *post_p,
      variable has be completely cleared already or it's initialized
      with an empty constructor.  */
   if (is_init_expr
+      && clear_padding_type_has_padding_p (type)
       && ((AGGREGATE_TYPE_P (type) && !cleared && !is_empty_ctor)
          || !AGGREGATE_TYPE_P (type))
       && is_var_need_auto_init (object))

Reply via email to