From patchwork Mon Nov 27 18:16:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Jambor X-Patchwork-Id: 170357 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3352053vqx; Mon, 27 Nov 2023 10:16:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFL7Np4zu9gckIakIF9SCm9bAhvvN1BBA7N/IYDbN9dJmYvruBhbSre8GztRYkiVAW4v1Rw X-Received: by 2002:ad4:4bc7:0:b0:67a:15db:a4d5 with SMTP id l7-20020ad44bc7000000b0067a15dba4d5mr16786432qvw.28.1701109008995; Mon, 27 Nov 2023 10:16:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701109008; cv=pass; d=google.com; s=arc-20160816; b=c1uAxfSRgXpVi9Dm+QHnPD+WnCz5EtfhChUk9lI00HqrGbMByuI35GGB2gVLXog5o3 JsLEQzPKSEQnumXA+K9fqpOtwF87Golp1fl4daBvyO3vKyTiCjcszYu2ATLaIFbCzpbT iVUgnD97QkIWkHMIq1Hz7DBYDU20ilHXIn60MkVkNrbr/lQWfpFrXcPfAoMA7n0lq8ke akFYHFfORhxnG+e4OS3ynGjDzq8DHdXzn3xBw0fgDTW5G9iHsDram2Uz53If29u6xvu1 LiVf/3KDbEdoqgDg7JxQ1JNbtwYSLWj6xyvNHA34zFDMt5jPcIP0CCRmIsQ4Ky44Dy+O Pdaw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:date:user-agent :subject:cc:to:from:dkim-signature:dkim-signature:arc-filter :dmarc-filter:delivered-to:message-id; bh=frmJ2l7LswalfTe1hz/sQLvJcFhMRnXEWw5fqkHWenA=; fh=A3Wf0JmQRmrpDq4HcJatpWHDr5j5KRjG4sAEmNkNl4c=; b=oZfPLilx2TT4oGaaOLiK+eAnDjIN++/AHTfjwPJbK6iqmBkhGC2+yPW3FTY5i6qyrX 3A8uP/X8vKC4GIq0TlYVaV+O9Iv1Ln7EP+dvX8bNxAxwa5y2105nzT40kSspel+a7EIA PTskJd3Ju3uSlZtpW/JFq8IbCDkJxlNtVMrv59/2ilJGI1RWYStuheK7clX36aDrhZse SVdE193rYMdNbJ8FamZIZEEJckWLBO8AGm3JFA5SrfrzTtzrbaMjMVhAg49uY1jDBtx/ X+sLCwPSedODgEK0Uy3yXT6WiUxDci6v2rwou8yOYJjAoQ/p+MU4HYoxaegnQdfiLx8X H56g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=CQCcay11; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; arc=pass (i=1); 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" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ep4-20020a05621418e400b006563f18c948si9224375qvb.367.2023.11.27.10.16.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 10:16:48 -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=@suse.cz header.s=susede2_rsa header.b=CQCcay11; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; arc=pass (i=1); 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" Message-ID: <6564dd10.050a0220.5a111.9cebSMTPIN_ADDED_BROKEN@mx.google.com> X-Google-Original-Message-ID: Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C1D30385802E for ; Mon, 27 Nov 2023 18:16:48 +0000 (GMT) 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 [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id 6E67C3858C2D for ; Mon, 27 Nov 2023 18:16:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E67C3858C2D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6E67C3858C2D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701108986; cv=none; b=qdbsCRq+KAN2dSPBnwvEYOxB5tQtGvC7fFEKup1JoWk+xdpW9wZgjbzKayF400m99fNaxK9RFK5C3v2SnhNaGdyFFxXZLgw3R1tR5DOjpwOfjI2bIrZMq3GTV3SoF2KGRHJmqJlCR5VXqFJnUaAQIeoS0k8OwvA2DWAzXH3dMGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701108986; c=relaxed/simple; bh=dQXqvzhrDnNy3XNRjkTN9MkFRqX7BhbFxAVvtx0E24M=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=CmrwoRypNcOu59Vz4vjmPMK4R/miNafOlJrA53tjQcnXwWMT7vUivR4FBXFRvbyTZzbtiLt0IRUtd5sIYpgXgj63E2sIMidro/7jNSXFamtKvsp1C1Hk1/TSbg8hDKc+T0/NKmFfCOa2lE8mViqZfSaHU8LpzJOUdjeHbce5Qzg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 874D51FC0D; Mon, 27 Nov 2023 18:16:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1701108981; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=frmJ2l7LswalfTe1hz/sQLvJcFhMRnXEWw5fqkHWenA=; b=CQCcay11GhBVMATi3OegKtHfVpUK5DxKN1vO2xmWRznU/KIT3IUfTPst7Z7Wyh9j1co3Wr SJ237/SeYSFBc6qxAaBXUsacssnmBMa7yzHhqU4D2IpEs3MYAU03SmmXvEADnnF6voA1k9 IEAtEn3zZ5RZ8VFvLHDAEafyiejP3Hw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1701108981; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=frmJ2l7LswalfTe1hz/sQLvJcFhMRnXEWw5fqkHWenA=; b=J7c3xXoehwxxefi7D75c4UdLcGFO4449jbksS/zm1yR/CLWaZUNMNFhvK8H73/oslm/IxO 4mRvJHcKxODtiuDw== Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 7CB5D13440; Mon, 27 Nov 2023 18:16:21 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id kbIsHvXcZGX+fgAAn2gu4w (envelope-from ); Mon, 27 Nov 2023 18:16:21 +0000 From: Martin Jambor To: GCC Patches Cc: Richard Biener , Jan Hubicka Subject: [PATCH] tree-sra: Avoid returns of references to SRA candidates User-Agent: Notmuch/0.37 (https://notmuchmail.org) Emacs/29.1 (x86_64-suse-linux-gnu) Date: Mon, 27 Nov 2023 19:16:17 +0100 MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -0.90 X-Spamd-Result: default: False [-0.90 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; TO_DN_ALL(0.00)[]; INVALID_MSGID(1.70)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, INVALID_MSGID, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783742080412117631 X-GMAIL-MSGID: 1783742080412117631 Hi, The enhancement to address PR 109849 contained an importsnt thinko, and that any reference that is passed to a function and does not escape, must also not happen to be aliased by the return value of the function. This has quickly transpired as bugs PR 112711 and PR 112721. Just as IPA-modref does a good enough job to allow us to rely on the escaped set of variables, it sems to be doing well also on updating EAF_NOT_RETURNED_DIRECTLY call argument flag which happens to address exactly the situation we need to avoid. Of course, if a call statement ignores any returned value, we also do not need to check the flag. Hopefully this does not pessimize things too much, I have verified that the PR 109849 testcae remains quick and so should also the benchmark it is derived from. The patch has passed bootstrap and testing on x86_64-linux, OK for master? Thanks, Martin gcc/ChangeLog: 2023-11-27 Martin Jambor PR tree-optimization/112711 PR tree-optimization/112721 * tree-sra.cc (build_access_from_call_arg): New parameter CAN_BE_RETURNED, disqualify any candidate passed by reference if it is true. Adjust leading comment. (scan_function): Pass appropriate value to CAN_BE_RETURNED of build_access_from_call_arg. gcc/testsuite/ChangeLog: 2023-11-27 Martin Jambor PR tree-optimization/112711 PR tree-optimization/112721 * g++.dg/tree-ssa/pr112711.C: New test. * gcc.dg/tree-ssa/pr112721.c: Likewise. --- gcc/testsuite/g++.dg/tree-ssa/pr112711.C | 31 ++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr112721.c | 26 +++++++++++++++ gcc/tree-sra.cc | 40 ++++++++++++++++++------ 3 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr112711.C create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr112721.c diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr112711.C b/gcc/testsuite/g++.dg/tree-ssa/pr112711.C new file mode 100644 index 00000000000..c04524b04a7 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr112711.C @@ -0,0 +1,31 @@ +/* { dg-do run } */ +/* { dg-options "-O1" } */ + +typedef int i32; +typedef unsigned int u32; + +static inline void write_i32(void *memory, i32 value) { + // swap i32 bytes as if it was u32: + u32 u_value = value; + value = __builtin_bswap32(u_value); + + // llvm infers '1' alignment from destination type + __builtin_memcpy(__builtin_assume_aligned(memory, 1), &value, sizeof(value)); +} + +__attribute__((noipa)) +static void bug (void) { + #define assert_eq(lhs, rhs) if (lhs != rhs) __builtin_trap() + + unsigned char data[5]; + write_i32(data, -1362446643); + assert_eq(data[0], 0xAE); + assert_eq(data[1], 0xCA); + write_i32(data + 1, -1362446643); + assert_eq(data[1], 0xAE); +} + +int main() { + bug(); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr112721.c b/gcc/testsuite/gcc.dg/tree-ssa/pr112721.c new file mode 100644 index 00000000000..adf62613266 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr112721.c @@ -0,0 +1,26 @@ +/* { dg-do run } */ +/* { dg-options "-O1" } */ + +unsigned * volatile gv; + +struct a { + int b; +}; +int c, e; +long d; +unsigned * __attribute__((noinline)) +f(unsigned *g) { + for (; c;) + e = d; + return gv ? gv : g; +} +int main() { + int *h; + struct a i = {8}; + int *j = &i.b; + h = (unsigned *) f(j); + *h = 0; + if (i.b != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/tree-sra.cc b/gcc/tree-sra.cc index 3a0d52675fe..6a759783990 100644 --- a/gcc/tree-sra.cc +++ b/gcc/tree-sra.cc @@ -1268,18 +1268,27 @@ abnormal_edge_after_stmt_p (gimple *stmt, enum out_edge_check *oe_check) } /* Scan expression EXPR which is an argument of a call and create access - structures for all accesses to candidates for scalarization. Return true if - any access has been inserted. STMT must be the statement from which the - expression is taken. */ + structures for all accesses to candidates for scalarization. Return true + if any access has been inserted. STMT must be the statement from which the + expression is taken. CAN_BE_RETURNED must be true if call argument flags + do not rule out that the argument is directly returned. OE_CHECK is used + to remember result of a test for abnormal outgoing edges after this + statement. */ static bool -build_access_from_call_arg (tree expr, gimple *stmt, +build_access_from_call_arg (tree expr, gimple *stmt, bool can_be_returned, enum out_edge_check *oe_check) { if (TREE_CODE (expr) == ADDR_EXPR) { tree base = get_base_address (TREE_OPERAND (expr, 0)); + if (can_be_returned) + { + disqualify_base_of_expr (base, "Address possibly returned, " + "leading to an alis SRA may not know."); + return false; + } if (abnormal_edge_after_stmt_p (stmt, oe_check)) { disqualify_base_of_expr (base, "May lead to need to add statements " @@ -1508,12 +1517,25 @@ scan_function (void) case GIMPLE_CALL: { enum out_edge_check oe_check = SRA_OUTGOING_EDGES_UNCHECKED; - for (i = 0; i < gimple_call_num_args (stmt); i++) - ret |= build_access_from_call_arg (gimple_call_arg (stmt, i), - stmt, &oe_check); + gcall *call = as_a (stmt); + for (i = 0; i < gimple_call_num_args (call); i++) + { + bool can_be_returned; + if (gimple_call_lhs (call)) + { + int af = gimple_call_arg_flags (call, i); + can_be_returned = !(af & EAF_NOT_RETURNED_DIRECTLY); + } + else + can_be_returned = false; + ret |= build_access_from_call_arg (gimple_call_arg (call, + i), + stmt, can_be_returned, + &oe_check); + } if (gimple_call_chain(stmt)) - ret |= build_access_from_call_arg (gimple_call_chain(stmt), - stmt, &oe_check); + ret |= build_access_from_call_arg (gimple_call_chain(call), + stmt, false, &oe_check); } t = gimple_call_lhs (stmt);