On Sun, Aug 31, 2025 at 12:29 AM Andrew Pinski
<[email protected]> wrote:
>
> Inside split_address_to_core_and_offset, this calls get_inner_reference.
>
> Take:
> ```
> _6 = t_3(D) + 12;
> _8 = &MEM[(struct s1 *)t_3(D) + 4B].t;
> _1 = _6 - _8;
> ```
>
> On the assignement of _8, get_inner_reference will return `MEM[(struct s1
> *)t_3(D) + 4B]`
> and an offset but that does not match up with `t_3(D)` which is how
> split_address_to_core_and_offset
> handles pointer plus.
> So this patch adds the unwrapping of the MEM_REF after the call to
> get_inner_reference
> and have it act like a pointer plus.
>
> Bootstrapped and tested on x86_64-linux-gnu.
>
> PR tree-optimization/121355
>
> gcc/ChangeLog:
>
> * fold-const.cc (split_address_to_core_and_offset): Handle an MEM_REF
> after the call
> to get_inner_reference.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/ptrdiff-1.c: New test.
>
> Signed-off-by: Andrew Pinski <[email protected]>
> ---
> gcc/fold-const.cc | 18 +++++++++
> gcc/testsuite/gcc.dg/tree-ssa/ptrdiff-1.c | 45 +++++++++++++++++++++++
> 2 files changed, 63 insertions(+)
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ptrdiff-1.c
>
> diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
> index 8867540243b..d26d4adce97 100644
> --- a/gcc/fold-const.cc
> +++ b/gcc/fold-const.cc
> @@ -16514,6 +16514,24 @@ split_address_to_core_and_offset (tree exp,
> core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
> poffset, &mode, &unsignedp, &reversep,
> &volatilep);
> + /* If we are left with MEM[a + CST] strip that and add it to the
> pbitpos and return a. */
> + if (TREE_CODE (core) == MEM_REF)
> + {
> + tree offset = TREE_OPERAND (core, 1);
> + if (poly_int_tree_p (offset))
offset is always a constant, you should be able to use
wi::to_poly_offset (TREE_OPERAND (core, 1)), that should
properly extend as well.
> + {
> + poly_offset_int tem
> + = wi::sext (wi::to_poly_offset (offset),
> + TYPE_PRECISION (TREE_TYPE (offset)));
> + tem <<= LOG2_BITS_PER_UNIT;
> + poly_int64 pbit;
> + if (tem.to_shwi (&pbit))
> + {
> + *pbitpos += pbit;
So instead convert *pbitpos to poly_offset, add and check whether
the result fits in *pbitpos.
> + return TREE_OPERAND (core, 0);
> + }
> + }
> + }
> core = build_fold_addr_expr_loc (loc, core);
> }
> else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ptrdiff-1.c
> b/gcc/testsuite/gcc.dg/tree-ssa/ptrdiff-1.c
> new file mode 100644
> index 00000000000..af9291c6608
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/ptrdiff-1.c
> @@ -0,0 +1,45 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized" } */
> +
> +/* PR tree-optimization/121355 */
> +
> +#define array_size 2
> +struct s1
> +{
> + int t[array_size];
> +};
> +
> +struct s2
> +{
> + int p;
> + struct s1 t;
> +};
> +static inline int *b(struct s1 *t)
> +{
> + return t->t;
> +}
> +static inline int *e(struct s1 *t)
> +{
> + return b(t) + array_size;
> +}
> +void g(struct s2 *t)
> +{
> + struct s1 *t2 = &t->t;
> + int *te = e(t2);
> + int *ts = b(t2);
> + int tt = te - ts;
> +/*
> + _6 = t_3(D) + 12;
> + _8 = &MEM[(struct s1 *)t_3(D) + 4B].t;
> + _1 = _6 - _8;
> +
> + _1 should be optimized to 2*sizeof(int) == 8.
> + */
> +
> + if (tt != array_size)
> + __builtin_abort();
> +}
> +
> +/* the call to abort should be removed. */
> +
> +/* { dg-final { scan-tree-dump-not "abort " "optimized" } } */
> --
> 2.43.0
>