https://gcc.gnu.org/g:004b053c4b60bdbf946168773d15f5971a28cd1b

commit r16-9667-g004b053c4b60bdbf946168773d15f5971a28cd1b
Author: Richard Biener <[email protected]>
Date:   Mon Aug 24 15:28:42 2026 +0200

    tree-optimization/127000 - revise (T *)(ptr - ptr_cst) pattern
    
    The following revises the earlier fix which was avoiding
    build_fold_addr_expr_with_type.  Instead the following applies
    the GENERIC simplification that this function should have done
    to the pattern directly, canonicalizing (T *)(ptr - ptr_cst)
    to (T *)ptr p+ cst.  The pattern was originally added with
    r10-7127-gcb99630f254aae.
    
            PR tree-optimization/127000
            * match.pd ((T *)(ptr - ptr_cst)): Canonicalize directly
            to (T *)ptr p+ cst.
    
    (cherry picked from commit c5d24759ba5e470f6fb75e251b84ec8fcb33db07)

Diff:
---
 gcc/match.pd | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 933f66125ad7..e9132e857168 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3259,14 +3259,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (if (ptr_difference_const (@0, @1, &diff))
     (minus { build_int_cst_type (type, diff); } (convert @2)))))
 
-/* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
+/* Canonicalize (T *)(ptr - ptr-cst) to (T *)ptr + -ptr-cst.  */
 (simplify
  (convert (pointer_diff @0 INTEGER_CST@1))
  (if (POINTER_TYPE_P (type))
-  { build1 (ADDR_EXPR, type,
-           build2 (MEM_REF, char_type_node, @0,
-                   wide_int_to_tree (ptr_type_node,
-                                     wi::neg (wi::to_wide (@1))))); }))
+  (pointer_plus
+   (convert @0) { wide_int_to_tree (sizetype,
+                                   wi::neg (wi::to_wide (@1))); })))
 
 /* If arg0 is derived from the address of an object or function, we may
    be able to fold this expression using the object or function's

Reply via email to