public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-4600] tree-optimization/89317 - missed folding of (p + 4) - &p->d
Date: Sun, 11 Dec 2022 13:46:03 +0000 (GMT)	[thread overview]
Message-ID: <20221211134603.3B5BF396C5D1@sourceware.org> (raw)

https://gcc.gnu.org/g:d13b86f932ff7b9d8f41483fd869c637b67d4dec

commit r13-4600-gd13b86f932ff7b9d8f41483fd869c637b67d4dec
Author: Richard Biener <rguenther@suse.de>
Date:   Sun Dec 11 14:07:34 2022 +0100

    tree-optimization/89317 - missed folding of (p + 4) - &p->d
    
    The PR notices we fail to simplify
    
      a_4 = &x_3(D)->data;
      b_5 = x_3(D) + 16;
      _1 = b_5 - a_4;
    
    together with the enabler handling ADDR_EXPR leafs in separate
    stmts in match.pd the suggested patterns work.
    
            PR tree-optimization/89317
            * match.pd ((p + b) - &p->c -> b - offsetof(c)): New patterns.
    
            * gcc.dg/tree-ssa/pr89317.c: New testcase.

Diff:
---
 gcc/match.pd                            | 11 +++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr89317.c | 13 +++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 046ea2cdef4..ba83c69734f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2451,6 +2451,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (with { poly_int64 diff; }
    (if (ptr_difference_const (@0, @2, &diff))
     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
+/* (p + b) - &p->d -> offsetof (*p, d) + b */
+(simplify
+ (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
+ (with { poly_int64 diff; }
+   (if (ptr_difference_const (@0, @2, &diff))
+    (plus { build_int_cst_type (type, diff); } (convert @1)))))
+(simplify
+ (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
+ (with { poly_int64 diff; }
+   (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].  */
 (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr89317.c b/gcc/testsuite/gcc.dg/tree-ssa/pr89317.c
new file mode 100644
index 00000000000..0d0a049454f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr89317.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-ccp1" } */
+
+struct b { int data[16]; };
+
+int foo (struct b *x)
+{
+  int *a = x->data;
+  int *b = ((int*)x) + 4;
+  return b - a;
+}
+
+/* { dg-final { scan-tree-dump "return 4;" "ccp1" } } */

                 reply	other threads:[~2022-12-11 13:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221211134603.3B5BF396C5D1@sourceware.org \
    --to=rguenth@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).