diff mbox series

[gcc-14/to-be-committed] tree-object-size: Fix reexamine propagation [PR 127899]

Message ID 20261104171816.4140292-1-siddhesh@gotplt.org
State New
Headers show
Series [gcc-14/to-be-committed] tree-object-size: Fix reexamine propagation [PR 127899] | expand

Commit Message

Siddhesh Poyarekar Nov. 4, 2026, 5:18 p.m. UTC
For OST_DYNAMIC, the reexamine flag is set to mark sites using temporary
SSA names so that they can be freed up during unknown propagation.  For
this to work correctly, unknown values should not
be marked for reexamination.  Fix this in plus_stmt_object_size.

Further, add reexamine propagation in cond_expr too since otherwise one
could have stale references to temporary SSA names that could
potentially get freed during unknown propagation.

Note that for gcc14, builtin-dynamic-object-size-pr127899-1.c has been
dropped since counted_by is not implemented.

gcc/ChangeLog:

	PR tree-optimization/127899
	* tree-object-size.cc (plus_stmt_object_size): Reset WHOLESIZE
	if BYTES is unknown.
	(dynamic_object_size): Return bool.
	(cond_expr_object_size): Adjust calls and return REEXAMINE.
	(collect_object_sizes_for): Add assert that size is unknown for
	reexamined objects.

gcc/testsuite/ChangeLog:

	PR tree-optimization/127899
	* gcc.dg/builtin-dynamic-object-size-pr127899-2.c: Likewise.
	* gcc.dg/builtin-dynamic-object-size-pr127899-3.c: Likewise.

Signed-off-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
(cherry picked from commit 893e2613ddb928aacfdbae35d96c105ecf18bf82)
---

FYI, I modified the backport to drop the unsupported test for gcc-14
since it seemed like overkill to engineer the test case to be backport
safe instead (and then backport that fix all the way).  I'll push this
next week if there are no objections.

Thanks,
Sid

 .../builtin-dynamic-object-size-pr127899-2.c  | 30 +++++++++++++++++
 .../builtin-dynamic-object-size-pr127899-3.c  | 15 +++++++++
 gcc/tree-object-size.cc                       | 32 ++++++++++++++++---
 3 files changed, 72 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr127899-2.c
 create mode 100644 gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr127899-3.c
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr127899-2.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr127899-2.c
new file mode 100644
index 00000000000..7f05e1ee826
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr127899-2.c
@@ -0,0 +1,30 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+typedef __SIZE_TYPE__ size_t;
+static char pool[64];
+static size_t used;
+
+/* Hand out consecutive pieces of POOL.  */
+__attribute__ ((alloc_size (1), noinline)) char *
+pool_alloc (size_t n)
+{
+  char *p = pool + used;
+
+  used += n;
+  return p;
+}
+
+int
+main (int argc, char **argv)
+{
+  (void) argv;
+  size_t n = argc + 7;
+  char *a = pool_alloc (8);
+  char *b = pool_alloc (n);
+  /* A again: B - 8 is &pool[0].  */
+  char *before = b - 8;
+
+  __builtin_printf ("%d %zu\n", before == a, __builtin_dynamic_object_size (before, 2));
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr127899-3.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr127899-3.c
new file mode 100644
index 00000000000..397b3df3f8d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-pr127899-3.c
@@ -0,0 +1,15 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+__SIZE_TYPE__ h (int n)
+{
+  char *q = __builtin_malloc (n);
+  return __builtin_dynamic_object_size (q - 8, 2);
+}
+
+extern char a[10], b[20];
+__SIZE_TYPE__ k (int c)
+{
+  char *q = c ? a : b;
+  return __builtin_dynamic_object_size (q - 8, 2);
+}
diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc
index 24e7d710371..4242883664f 100644
--- a/gcc/tree-object-size.cc
+++ b/gcc/tree-object-size.cc
@@ -1473,6 +1473,15 @@  plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt)
 	bytes = size_unknown (object_size_type);
       else
 	bytes = wholesize;
+
+      /* If the size ends up being unknown (either intentionally or because
+	 SIZE_FOR_OFFSET returned a zero size, which happens to be the unknown
+	 value for OST_MINIMUM), reset wholesize too.  */
+      if (size_unknown_p (bytes, object_size_type))
+	{
+	  wholesize = size_unknown (object_size_type);
+	  reexamine = false;
+	}
     }
   else
     bytes = wholesize = size_unknown (object_size_type);
@@ -1489,11 +1498,12 @@  plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt)
 /* Compute the dynamic object size for VAR.  Return the result in SIZE and
    WHOLESIZE.  */
 
-static void
+static bool
 dynamic_object_size (struct object_size_info *osi, tree var,
 		     tree *size, tree *wholesize)
 {
   int object_size_type = osi->object_size_type;
+  bool reexamine = false;
 
   if (TREE_CODE (var) == SSA_NAME)
     {
@@ -1502,11 +1512,14 @@  dynamic_object_size (struct object_size_info *osi, tree var,
       collect_object_sizes_for (osi, var);
       *size = object_sizes_get (osi, varno);
       *wholesize = object_sizes_get (osi, varno, true);
+      reexamine = bitmap_bit_p (osi->reexamine, varno);
     }
   else if (TREE_CODE (var) == ADDR_EXPR)
     addr_object_size (osi, var, object_size_type, size, wholesize);
   else
     *size = *wholesize = size_unknown (object_size_type);
+
+  return reexamine;
 }
 
 /* Compute object_sizes for VAR, defined at STMT, which is
@@ -1533,9 +1546,11 @@  cond_expr_object_size (struct object_size_info *osi, tree var, gimple *stmt)
     {
       tree then_size, then_wholesize, else_size, else_wholesize;
 
-      dynamic_object_size (osi, then_, &then_size, &then_wholesize);
+      reexamine |= dynamic_object_size (osi, then_, &then_size,
+					&then_wholesize);
       if (!size_unknown_p (then_size, object_size_type))
-	dynamic_object_size (osi, else_, &else_size, &else_wholesize);
+	reexamine |= dynamic_object_size (osi, else_, &else_size,
+					  &else_wholesize);
 
       tree cond_size, cond_wholesize;
       if (size_unknown_p (then_size, object_size_type)
@@ -1553,7 +1568,7 @@  cond_expr_object_size (struct object_size_info *osi, tree var, gimple *stmt)
 
       object_sizes_set (osi, varno, cond_size, cond_wholesize);
 
-      return false;
+      return reexamine;
     }
 
   if (TREE_CODE (then_) == SSA_NAME)
@@ -1850,8 +1865,15 @@  collect_object_sizes_for (struct object_size_info *osi, tree var)
       bitmap_set_bit (computed[object_size_type], varno);
       if (!(object_size_type & OST_DYNAMIC))
 	bitmap_clear_bit (osi->reexamine, varno);
+      /* The REEXAMINE flag for dynamic size expressions needs to propagate so
+	 that there are no stale references to SSA names released during
+	 unknown size propagation in gimplify_size_expressions.  */
       else if (reexamine)
-	bitmap_set_bit (osi->reexamine, varno);
+	{
+	  gcc_checking_assert (!object_sizes_unknown_p (object_size_type,
+							varno));
+	  bitmap_set_bit (osi->reexamine, varno);
+	}
     }
   else
     {