From patchwork Sat Jun 22 13:23:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1951088 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=X3rCBi2h; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Suoc37qO; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=X3rCBi2h; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Suoc37qO; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W5w1K6qq1z20Wb for ; Sat, 22 Jun 2024 23:24:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4325D382EF2F for ; Sat, 22 Jun 2024 13:24:20 +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 [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id 749E1382FAF5 for ; Sat, 22 Jun 2024 13:23:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 749E1382FAF5 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 749E1382FAF5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719062637; cv=none; b=SQJz5vZ1ha25tnzh4yjgBcSeeMuHwm7aQWI30hy7NcdNT/yumLzhDSKriXFyFwp9/RX+XzIeH0FKZyzMwG49PwtUHduGZCu2ggzOn+aFX7iNJXWaNLx/C35dO5BocoRUABEg7tajEuWUIVZhjRGXumRVLs72OS03KTS/E5CvAjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719062637; c=relaxed/simple; bh=ahAk8BL7QyQjC5xouyTs27W6kKIA3lhtd+N5JpIGDRU=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=wiua2Bt72WYKsZYrIeSxyPachqGSrZoooxyQOS34EPX7EwGMzZW9w4wsVOcMpJK+H6T+6t+IvoIgYSRQyL9bmL26Op5+i3iVNzo3g11ean3Q5rKEosXKLrkHTEmIycQipYWb1okEuJMZJTU1mOD2Na3r93kMt4yHjz8USZIhU0E= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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-out1.suse.de (Postfix) with ESMTPS id 20D3921BA2 for ; Sat, 22 Jun 2024 13:23:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1719062633; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=cJaBH4tnd7cuhdx5egLQ41/MvN/2w8W1AX8IalzBXOA=; b=X3rCBi2hPqov0HDQQf1G7D8hmRhCD6nVeYbDqXiafXf1Fksifh+JPUq9xU3pdIQPcTQSKc EMO5Q+rpNhizSaRYPTiobimYtCnvV4qaBX+YqoZvN/E1ptq3ez3bhDJeUXASsDUNCfhzaZ 7CHfbmWzGtJdBARubooF4Z3Ge2s5LPQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1719062633; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=cJaBH4tnd7cuhdx5egLQ41/MvN/2w8W1AX8IalzBXOA=; b=Suoc37qOSN+haDXEFpZV7XkPSGaou1xTaVzP/VvfnMe1NLSFpztV62+FzB1R1FNz2vGqOw OCUyo8+HpPVJqGDg== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=X3rCBi2h; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=Suoc37qO DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1719062633; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=cJaBH4tnd7cuhdx5egLQ41/MvN/2w8W1AX8IalzBXOA=; b=X3rCBi2hPqov0HDQQf1G7D8hmRhCD6nVeYbDqXiafXf1Fksifh+JPUq9xU3pdIQPcTQSKc EMO5Q+rpNhizSaRYPTiobimYtCnvV4qaBX+YqoZvN/E1ptq3ez3bhDJeUXASsDUNCfhzaZ 7CHfbmWzGtJdBARubooF4Z3Ge2s5LPQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1719062633; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=cJaBH4tnd7cuhdx5egLQ41/MvN/2w8W1AX8IalzBXOA=; b=Suoc37qOSN+haDXEFpZV7XkPSGaou1xTaVzP/VvfnMe1NLSFpztV62+FzB1R1FNz2vGqOw OCUyo8+HpPVJqGDg== Received: from imap1.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 04E1813ABD for ; Sat, 22 Jun 2024 13:23:52 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id yuzNOmjQdmbpEAAAD6G6ig (envelope-from ) for ; Sat, 22 Jun 2024 13:23:52 +0000 Date: Sat, 22 Jun 2024 15:23:52 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/115579 - fix wrong code with store-motion MIME-Version: 1.0 Message-Id: <20240622132353.04E1813ABD@imap1.dmz-prg2.suse.org> X-Spamd-Result: default: False [-6.51 / 50.00]; BAYES_HAM(-3.00)[100.00%]; DWL_DNSWL_MED(-2.00)[suse.de:dkim]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; MISSING_XM_UA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gcc-patches@gcc.gnu.org]; DKIM_TRACE(0.00)[suse.de:+] X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Queue-Id: 20D3921BA2 X-Spam-Score: -6.51 X-Spam-Level: X-Spam-Status: No, score=-11.6 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org The recent change to relax store motion for variables that cannot have store data races broke the optimization to share flag vars for stores that all happen in the same single BB. The following fixes this. Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. PR tree-optimization/115579 * tree-ssa-loop-im.cc (execute_sm): Return the auxiliary data created. (hoist_memory_references): Record the flag var that's eventually created and re-use it when all stores are in the same BB. * gcc.dg/pr115579.c: New testcase. --- gcc/testsuite/gcc.dg/pr115579.c | 18 ++++++++++++++++++ gcc/tree-ssa-loop-im.cc | 27 ++++++++++++++------------- 2 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr115579.c diff --git a/gcc/testsuite/gcc.dg/pr115579.c b/gcc/testsuite/gcc.dg/pr115579.c new file mode 100644 index 00000000000..04781056723 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr115579.c @@ -0,0 +1,18 @@ +/* { dg-do run } */ +/* { dg-options "-Os -fno-tree-sra" } */ + +int printf(const char *, ...); +int a, b = 1, c; +int main() { + int d[2], *e = &d[1]; + while (a) { + int *f = &b; + d[1] = 0; + *f = 0; + } + if (c) + printf("%d\n", *e); + if (b != 1) + __builtin_abort(); + return 0; +} diff --git a/gcc/tree-ssa-loop-im.cc b/gcc/tree-ssa-loop-im.cc index 3acbd886a0d..61c6339bc35 100644 --- a/gcc/tree-ssa-loop-im.cc +++ b/gcc/tree-ssa-loop-im.cc @@ -2269,7 +2269,7 @@ struct sm_aux temporary variable is put to the preheader of the loop, and assignments to the reference from the temporary variable are emitted to exits. */ -static void +static sm_aux * execute_sm (class loop *loop, im_mem_ref *ref, hash_map &aux_map, bool maybe_mt, bool use_other_flag_var) @@ -2345,6 +2345,8 @@ execute_sm (class loop *loop, im_mem_ref *ref, lim_data->tgt_loop = loop; gsi_insert_before (&gsi, load, GSI_SAME_STMT); } + + return aux; } /* sm_ord is used for ordinary stores we can retain order with respect @@ -2802,20 +2804,18 @@ hoist_memory_references (class loop *loop, bitmap mem_refs, hash_map aux_map; /* Execute SM but delay the store materialization for ordered - sequences on exit. */ - bool first_p = true; + sequences on exit. Remember a created flag var and make + sure to re-use it. */ + sm_aux *flag_var_aux = nullptr; EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi) { ref = memory_accesses.refs_list[i]; - execute_sm (loop, ref, aux_map, true, !first_p); - first_p = false; + sm_aux *aux = execute_sm (loop, ref, aux_map, true, + flag_var_aux != nullptr); + if (aux->store_flag) + flag_var_aux = aux; } - /* Get at the single flag variable we eventually produced. */ - im_mem_ref *ref - = memory_accesses.refs_list[bitmap_first_set_bit (mem_refs)]; - sm_aux *aux = *aux_map.get (ref); - /* Materialize ordered store sequences on exits. */ edge e; FOR_EACH_VEC_ELT (exits, i, e) @@ -2826,13 +2826,14 @@ hoist_memory_references (class loop *loop, bitmap mem_refs, /* Construct the single flag variable control flow and insert the ordered seq of stores in the then block. With -fstore-data-races we can do the stores unconditionally. */ - if (aux->store_flag) + if (flag_var_aux) insert_e = single_pred_edge (execute_sm_if_changed (e, NULL_TREE, NULL_TREE, - aux->store_flag, + flag_var_aux->store_flag, loop_preheader_edge (loop), - &aux->flag_bbs, append_cond_position, + &flag_var_aux->flag_bbs, + append_cond_position, last_cond_fallthru)); execute_sm_exit (loop, insert_e, seq, aux_map, sm_ord, append_cond_position, last_cond_fallthru);