From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2206) id CD8673858D1E; Wed, 16 Mar 2022 15:35:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD8673858D1E MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Siddhesh Poyarekar To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-7675] tree-optimization/104941: Actually assign the conversion result X-Act-Checkin: gcc X-Git-Author: Siddhesh Poyarekar X-Git-Refname: refs/heads/master X-Git-Oldrev: be093b8dcc9cf5c9f99479ec68f1abe95aa38a83 X-Git-Newrev: beb12c62eaec37ed0ee5a251ca0907d965d413b2 Message-Id: <20220316153543.CD8673858D1E@sourceware.org> Date: Wed, 16 Mar 2022 15:35:43 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Mar 2022 15:35:43 -0000 https://gcc.gnu.org/g:beb12c62eaec37ed0ee5a251ca0907d965d413b2 commit r12-7675-gbeb12c62eaec37ed0ee5a251ca0907d965d413b2 Author: Siddhesh Poyarekar Date: Wed Mar 16 20:45:47 2022 +0530 tree-optimization/104941: Actually assign the conversion result Assign the result of fold_convert to offset. Also make the useless conversion check lighter since the two way check is not needed here. gcc/ChangeLog: PR tree-optimization/104941 * tree-object-size.cc (size_for_offset): Make useless conversion check lighter and assign result of fold_convert to OFFSET. gcc/testsuite/ChangeLog: PR tree-optimization/104941 * gcc.dg/builtin-dynamic-object-size-0.c (S1, S2): New structs. (test_alloc_nested_structs, g): New functions. (main): Call test_alloc_nested_structs. Signed-off-by: Siddhesh Poyarekar Diff: --- .../gcc.dg/builtin-dynamic-object-size-0.c | 34 ++++++++++++++++++++++ gcc/tree-object-size.cc | 4 +-- 2 files changed, 36 insertions(+), 2 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 2fca0a9c5b4..e5dc23a908d 100644 --- a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c +++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c @@ -323,6 +323,34 @@ test_substring (size_t sz, size_t off) return __builtin_dynamic_object_size (&str[off], 0); } +struct S2 +{ + char arr[7]; +}; + +struct S1 +{ + int pad; + struct S2 s2; +}; + +static long +g (struct S1 *s1) +{ + struct S2 *s2 = &s1->s2; + return __builtin_dynamic_object_size (s2->arr, 0); +} + +long +__attribute__ ((noinline)) +test_alloc_nested_structs (int x) +{ + struct S1 *s1 = __builtin_malloc (x); + return g (s1); +} + +/* POINTER_PLUS expressions. */ + size_t __attribute__ ((noinline)) test_substring_ptrplus (size_t sz, size_t off) @@ -342,6 +370,8 @@ test_substring_ptrplus2 (size_t sz, size_t off, size_t off2) return __builtin_dynamic_object_size (ptr + off2, 0); } +/* Function parameters. */ + size_t __attribute__ ((access (__read_write__, 1, 2))) __attribute__ ((noinline)) @@ -382,6 +412,8 @@ test_parmsz_unknown (void *obj, void *unknown, size_t sz, int cond) return __builtin_dynamic_object_size (cond ? obj : unknown, 0); } +/* Loops. */ + size_t __attribute__ ((noinline)) __attribute__ ((access (__read_write__, 1, 2))) @@ -491,6 +523,8 @@ main (int argc, char **argv) FAIL (); if (test_dynarray_cond (1) != 8) FAIL (); + if (test_alloc_nested_structs (42) != 42 - sizeof (int)) + FAIL (); if (test_deploop (128, 4) != 128) FAIL (); if (test_deploop (128, 129) != 32) diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 9728f79da75..b0b50774936 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -371,8 +371,8 @@ size_for_offset (tree sz, tree offset, tree wholesize = NULL_TREE) } /* Safe to convert now, since a valid net offset should be non-negative. */ - if (!types_compatible_p (TREE_TYPE (offset), sizetype)) - fold_convert (sizetype, offset); + if (!useless_type_conversion_p (sizetype, TREE_TYPE (offset))) + offset = fold_convert (sizetype, offset); if (TREE_CODE (offset) == INTEGER_CST) {