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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
First of all, we shouldn't create such weirdo arrays, so I think we want
something like:
--- gcc/tree-ssa-ccp.c.jj       2019-09-20 12:25:26.809718354 +0200
+++ gcc/tree-ssa-ccp.c  2019-09-23 19:38:03.530722874 +0200
@@ -2223,7 +2223,18 @@ fold_builtin_alloca_with_align (gimple *
   /* Declare array.  */
   elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
   n_elem = size * 8 / BITS_PER_UNIT;
-  array_type = build_array_type_nelts (elem_type, n_elem);
+  if (n_elem == 0)
+    {
+      /* For alloca (0), use array type similar to C zero-length arrays.  */
+      tree range_type = build_range_type (sizetype, size_zero_node,
NULL_TREE);
+      array_type = build_array_type (elem_type, range_type);
+      array_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (array_type));
+      TYPE_SIZE (array_type) = bitsize_zero_node;
+      TYPE_SIZE_UNIT (array_type) = size_zero_node;
+      SET_TYPE_STRUCTURAL_EQUALITY (array_type);
+    }
+  else
+    array_type = build_array_type_nelts (elem_type, n_elem);
   var = create_tmp_var (array_type);
   SET_DECL_ALIGN (var, TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)));
   if (uid != 0)
because build_array_type_nelts only makes sense for non-zero element counts.
We still warn even with that, as we would if user just added say some if (D ==
0) something (); and the compiler decided to jump thread it.
It would be nice to take sanitizer instrumentation into account when doing jump
threading, but probably disabling it completely isn't the best idea, it can be
e.g. effective in removing multiple identical checks, though we should limit
the size growth due to that as the sanitization is cold.

Reply via email to