From patchwork Thu Dec 8 10:54:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 61691 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2E65D3943403 for ; Thu, 8 Dec 2022 10:54:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E65D3943403 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670496874; bh=oGzEKt9gHiI9YinznUWGlpct3znmKZPujo4iuW+4xRk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VGQtDP9yyHUXPkMXBPz0lwdl57FOPe3gKN4MD4GejIOJwhMfKZkIyqU+155+8q7kE 9SCCG4FW5M8W5UyBujF72S/+AY6GU2wyS3QFNbfsxFNMvHTCPYF1yJLwTqby1MNV4t isD7gHTAorcsMeYNlf04BRhA1FOwQl/BWn5k/XZs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 0F0D9392B150 for ; Thu, 8 Dec 2022 10:54:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0F0D9392B150 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id C6379337A8 for ; Thu, 8 Dec 2022 10:54:04 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 91E9A2C142 for ; Thu, 8 Dec 2022 10:54:02 +0000 (UTC) Date: Thu, 8 Dec 2022 10:54:04 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/107699 - missed &data._M_elems + _1 != &data._M_elems folding Message-ID: User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The following addresses a missed folding noticed in PR107699 that can be fixed amending the existing &x + a != &x + b pattern to also handle the case of only one side having a pointer plus. I'm moving the patterns next to related simpifications showing there'd be an existing pattern matching this if it were not gated with an explicit single_use constraint. Note the new pattern also handles &x.a + a != &x.b, but this hints at some unification / generalization opportunities here. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/107699 * match.pd (&a !=/== &a.b + c -> (&a - &a.b) !=/== c): New pattern variant. * gcc.dg/tree-ssa/pr107699.c: New testcase. --- gcc/match.pd | 21 +++++++++++++-------- gcc/testsuite/gcc.dg/tree-ssa/pr107699.c | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr107699.c diff --git a/gcc/match.pd b/gcc/match.pd index f48cbd9b73b..127cef9a610 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2260,6 +2260,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3)))) (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) +/* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */ +(for neeq (ne eq) + (simplify + (neeq:c ADDR_EXPR@0 (pointer_plus ADDR_EXPR@2 @3)) + (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);} + (if (ptr_difference_const (@0, @2, &diff)) + (neeq { build_int_cst_type (inner_type, diff); } @3)))) + (simplify + (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3)) + (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);} + (if (ptr_difference_const (@0, @2, &diff)) + (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3))))) + /* X - Y < X is the same as Y > 0 when there is no overflow. For equality, this is also true with wrapping overflow. */ (for op (simple_comparison) @@ -2439,14 +2452,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (ptr_difference_const (@0, @2, &diff)) (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3)))))) -/* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */ -(for neeq (ne eq) - (simplify - (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3)) - (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);} - (if (ptr_difference_const (@0, @2, &diff)) - (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3))))) - /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */ (simplify (convert (pointer_diff @0 INTEGER_CST@1)) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr107699.c b/gcc/testsuite/gcc.dg/tree-ssa/pr107699.c new file mode 100644 index 00000000000..4bf864dfd72 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr107699.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-forwprop1" } */ + +struct { int data[16]; } x; + +int foo (int n) +{ + int *p = x.data + n; + /* Should simplify this to n * 4 != 0. */ + if ((void *)&x != (void *)p) + return 1; + return 0; +} + +/* { dg-final { scan-tree-dump " != 0" "forwprop1" } } */