From patchwork Sun Dec 11 13:32:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 32189 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1769551wrr; Sun, 11 Dec 2022 05:34:42 -0800 (PST) X-Google-Smtp-Source: AA0mqf457+tEIO2jiOvAnTySGzHwlbSJ+og3IWt8S6V2HniL5RANEFBrPbBUS7VupcQQSFfiL+WI X-Received: by 2002:a17:906:2297:b0:7c1:6344:840 with SMTP id p23-20020a170906229700b007c163440840mr2524948eja.24.1670765682705; Sun, 11 Dec 2022 05:34:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670765682; cv=none; d=google.com; s=arc-20160816; b=T+dRw6g7/WJ3DlBmHUlPV5EpXXSl1xKm+aYCe7l56Z0MzMqvBjkDgNYoViNpqksbeB j1/QuhyZr0w1x3dUGl79H1m0fdQaf9AWI5qV98K/80hcXKdT+YTZvNYtUWp96uARJ1cw pAH0xptYj/9ioPaic1hcARMez12qOw1nY26E31JlAUa2ZtGCA5zcgNigCP/xOFKRdFjm oBs9G+41VWy0Og98hthuHd0VsF+g+XgqLj2z+WQAHdcJBpLvtdZoamiYUlRXj3mKIDqp 4VKmIlTDDqTeaz0FMQgs/2XO9psQd01nn1fCz2l3POMzzgc6VwCV8+IR86cc6NlTHGz+ euMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=h2Y7FsifJ4XrzeKFt93dtMtbK7hYwKN8PmrQTzG+adI=; b=u/EqWmzjS1keN/JnONoEoxrUf2V5R1P694x/tX1S1B2kZBcuKX566+V8e9SHEsCDeg eaUyOUfuKSWngFX7LqL6BoApVntHfBc7gtv8FW6EZ0pyEBO6ZgxUR3vkhi1iFkT9LqBc RlPbeRHXsClYLcjKdFLuF1eQHz32en6poBF8GweHZ1rYNY2Hp5oZ+VYgeVLLrQ+c/T5d xYLdE3E3/l4u0dHMEfu4sDrUjgHXgDqdy4+vzDYJ4MNeQ1pwHpaSX8p9kgU3oxCc2TPn yHNFz+2w9KYYHj7kFDdX+eNR9/9LmaD2YKxlyx34yC7wYrb5CC9ektvUihZumy7YFlJj 4D+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Dj+7Sndo; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hq20-20020a1709073f1400b007c0dcc79ec1si4702609ejc.164.2022.12.11.05.34.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Dec 2022 05:34:42 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Dj+7Sndo; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8421E3952517 for ; Sun, 11 Dec 2022 13:33:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8421E3952517 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670765604; bh=h2Y7FsifJ4XrzeKFt93dtMtbK7hYwKN8PmrQTzG+adI=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Dj+7SndokNZ21yXeK3oOpxjsTvcbLO9VCijKl0IZaXHOqD9YEDrV44LSjnlDB6N4z 859MgvERbNhXW91OzCvZEMhDH68CmOo14ufgHws3MqpOY7IEm8l7ZhE+KVJHm3zvzk f286IV5FGmvdbM7zaKuXLH4BUZp0LS2YpCexawzY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 95D3A395252F for ; Sun, 11 Dec 2022 13:32:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 95D3A395252F Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 800571FE44 for ; Sun, 11 Dec 2022 13:32:34 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 64ABB13413 for ; Sun, 11 Dec 2022 13:32:34 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id uWOZF/LblWOZKAAAMHmgww (envelope-from ) for ; Sun, 11 Dec 2022 13:32:34 +0000 Date: Sun, 11 Dec 2022 14:32:34 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] Treat ADDR_EXPR and CONSTRUCTOR as GIMPLE/GENERIC magically MIME-Version: 1.0 Message-Id: <20221211133234.64ABB13413@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751924796778607878?= X-GMAIL-MSGID: =?utf-8?q?1751924796778607878?= The following allows to match ADDR_EXPR for both the invariant &a.b case as well as the &p->d case in a separate definition transparently. This also allows to remove the hack we employ for CONSTRUCTOR which we handle for example with (match vec_same_elem_p CONSTRUCTOR@0 (if (TREE_CODE (@0) == SSA_NAME && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)))))) Note CONSTUCTORs always appear as separate definition in GIMPLE, but I continue to play safe and ADDR_EXPRs are now matched in both places where previously ADDR_EXPR@0 would have missed the &p->x case. This is a prerequesite for the PR89317 fix. Bootstrapped and tested on x86_64-unknown-linux-gnu, will push after testing of the 2nd patch succeeded. * genmatch.cc (dt_node::gen_kids): Handle ADDR_EXPR in both the GENERIC and GIMPLE op position. (dt_simplify::gen): Capture both GENERIC and GIMPLE op position for ADDR_EXPR and CONSTRUCTOR. * match.pd: Simplify CONSTRUCTOR leaf handling. * gcc.dg/tree-ssa/forwprop-3.c: Adjust. * g++.dg/tree-ssa/pr31146-2.C: Likewise. --- gcc/genmatch.cc | 30 ++++++++++++++++++---- gcc/match.pd | 12 +++------ gcc/testsuite/g++.dg/tree-ssa/pr31146-2.C | 2 +- gcc/testsuite/gcc.dg/tree-ssa/forwprop-3.c | 2 +- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc index 4a8802469cd..f53e61a4a8d 100644 --- a/gcc/genmatch.cc +++ b/gcc/genmatch.cc @@ -2927,8 +2927,16 @@ dt_node::gen_kids (FILE *f, int indent, bool gimple, int depth) if (expr *e = dyn_cast (op->op)) { if (e->ops.length () == 0 + /* In GIMPLE a CONSTRUCTOR always appears in a + separate definition. */ && (!gimple || !(*e->operation == CONSTRUCTOR))) - generic_exprs.safe_push (op); + { + generic_exprs.safe_push (op); + /* But ADDR_EXPRs can appear directly when invariant + and in a separate definition when not. */ + if (gimple && *e->operation == ADDR_EXPR) + gimple_exprs.safe_push (op); + } else if (e->operation->kind == id_base::FN) { if (gimple) @@ -3599,14 +3607,26 @@ dt_simplify::gen (FILE *f, int indent, bool gimple, int depth ATTRIBUTE_UNUSED) if (s->capture_max >= 0) { char opname[20]; - fprintf_indent (f, indent, "tree captures[%u] ATTRIBUTE_UNUSED = { %s", - s->capture_max + 1, indexes[0]->get_name (opname)); + fprintf_indent (f, indent, "tree captures[%u] ATTRIBUTE_UNUSED = {", + s->capture_max + 1); - for (int i = 1; i <= s->capture_max; ++i) + for (int i = 0; i <= s->capture_max; ++i) { if (!indexes[i]) break; - fprintf (f, ", %s", indexes[i]->get_name (opname)); + const char *opstr = indexes[i]->get_name (opname); + expr *e = dyn_cast (indexes[i]->op); + fputs (i == 0 ? " " : ", ", f); + if (e && gimple + /* Transparently handle picking up CONSTRUCTOR and ADDR_EXPR + leafs if they appear in a separate definition. */ + && (*e->operation == CONSTRUCTOR + || *e->operation == ADDR_EXPR)) + fprintf (f, "(TREE_CODE (%s) == SSA_NAME " + "? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (%s)) : %s)", + opstr, opstr, opstr); + else + fprintf (f, "%s", opstr); } fprintf (f, " };\n"); } diff --git a/gcc/match.pd b/gcc/match.pd index 127cef9a610..046ea2cdef4 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3925,8 +3925,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (view_convert CONSTRUCTOR@0) (with - { tree ctor = (TREE_CODE (@0) == SSA_NAME - ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); } + { tree ctor = @0; } (switch (if (CONSTRUCTOR_NELTS (ctor) == 0) { build_zero_cst (type); }) @@ -7292,8 +7291,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))))) (with { - tree ctor = (TREE_CODE (@0) == SSA_NAME - ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); + tree ctor = @0; tree eltype = TREE_TYPE (TREE_TYPE (ctor)); unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype)); unsigned HOST_WIDE_INT n = tree_to_uhwi (@1); @@ -7941,8 +7939,7 @@ and, /* Fold reduction of a single nonzero element constructor. */ (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR) (simplify (reduc (CONSTRUCTOR@0)) - (with { tree ctor = (TREE_CODE (@0) == SSA_NAME - ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); + (with { tree ctor = @0; tree elt = ctor_single_nonzero_element (ctor); } (if (elt && !HONOR_SNANS (type) @@ -8161,8 +8158,7 @@ and, (match vec_same_elem_p CONSTRUCTOR@0 - (if (TREE_CODE (@0) == SSA_NAME - && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)))))) + (if (uniform_vector_p (@0)))) (match vec_same_elem_p @0 diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr31146-2.C b/gcc/testsuite/g++.dg/tree-ssa/pr31146-2.C index fc268578f69..10232855e26 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/pr31146-2.C +++ b/gcc/testsuite/g++.dg/tree-ssa/pr31146-2.C @@ -21,4 +21,4 @@ double foo (void) } /* GCC 8 emits operator new () != NULL with -fcheck-new. */ -/* { dg-final { scan-tree-dump "Replaced .* != 0B. with .1" "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times " != 0B\\)" 1 "forwprop1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-3.c b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-3.c index 4aa68c64950..bfdfba1800c 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-3.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-3.c @@ -14,4 +14,4 @@ int foo(struct bar *x) return 0; } -/* { dg-final { scan-tree-dump "Replaced .p_. < q_.. with .1." "forwprop1" } } */ +/* { dg-final { scan-tree-dump "return 1;" "forwprop1" } } */ From patchwork Sun Dec 11 13:32:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 32190 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1770034wrr; Sun, 11 Dec 2022 05:36:23 -0800 (PST) X-Google-Smtp-Source: AA0mqf459tzneli04lRbXoXF0QI1FID5HtstAHO478aNirVvnLQtRoxWMtF13VAydrR2CnWkzTNy X-Received: by 2002:aa7:c9c7:0:b0:461:2030:71ce with SMTP id i7-20020aa7c9c7000000b00461203071cemr10651516edt.30.1670765783383; Sun, 11 Dec 2022 05:36:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670765783; cv=none; d=google.com; s=arc-20160816; b=ymp4nc+YoUaQdanY80G4PVq0ayvnNAuVBzrbWZOoR9fyebeIl6RgzT91iOFKpRPqKm UK81bxPlcuzGp122FwYjIAbuukeIoaP5qVJt9TUc8hUgprqzr3sa5wvqfsRE/pl914cB eEk7pN+JfhLAWqZVAai+lY87jN2uI5ynnt5GSUChR8OsM6Ih+wS09d9vTox5WMz53hC+ 6szFGkqJNZSw4wnF4zWQuOm26kMDjIOYzKaUMgChsLhtdNMQhyPsedSFXvt0oXE6wICv t7cVx807DvNYe+XOn5eM9fBwT26zzRab2fsuunDMfY4ssOQcT8ZzOfTbYq80BgeQbPMP KVoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=68OOqcjtfJ6LzaKnyw+vJVzCixre0vX5PXlbbUItJP8=; b=Gl+ju948r1oPqQNvrUg6oI+NEnGl0RoNtNQzZ5xAiOUyCnNuxmW3mou8tjUJpvduNk RWAbUeFBmHj4SGdkupgiWTI7v0LbI2AIZIaW5ZX0UlICC7Is2SqDHpBIADh+EEC/QaBS RMp9qSB9Ey6H3w22oYk1R1SNO83aXRjtgIOWqQYlWkmDgil6T8d9Z5ARpp//4N5urBf8 iS9ozymBdMaQMhwygDlE1j+ZMBWSbnmX0zbnwhRhphX3ChpNuna5k99pbFaoNUxEUhJn pRdRb/1E54+8lJxWdAuDI03CIpkuWRnG9Uocd6Hi1xmSjIOU/p8/L+IrdEsNp7uKf+no xFKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ne10+1Xa; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c15-20020a05640227cf00b0046a279823d6si6533932ede.298.2022.12.11.05.36.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Dec 2022 05:36:23 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ne10+1Xa; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 18E14384E200 for ; Sun, 11 Dec 2022 13:34:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18E14384E200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670765661; bh=68OOqcjtfJ6LzaKnyw+vJVzCixre0vX5PXlbbUItJP8=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ne10+1XadMhrK1GWaooV2qNaM29b1sPcANNw8nXTbLh/UH05rqdepsdluRctJdaxq tE6J8a2mkSQ7r8DJY0X8g8B1+yWWv5/JHjWZcW6kuevq4BNw0rjmfCnsMtWnlolcU/ n7fBbnY8QYPw/w/193dn79V4c/uUrbnLCz8uQ+sk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 0239E3953831 for ; Sun, 11 Dec 2022 13:32:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0239E3953831 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2EEF31FE44 for ; Sun, 11 Dec 2022 13:32:52 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1DDD913413 for ; Sun, 11 Dec 2022 13:32:52 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id ymBVBgTclWO1KAAAMHmgww (envelope-from ) for ; Sun, 11 Dec 2022 13:32:52 +0000 Date: Sun, 11 Dec 2022 14:32:51 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] tree-optimization/89317 - missed folding of (p + 4) - &p->d MIME-Version: 1.0 Message-Id: <20221211133252.1DDD913413@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751924902082568401?= X-GMAIL-MSGID: =?utf-8?q?1751924902082568401?= 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. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. PR tree-optimization/89317 * match.pd ((p + b) - &p->c -> b - offsetof(c)): New patterns. * gcc.dg/tree-ssa/pr89317.c: New testcase. --- gcc/match.pd | 11 +++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr89317.c | 13 +++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr89317.c 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" } } */