From patchwork Tue Nov 29 13:30:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 27234 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp336970wrr; Tue, 29 Nov 2022 05:31:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf6smJR+hYCwlwJ/5sHNg8+WP1s8yQGaAyUBZWzKxWSK4it6s/lKGXUvAG4DjHT33o6XPA0F X-Received: by 2002:a17:906:fc5:b0:7c0:8b4c:e30f with SMTP id c5-20020a1709060fc500b007c08b4ce30fmr2102319ejk.502.1669728671491; Tue, 29 Nov 2022 05:31:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669728671; cv=none; d=google.com; s=arc-20160816; b=gekIhjkoUp1eKPf/STm4quPb2lfyo+Anr3iDxqksgcHYb8iI6/YSZ36eY5/Giy6qiL U6w4Y3OeH8Zx32O6phYc1igfonxK39dKtGpl5ftYGp/a7heGQ1QbrcwSHBqbVUKSEuUq Dad3Yag4ZjgFzDZDpVzrDTwpEywgbCQ/YbAf+jVLByIHcUTVy3ElYP1N9SOYPpp62Typ /ePj3sUIukkv+fAwqd3lVk7YhKol6fZhJUhkfcOFC5Kj7OPp0EifkMLSIbgJt2PNrXJW zzsaItHyMhTovDSn0Q57JB3TacxYGVFZE5F4tv3Oou0K/ge5PQ3cosYULUdSyyz2UnZI mjvA== 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=K5uNyjwCnwv0CuaP895NNVCzPRRYqCVmwbOgj15c3Mo=; b=d1KRcgAF5nK1MSaajf4j7vQmwMMuad0qCoc9vzO0Dwh6OZhc4QuRCdZqzOB+tadkQx 47G8J/tlFfMS5QPSBqmxf3W7HsR5FpgMyoECXQf/uHu7KqMENha2fyq6+HgAE/b7l+d8 dVwQri3k+CTipq/awuY16GebDbxBpNGxGorvud16pEr67HNY8qxkW7Ss3Rahz5I4AhWj xJ0te2Xz7VlupXhd14u/lLFeyYaDy4hmGmHMCZpyvNJPaIbhfU9CSO1EI40/obB1pBt8 0MxN7TgijU9jNmoHg3+dggCnN5QybYKiqM6onKmnasq+hyjatWDyovZCErS3G/DqCu9r ncKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Wq8815nx; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id a4-20020a05640213c400b0045945eed10asi11012541edx.5.2022.11.29.05.31.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 05:31:11 -0800 (PST) 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=Wq8815nx; 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 9F36E385B505 for ; Tue, 29 Nov 2022 13:31:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9F36E385B505 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669728666; bh=K5uNyjwCnwv0CuaP895NNVCzPRRYqCVmwbOgj15c3Mo=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Wq8815nxk5weV5MwGVvkN+IXPXrAW0cRJFnX4PCzNxQgAx+EsurqUAK37SuKojNCo +f34x4VnsCA3g2XmkI4fUEK0lLlPx+XfnTGN/fYB4lV9hB6bLyrERa8YTtLGulSQUT BXcVFwoyoGotd9t6XN+MdZDoiSYpXqfxzIWhWKxE= 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 D14583858D1E for ; Tue, 29 Nov 2022 13:30:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D14583858D1E 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 AD2B221BBE for ; Tue, 29 Nov 2022 13:30:22 +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 99C0C13AF6 for ; Tue, 29 Nov 2022 13:30:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id jV2GJG4JhmMMMQAAMHmgww (envelope-from ) for ; Tue, 29 Nov 2022 13:30:22 +0000 Date: Tue, 29 Nov 2022 14:30:22 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/107852 - missed optimization with PHIs MIME-Version: 1.0 Message-Id: <20221129133022.99C0C13AF6@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 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?1750837411481667302?= X-GMAIL-MSGID: =?utf-8?q?1750837411481667302?= The following deals with the situation where we have [local count: 1073741824]: _5 = bytes.D.25336._M_impl.D.24643._M_start; _6 = bytes.D.25336._M_impl.D.24643._M_finish; pretmp_66 = bytes.D.25336._M_impl.D.24643._M_end_of_storage; if (_5 != _6) goto ; [70.00%] else goto ; [30.00%] ... [local count: 329045359]: _89 = operator new (4); _43 = bytes.D.25336._M_impl.D.24643._M_start; _Num_44 = _137 - _43; if (_Num_44 != 0) but fail to see that _137 is equal to _5 and thus eventually _Num_44 is zero if not operator new would possibly clobber the global bytes variable. The following resolves this in value-numbering by using the predicated values for _5 == _6 recorded for the dominating condition. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/107852 * tree-ssa-sccvn.cc (visit_phi): Use equivalences recorded as predicated values to elide more redundant PHIs. * gcc.dg/tree-ssa/ssa-fre-101.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c | 47 +++++++++++++++++++ gcc/tree-ssa-sccvn.cc | 51 ++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c new file mode 100644 index 00000000000..c67f211dcf6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-101.c @@ -0,0 +1,47 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-fre1-details" } */ + +int test1 (int i, int j) +{ + int k; + if (i != j) + k = i; + else + k = j; + return k; +} + +int test2 (int i, int j) +{ + int k; + if (i != j) + k = j; + else + k = i; + return k; +} + +int test3 (int i, int j) +{ + int k; + if (i == j) + k = j; + else + k = i; + return k; +} + +int test4 (int i, int j) +{ + int k; + if (i == j) + k = i; + else + k = j; + return k; +} + +/* We'd expect 4 hits but since we only keep one forwarder the + VN predication machinery cannot record something for the entry + block since it doesn't work on edges but on their source. */ +/* { dg-final { scan-tree-dump-times "equal on edge" 2 "fre1" } } */ diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 1f9c6c53b52..6895ae84d13 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -5814,7 +5814,8 @@ visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p) /* See if all non-TOP arguments have the same value. TOP is equivalent to everything, so we can ignore it. */ - FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds) + basic_block bb = gimple_bb (phi); + FOR_EACH_EDGE (e, ei, bb->preds) if (e->flags & EDGE_EXECUTABLE) { tree def = PHI_ARG_DEF_FROM_EDGE (phi, e); @@ -5859,6 +5860,54 @@ visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p) && known_eq (soff, doff)) continue; } + /* There's also the possibility to use equivalences. */ + if (!FLOAT_TYPE_P (TREE_TYPE (def))) + { + vn_nary_op_t vnresult; + tree ops[2]; + ops[0] = def; + ops[1] = sameval; + tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR, + boolean_type_node, + ops, &vnresult); + if (! val && vnresult && vnresult->predicated_values) + { + val = vn_nary_op_get_predicated_value (vnresult, e->src); + if (val && integer_truep (val)) + { + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "Predication says "); + print_generic_expr (dump_file, def, TDF_NONE); + fprintf (dump_file, " and "); + print_generic_expr (dump_file, sameval, TDF_NONE); + fprintf (dump_file, " are equal on edge %d -> %d\n", + e->src->index, e->dest->index); + } + continue; + } + /* If on all previous edges the value was equal to def + we can change sameval to def. */ + if (EDGE_COUNT (bb->preds) == 2 + && (val = vn_nary_op_get_predicated_value + (vnresult, EDGE_PRED (bb, 0)->src)) + && integer_truep (val)) + { + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "Predication says "); + print_generic_expr (dump_file, def, TDF_NONE); + fprintf (dump_file, " and "); + print_generic_expr (dump_file, sameval, TDF_NONE); + fprintf (dump_file, " are equal on edge %d -> %d\n", + EDGE_PRED (bb, 0)->src->index, + EDGE_PRED (bb, 0)->dest->index); + } + sameval = def; + continue; + } + } + } sameval = NULL_TREE; break; }