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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |siddhesh at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I've tried
--- builtins.cc.jj3     2026-02-06 11:18:47.051640642 +0100
+++ builtins.cc 2026-03-03 14:46:28.810230892 +0100
@@ -11834,21 +11834,29 @@ fold_builtin_object_size (tree ptr, tree
   if (TREE_SIDE_EFFECTS (ptr))
     return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);

+  tree orig_ptr = ptr;
+  STRIP_NOPS (ptr);
+  if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
+    return NULL_TREE;
+
   if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
     object_size_type |= OST_DYNAMIC;

   if (TREE_CODE (ptr) == ADDR_EXPR)
     {
-      compute_builtin_object_size (ptr, object_size_type, &bytes);
-      if ((object_size_type & OST_DYNAMIC)
-         || int_fits_type_p (bytes, size_type_node))
+      /* If STRIP_NOPS stripped some casts and object size is not
+        know yet, delay folding until later.  Maybe subsequent passes
+        will help determining it.  */
+      if ((compute_builtin_object_size (ptr, object_size_type, &bytes)
+          || ptr == orig_ptr)
+         && ((object_size_type & OST_DYNAMIC)
+             || int_fits_type_p (bytes, size_type_node)))
        return fold_convert (size_type_node, bytes);
     }
   else if (TREE_CODE (ptr) == SSA_NAME)
     {
       /* If object size is not known yet, delay folding until
-       later.  Maybe subsequent passes will help determining
-       it.  */
+        later.  Maybe subsequent passes will help determining it.  */
       if (compute_builtin_object_size (ptr, object_size_type, &bytes)
          && ((object_size_type & OST_DYNAMIC)
              || int_fits_type_p (bytes, size_type_node)))
but that regresses builtin-dynamic-object-size-0.c
  if (test_dynarray_struct_subobj (42, 48) != 0)
    FAIL ();
case where it changes (among other things, but this one is what breaks)
@@ -663,8 +668,7 @@ size_t test_dynarray_struct_subobj (size
       _9 = sz.30 * 32;
       D.3649 = _9;
       bin.32 = __builtin_alloca_with_align (D.3649, 64);
-      _10 = &(*bin.32)[off].c[4];
-      D.3651 = __builtin_dynamic_object_size (_10, 1);
+      D.3651 = 12;
       return D.3651;
     }
   finally

So, I think it would be safer to do it in the C++ FE only and limit to
manifestly-constant evaluation.

Reply via email to