From patchwork Tue May 23 09:55: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: 97942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2016847vqo; Tue, 23 May 2023 02:56:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5/2Rp8+GOxKePanF7AgxAzet5uP+olvvK6ZGifInM0Rc2NRqP4fUIlT+KMb/6dbgpreF+P X-Received: by 2002:a17:907:a0e:b0:96b:48d2:1997 with SMTP id bb14-20020a1709070a0e00b0096b48d21997mr12720791ejc.65.1684835780189; Tue, 23 May 2023 02:56:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684835780; cv=none; d=google.com; s=arc-20160816; b=xRfOk9KM23Yfh5aSISQfC6K3rTHXlLhYOv2kp9CbTe7epj88lzIsXX97E5GLi11nK+ GOc9FRez//D1Gr1bmDzr1e2ePa92bCYvx9/Jyg8BxyBGgTVZ64qI24h7wYZrQtSDUeDx D6j0S2cg571cveZoK1oFqR7dGY8Mqm+4GLaXpskf87l2Dbft28FGopttsJfFxg0J2nFb tcZfVrGfoWwpRFCbxMvgOOgl8JYBIJXIoRwpnyKsUPMjZ5XDRbCkTUOKKqlGxHou/Kzv uzsGYpTGV3jrCpRxSVHBO44l/75rjU02cVqm/NNU7fKTIKIYEcz3994w7meDGk5GIuIu oALQ== 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=oIet7jUdL1yrxi8CzLbzlMaoYaW0BWACbr0YUKYISXE=; b=ZIdacLSPRfhoIKFmZWQuyH+Vo4/7nYSJkYARh6HEWEEod9awyEMG3eqf6Y4WDwrPeC ubbH24BTK/UdhjV4vrIjO1zgcxw6qEnMqLszkh3wXGIUL4/Y4P9nx1uQMr8A+yHmtfOz 2fafN10QKBwHU4o3mqSNUaaSGSXq/+P5UhhxHhJdYksDOfDc3Gc7//GEekXW2CzWcrUZ tGWkAVSnXtjZj9LaqFZcxxZZdrNVKWqV8bY2JKKUYI06/K3EylgYD7dKBOWOx44Wsaf0 Uj6ibGSoBYmfg7LzRkKBfz5dQYEr3269+HmTQ1oKu7P3/ntAZmGU0lZ725Ag6Jw9yuFj N6gQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ngjoCYmn; 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 sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id e10-20020a1709067e0a00b0096f7c26770csi981193ejr.260.2023.05.23.02.56.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 May 2023 02:56:20 -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=ngjoCYmn; 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 1CAB73858D37 for ; Tue, 23 May 2023 09:56:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1CAB73858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684835779; bh=oIet7jUdL1yrxi8CzLbzlMaoYaW0BWACbr0YUKYISXE=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ngjoCYmnJWohbMlPwH4Zfkyo/Lxn2hYWrVqQyw5u53GnxRQLVYVcFIw4QN9S4Rzn9 UUXcxE+y+FlM5icaqJe6hLXuQgwdpWvFucZRC5I6ZzBwVXeVpadZS9ZJXWodNEcToN 0APt5sRt/4XDEYJBHraXR6i9w07XdJrwEEybnYfI= 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 C06703858D35 for ; Tue, 23 May 2023 09:55:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C06703858D35 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-out1.suse.de (Postfix) with ESMTPS id C3F6F228BC for ; Tue, 23 May 2023 09:55: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 B1C0713588 for ; Tue, 23 May 2023 09:55:33 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id JdtAKpWNbGQxYgAAMHmgww (envelope-from ) for ; Tue, 23 May 2023 09:55:33 +0000 Date: Tue, 23 May 2023 11:55:33 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/109849 - missed code hoisting MIME-Version: 1.0 Message-Id: <20230523095533.B1C0713588@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 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, 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.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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766678363374089156?= X-GMAIL-MSGID: =?utf-8?q?1766678363374089156?= The following fixes code hoisting to properly consider ANTIC_OUT instead of ANTIC_IN. That's a bit expensive to re-compute but since we no longer iterate we're doing this only once per BB which should be acceptable. This avoids missing hoistings to the end of blocks where something in the block clobbers the hoisted value. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/109849 * tree-ssa-pre.cc (do_hoist_insertion): Compute ANTIC_OUT and use that to determine what to hoist. * gcc.dg/tree-ssa/ssa-hoist-8.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-hoist-8.c | 22 ++++++++++ gcc/tree-ssa-pre.cc | 48 ++++++++++++++++++--- 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-hoist-8.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-hoist-8.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-hoist-8.c new file mode 100644 index 00000000000..66bb48e0dc1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-hoist-8.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-pre-stats" } */ + +int mem; +void foo (); +int bar (int flag) +{ + int res; + foo (); + /* Hoist the load of mem here even though foo () clobbers it. */ + if (flag) + res = mem; + else + { + res = mem; + mem = 2; + } + return res; +} + +/* { dg-final { scan-tree-dump "HOIST inserted: 1" "pre" } } */ +/* { dg-final { scan-tree-dump-times " = mem;" 1 "pre" } } */ diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc index 1f7eea93c16..d56431b4145 100644 --- a/gcc/tree-ssa-pre.cc +++ b/gcc/tree-ssa-pre.cc @@ -3622,19 +3622,51 @@ do_hoist_insertion (basic_block block) && stmt_ends_bb_p (gsi_stmt (last))) return false; - /* Compute the set of hoistable expressions from ANTIC_IN. First compute + /* We have multiple successors, compute ANTIC_OUT by taking the intersection + of all of ANTIC_IN translating through PHI nodes. Note we do not have to + worry about iteration stability here so just intersect the expression sets + as well. This is a simplification of what we do in compute_antic_aux. */ + bitmap_set_t ANTIC_OUT = bitmap_set_new (); + bool first = true; + FOR_EACH_EDGE (e, ei, block->succs) + { + if (first) + { + phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e); + first = false; + } + else if (!gimple_seq_empty_p (phi_nodes (e->dest))) + { + bitmap_set_t tmp = bitmap_set_new (); + phi_translate_set (tmp, ANTIC_IN (e->dest), e); + bitmap_and_into (&ANTIC_OUT->values, &tmp->values); + bitmap_and_into (&ANTIC_OUT->expressions, &tmp->expressions); + bitmap_set_free (tmp); + } + else + { + bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values); + bitmap_and_into (&ANTIC_OUT->expressions, + &ANTIC_IN (e->dest)->expressions); + } + } + + /* Compute the set of hoistable expressions from ANTIC_OUT. First compute hoistable values. */ bitmap_set hoistable_set; - /* A hoistable value must be in ANTIC_IN(block) + /* A hoistable value must be in ANTIC_OUT(block) but not in AVAIL_OUT(BLOCK). */ bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack); bitmap_and_compl (&hoistable_set.values, - &ANTIC_IN (block)->values, &AVAIL_OUT (block)->values); + &ANTIC_OUT->values, &AVAIL_OUT (block)->values); /* Short-cut for a common case: hoistable_set is empty. */ if (bitmap_empty_p (&hoistable_set.values)) - return false; + { + bitmap_set_free (ANTIC_OUT); + return false; + } /* Compute which of the hoistable values is in AVAIL_OUT of at least one of the successors of BLOCK. */ @@ -3652,11 +3684,14 @@ do_hoist_insertion (basic_block block) /* Short-cut for a common case: availout_in_some is empty. */ if (bitmap_empty_p (&availout_in_some)) - return false; + { + bitmap_set_free (ANTIC_OUT); + return false; + } /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */ bitmap_move (&hoistable_set.values, &availout_in_some); - hoistable_set.expressions = ANTIC_IN (block)->expressions; + hoistable_set.expressions = ANTIC_OUT->expressions; /* Now finally construct the topological-ordered expression set. */ vec exprs = sorted_array_from_bitmap_set (&hoistable_set); @@ -3731,6 +3766,7 @@ do_hoist_insertion (basic_block block) } exprs.release (); + bitmap_set_free (ANTIC_OUT); return new_stuff; }