On Wed, Mar 16, 2022 at 08:24:24PM +0530, Siddhesh Poyarekar wrote:
> Assign the result of fold_convert to offset.
>
> gcc/ChangeLog:
>
> PR tree-optimization/104941
> * tree-object-size.cc (size_for_offset): 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 <[email protected]>
> --- a/gcc/tree-object-size.cc
> +++ b/gcc/tree-object-size.cc
> @@ -372,7 +372,7 @@ 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);
> + offset = fold_convert (sizetype, offset);
>
> if (TREE_CODE (offset) == INTEGER_CST)
> {
I think the types_compatible_p call is too heavy, you want to check if
emitting the fold_convert to sizetype is unnecessary, which is
if (!useless_type_conversion_p (sizetype, TREE_TYPE (offset)))
offset = fold_convert (sizetype, offset);
types_compatible_p is that the conversion is useless in both directions.
Ok for trunk with that change.
Jakub