From patchwork Mon Oct 9 11:13:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 149951 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp1793007vqo; Mon, 9 Oct 2023 04:14:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEcRe/y3e5wAtn34EmL+lbiJYRPJebZRqeb7qw+z/bCcD8CK1HmuUs1fAELKAn1KpgGtJuR X-Received: by 2002:a17:906:99c5:b0:9ae:3768:f0ce with SMTP id s5-20020a17090699c500b009ae3768f0cemr10626185ejn.0.1696850049079; Mon, 09 Oct 2023 04:14:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696850049; cv=none; d=google.com; s=arc-20160816; b=dFEwovWp1eRolGWp+BBupH1viYEVgLm6KzSXJO4AfkM5EACp8oUhH6ZFErQ+re36q7 PT78O3gpMVyjjH4J/XnLhHVIXdO90AaBDQ+Fx198iwDfTu54/7uThGm8jgsGojFR+CWs 90IcKcPedQgQclRLdkS9zy532qY6PUwmTNyrV282PDxSLzqVNO1/QijXes+n7+2GDKqY /JfZrJWDOc7yRLFz2THPIbiQFEjaFebHheX0poAtcU9KN6QmO2b20Ndd5LqmZY0RjeH8 /yXhTnUzOpcnSR6AgBg0p5pDx2BIAgQxbJHkNP24rtU61YNyEvEeFhmvRxFP9nSGSPOA QjSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :user-agent:subject:cc:to:from:date:dkim-signature:dkim-signature :dmarc-filter:delivered-to; bh=JWiLMK2kCQHnMCPNuX9vmZxa/gfwoDZp13IgrGHYyhU=; fh=+H+PfslJQ8SfgojuI5pxEuPHAhp/Ml9gJNsp0+tS7IU=; b=IFsl/hQB9pVuLIF3fLyJ/dcq1xaXhgZAyoqoNZ1mFRq2a4zIgmrDH2fXd/P6rwtWDF 0beIDBZLwLT1djyEqq0AOC6NmcrAXdSMyqlB/FKDMIDkqcTZXJ1Lwsk3D0oZgY2/5jKQ /3vMpC4NAqIrQPt2gLupKMmPoDY7CMqBFPVF9u4Ln0lhPCFZTraIZc/MSnkR9mfDM0ct 6sH0srEPUxq0hageb3kzrGZdKoKyEl0IR9miQYQAdoHm2jXEbcjE3YwPyYpnCdYqeW3v cLf/hsu1cpStBHZatUtNBXD5EGAt6MVBbYK3uEFhcPir3B3psbaJeKHTozGjQ2ft1bep WK1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=xBfFq7Nl; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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=suse.de Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id g11-20020a170906594b00b00992cd4e0b83si4354391ejr.426.2023.10.09.04.14.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 04:14:09 -0700 (PDT) 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=@suse.de header.s=susede2_rsa header.b=xBfFq7Nl; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 221E8385B53E for ; Mon, 9 Oct 2023 11:14:03 +0000 (GMT) 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 [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 459393858D28 for ; Mon, 9 Oct 2023 11:13:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 459393858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 75F0021879; Mon, 9 Oct 2023 11:13:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1696850018; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=JWiLMK2kCQHnMCPNuX9vmZxa/gfwoDZp13IgrGHYyhU=; b=xBfFq7NlZ7Dr0QPkR7PpBSwB1QAJPPHAX58hLvKUeSC4ok7jPSS6Mw0+9zONA6e9e2+cEF Y2XIFKMVuztg6uwYXtIWy2f7L+2i+/HmbVMINyNyDpFFwBlrQLG2de4vbp+TNIZg2X+XdG WTpSokXTej+FqGHTCdfSWMNkzgfM9dk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1696850018; h=from:from:reply-to:date:date:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=JWiLMK2kCQHnMCPNuX9vmZxa/gfwoDZp13IgrGHYyhU=; b=qo9O1j5/VtgYNyxY8roxmak7NXOjRQ8/ktWPyXeS+kXCQyZt9lDERWU+xp9lEBxeWUktwz JSXKAErcU3yEEsAQ== 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 457D72C142; Mon, 9 Oct 2023 11:13:38 +0000 (UTC) Date: Mon, 9 Oct 2023 11:13:38 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: Jan Hubicka Subject: [PATCH] tree-optimization/111715 - improve TBAA for access paths with pun User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Message-Id: <20231009111403.221E8385B53E@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779276237400313841 X-GMAIL-MSGID: 1779276237400313841 The following improves basic TBAA for access paths formed by C++ abstraction where we are able to combine a path from an address-taking operation with a path based on that access using a pun to avoid memory access semantics on the address-taking part. The trick is to identify the point the semantic memory access path starts which allows us to use the alias set of the outermost access instead of only that of the base of this path. Bootstrapped and tested on x86_64-unknown-linux-gnu for all languages with a slightly different variant, re-bootstrapping/testing now (with doing the extra walk just for AGGREGATE_TYPE_P). PR tree-optimization/111715 * alias.cc (reference_alias_ptr_type_1): When we have a type-punning ref at the base search for the access path part that's still semantically valid. * gcc.dg/tree-ssa/ssa-fre-102.c: New testcase. --- gcc/alias.cc | 20 ++++++++++++- gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c | 32 +++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c diff --git a/gcc/alias.cc b/gcc/alias.cc index 7c1af1fe96e..4060ff72949 100644 --- a/gcc/alias.cc +++ b/gcc/alias.cc @@ -774,7 +774,25 @@ reference_alias_ptr_type_1 (tree *t) && (TYPE_MAIN_VARIANT (TREE_TYPE (inner)) != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1)))))) - return TREE_TYPE (TREE_OPERAND (inner, 1)); + { + tree alias_ptrtype = TREE_TYPE (TREE_OPERAND (inner, 1)); + /* Unless we have the (aggregate) effective type of the access + somewhere on the access path. If we have for example + (&a->elts[i])->l.len exposed by abstraction we'd see + MEM [(B *)a].elts[i].l.len and we can use the alias set + of 'len' when typeof (MEM [(B *)a].elts[i]) == B for + example. See PR111715. */ + if (AGGREGATE_TYPE_P (TREE_TYPE (alias_ptrtype))) + { + tree inner = *t; + while (handled_component_p (inner) + && (TYPE_MAIN_VARIANT (TREE_TYPE (inner)) + != TYPE_MAIN_VARIANT (TREE_TYPE (alias_ptrtype)))) + inner = TREE_OPERAND (inner, 0); + } + if (TREE_CODE (inner) == MEM_REF) + return alias_ptrtype; + } /* Otherwise, pick up the outermost object that we could have a pointer to. */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c new file mode 100644 index 00000000000..afd48050819 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c @@ -0,0 +1,32 @@ +/* PR/111715 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-fre1" } */ + +struct B { + struct { int len; } l; + long n; +}; +struct A { + struct B elts[8]; +}; + +static void +set_len (struct B *b, int len) +{ + b->l.len = len; +} + +static int +get_len (struct B *b) +{ + return b->l.len; +} + +int foo (struct A *a, int i, long *q) +{ + set_len (&a->elts[i], 1); + *q = 2; + return get_len (&a->elts[i]); +} + +/* { dg-final { scan-tree-dump "return 1;" "fre1" } } */