aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@gotplt.org>2023-01-24 19:47:05 -0500
committerSiddhesh Poyarekar <siddhesh@gotplt.org>2023-01-24 19:47:05 -0500
commitb851ee9fdf0f3023635f0cb1f7c607b2d6801053 (patch)
tree8e81c1f792baf54fb1043f359ef2ed679b41e58e
parent0fa221685a36ef98cb20a6d435a150b5992e99e0 (diff)
downloadgcc-b851ee9fdf0f3023635f0cb1f7c607b2d6801053.zip
gcc-b851ee9fdf0f3023635f0cb1f7c607b2d6801053.tar.gz
gcc-b851ee9fdf0f3023635f0cb1f7c607b2d6801053.tar.bz2
tree-optimization/108522 Use COMPONENT_REF offset when available
Use the offset in TREE_OPERAND(component_ref, 2) when available instead of DECL_FIELD_OFFSET when trying to compute offset for a COMPONENT_REF. Co-authored-by: Jakub Jelinek <jakub@redhat.com> gcc/ChangeLog: PR tree-optimization/108522 * tree-object-size.cc (compute_object_offset): Use TREE_OPERAND(ref, 2) for COMPONENT_REF when available. gcc/testsuite/ChangeLog: PR tree-optimization/108522 * gcc.dg/builtin-dynamic-object-size-0.c (test_dynarray_struct_member): New test. (main): Call it. Signed-off-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
-rw-r--r--gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c16
-rw-r--r--gcc/tree-object-size.cc4
2 files changed, 19 insertions, 1 deletions
diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
index f9047a0..569c0a8 100644
--- a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
+++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
@@ -314,6 +314,20 @@ test_dynarray_struct_subobj2 (size_t sz, size_t off, size_t *objsz)
return __builtin_dynamic_object_size (&bin.c[off], 1);
}
+/* See pr #108522. */
+size_t
+__attribute__ ((noinline))
+test_dynarray_struct_member (size_t sz)
+{
+ struct
+ {
+ char a[sz];
+ char b;
+ } s;
+
+ return __builtin_dynamic_object_size (&s.b, 0);
+}
+
size_t
__attribute__ ((noinline))
test_substring (size_t sz, size_t off)
@@ -619,6 +633,8 @@ main (int argc, char **argv)
if (test_dynarray_struct_subobj2 (42, 4, &objsz)
!= objsz - 4 - sizeof (long) - sizeof (int))
FAIL ();
+ if (test_dynarray_struct_member (42) != sizeof (char))
+ FAIL ();
if (test_substring_ptrplus (128, 4) != (128 - 4) * sizeof (int))
FAIL ();
if (test_substring_ptrplus (128, 142) != 0)
diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc
index 356591c..de93ffa 100644
--- a/gcc/tree-object-size.cc
+++ b/gcc/tree-object-size.cc
@@ -412,7 +412,9 @@ compute_object_offset (const_tree expr, const_tree var)
return base;
t = TREE_OPERAND (expr, 1);
- off = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (t),
+ off = size_binop (PLUS_EXPR,
+ (TREE_OPERAND (expr, 2) ? TREE_OPERAND (expr, 2)
+ : DECL_FIELD_OFFSET (t)),
size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
/ BITS_PER_UNIT));
break;