From patchwork Fri Aug 18 13:04:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 136015 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp1385193vqi; Fri, 18 Aug 2023 06:05:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEedebX3Bcua9pO8mGPOWHZkJmSlCgQ3kMeZeLDy3hohHVHwTP2yPiRng308/Xmv6XIFfh4 X-Received: by 2002:a05:6512:1104:b0:4fd:c923:db5e with SMTP id l4-20020a056512110400b004fdc923db5emr2008279lfg.21.1692363924831; Fri, 18 Aug 2023 06:05:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692363924; cv=none; d=google.com; s=arc-20160816; b=U26HJ8Z5ciJGC/QqmfberQQZobkMyZRZVHN+tG79JGqm5OEOMd5Gq1AXc4NBAc04ui AJSxGW7O3GLEg13IMEa6cLhbc39qalde9PuTG2AHOrjdi97nqJ0pPU00bWMYBCybSkBC fA/t957THJvLEqA1puErGd7dfmZu/9U57KO8ZecFSrmUMz4BTk4EMfvbiJsdNK4diB9f E42TZybrr7lxALZiNpjPG4rkKF6xQ/X8gvXtPUzVhGIrm5u8ikBNIElcJPNtG1UBvPSc FqxUCqNoXpPBdHmzCWiEBGfJc42AMtLBlEa/77/zSnBQ89Fsrc+HEkaAOAjWf/NVBVMe +LfQ== 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=Qvmc9xOJBAxRZwsV2VRv/mhc/67iPmx/3jPhtessgwg=; fh=etb9MYHN7HLF/sff76ICVdPeKiI8ZsjoOL2bcdG0aog=; b=EpBYF4XacqEefg17bnkxcbYZMYNI3r/zv/B9ayqa6Tst8Czd8N517P2ZNcMBQGyjIF 5fGfGJJT1MbJf2UKtuj7Njys4JzGwOZAnZucE//6VxOCSEWESPnQnomMMF5yHgbxrElt wIy1wnPbtBcYtRMoQTc62/Wxfn70MfTF5+gRfbujjqkHYGGThNrrHpfKRhOmS/EV6Kac xVeCfOFz5RQMqRTukcjdnn2Znd5AxOXcoINl0mpoUXvjk1Kt4wnwvjr/cbrBDAiLC4jI DYAT1rwtci4+af2BhaNE+k0Konl1QvyjaKvU1NH4zCR5FJwjcP7zfMBV9dSedetODlPP Q0yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=peVn+rDE; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id qw23-20020a170906fcb700b0099e029797f7si1202117ejb.868.2023.08.18.06.05.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Aug 2023 06:05:24 -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=@gcc.gnu.org header.s=default header.b=peVn+rDE; 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 D1A5B38768A0 for ; Fri, 18 Aug 2023 13:05:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D1A5B38768A0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692363918; bh=Qvmc9xOJBAxRZwsV2VRv/mhc/67iPmx/3jPhtessgwg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=peVn+rDEdbO/9oQh/eawHw4YO5vRFcaORo4bMPbJnGyYaOk/ZpDsyREEwiGuPBrqM 0p0z7XGjQNP1bMkjWcCQrKiSzRB1JmrU5KjaenlEk+z3WXyTp4GepfzbKcuM1WgnUo diFRIJoaluDbdIBmXAYiBCbX3de62ELlMFbNmfTI= 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 01DC53851ABE for ; Fri, 18 Aug 2023 13:04:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 01DC53851ABE 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 E9D941F8A8 for ; Fri, 18 Aug 2023 13:04:33 +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 D4E24138F0 for ; Fri, 18 Aug 2023 13:04:33 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id gV+/MmFs32R9FwAAMHmgww (envelope-from ) for ; Fri, 18 Aug 2023 13:04:33 +0000 Date: Fri, 18 Aug 2023 15:04:33 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/111019 - invariant motion and aliasing MIME-Version: 1.0 Message-Id: <20230818130433.D4E24138F0@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.5 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: INBOX X-GMAIL-THRID: 1774572194646175420 X-GMAIL-MSGID: 1774572194646175420 The following fixes a bad choice in representing things to the alias oracle by LIM which while correct in pieces is inconsistent with itself. When canonicalizing a ref to a bare deref instead of leaving the base object and the extracted offset the same and just substituting an alternate ref the following replaces the base and the offset as well, avoiding the confusion that otherwise will arise in aliasing_matching_component_refs_p. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed to trunk sofar. Richard. PR tree-optimization/111019 * tree-ssa-loop-im.cc (gather_mem_refs_stmt): When canonicalizing also scrap base and offset in case the ref is indirect. * g++.dg/torture/pr111019.C: New testcase. --- gcc/testsuite/g++.dg/torture/pr111019.C | 65 +++++++++++++++++++++++++ gcc/tree-ssa-loop-im.cc | 14 +++++- 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/torture/pr111019.C diff --git a/gcc/testsuite/g++.dg/torture/pr111019.C b/gcc/testsuite/g++.dg/torture/pr111019.C new file mode 100644 index 00000000000..ce21a311c96 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr111019.C @@ -0,0 +1,65 @@ +// { dg-do run } +// { dg-additional-options "-fstrict-aliasing" } + +#include +#include +#include + +class Base +{ +public: + Base* previous = nullptr; + Base* next = nullptr; + Base* target = nullptr; +}; + +class Target : public Base +{ +public: + __attribute__((always_inline)) ~Target() + { + while (this->next) + { + Base* n = this->next; + + if (n->previous) + n->previous->next = n->next; + if (n->next) + n->next->previous = n->previous; + n->previous = nullptr; + n->next = nullptr; + n->target = nullptr; + } + } +}; + +template +class TargetWithData final : public Target +{ +public: + TargetWithData(T data) + : data(data) + {} + T data; +}; + +void test() +{ + printf("test\n"); + Base ptr; + { + auto data = std::make_unique>(std::string("asdf")); + ptr.target = &*data; + ptr.previous = &*data; + data->next = &ptr; + + assert(ptr.target != nullptr); + } + assert(ptr.target == nullptr); +} + +int main(int, char**) +{ + test(); + return 0; +} diff --git a/gcc/tree-ssa-loop-im.cc b/gcc/tree-ssa-loop-im.cc index 268f466bdc9..b8e33a4d49a 100644 --- a/gcc/tree-ssa-loop-im.cc +++ b/gcc/tree-ssa-loop-im.cc @@ -1665,11 +1665,21 @@ gather_mem_refs_stmt (class loop *loop, gimple *stmt) unshare_expr (mem_base)); if (TYPE_ALIGN (ref_type) != ref_align) ref_type = build_aligned_type (ref_type, ref_align); - (*slot)->mem.ref + tree new_ref = fold_build2 (MEM_REF, ref_type, tmp, build_int_cst (ref_alias_type, mem_off)); if ((*slot)->mem.volatile_p) - TREE_THIS_VOLATILE ((*slot)->mem.ref) = 1; + TREE_THIS_VOLATILE (new_ref) = 1; + (*slot)->mem.ref = new_ref; + /* Make sure the recorded base and offset are consistent + with the newly built ref. */ + if (TREE_CODE (TREE_OPERAND (new_ref, 0)) == ADDR_EXPR) + ; + else + { + (*slot)->mem.base = new_ref; + (*slot)->mem.offset = 0; + } gcc_checking_assert (TREE_CODE ((*slot)->mem.ref) == MEM_REF && is_gimple_mem_ref_addr (TREE_OPERAND ((*slot)->mem.ref,