From patchwork Thu May 4 21:56:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 90255 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp631741vqo; Thu, 4 May 2023 14:57:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Q1Kq9kiqImmWGVZ65DSW7nk1mB/czMEb/tLmS2mgJonoPy0L0fGCNbOmYKp/QQckOqliA X-Received: by 2002:a17:907:c21:b0:90b:53f6:fd8b with SMTP id ga33-20020a1709070c2100b0090b53f6fd8bmr249244ejc.31.1683237455191; Thu, 04 May 2023 14:57:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683237455; cv=none; d=google.com; s=arc-20160816; b=xG9EAu4c9kfSTQ+UgnHOxVpRrpkxrnkYUjuHhgnN3Wni14EEPeEWlaIgGMHG6xahKM kxDUSh9GuRNr1XEr5Ia7nl2b2dd4OHvzv5O5GnAsqx6x3I+2xOcalEuBR9PWH5FUMv36 GXxQpdtqSC6c2TeHS258Pd1LUFTGSIuHqc2/biT5O+JJMekYTrfwUoh8PdN3pHq7mHd2 c6Ovd15mOEOlgIOyFZ1uElXGJPLj3+DD7mFmNkhTNWIN9CKyHsfCQpHaEct3SYuaDiWi vYyjiG14vk0OUUob8ZFIyiUZHCM+u6CvKDlEshaJN5xkJ7Wt+7WHaMnLz+lfji110Zr5 jGaQ== 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 :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=CWXq1hlIPD8JSUexP5XI/9acy9vpWSvEaMjKbLItO40=; b=SMTGdfi0O09PHbCnf6WFACm9kSC9xNgReGEoEXAbZ80LHPFD+MFh/4Y/euKZBpcTUn gsLF5cnCKzJRKN+hY5OaFtwe5Ql3U2lrHIYVqZtA09Mrt/vFrWCE5cF+7ll9aqhc32Zi Q6RdtpLu2BlIEfbtWe6S2m6vWBa1MAuqXyJ90gkt8LXZVKA0LeoLlcTY2RcaHa9hw/tc 7uUpF8wxR2GSSeL0rYHgDNBNLMD0pxqjdjn9tvWTuh4Pq03wXbeT8FUB7bx3VOurmLu7 gIAH4K1xVnkBJh/WWW2OC3F/3oGorc9DGemOmOv7buibGPa6bCE7XRjgrl/67Cr6OnqY mN8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ZKt1+kZv; 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 lv23-20020a170906bc9700b0094f03310057si87360ejb.833.2023.05.04.14.57.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 May 2023 14:57:35 -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=ZKt1+kZv; 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 1E4E23857353 for ; Thu, 4 May 2023 21:57:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E4E23857353 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683237454; bh=CWXq1hlIPD8JSUexP5XI/9acy9vpWSvEaMjKbLItO40=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ZKt1+kZv6SXTosYBzoUj4LZftrw10RvvkZTiGKBiNBMqTPfRny661jsXYouv8euPH q97xkuvuOIaPGH2Rwc+NITJYWpnREBI+YTwbqKgmj1VzDa7FDWaJV7Y35T4S/kiqUd KmX+sRYoa0eMXjfuKc49ccpYLFfp9p0GKcwNDapI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id E7F653858D20 for ; Thu, 4 May 2023 21:56:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E7F653858D20 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 344LskXL009138 for ; Thu, 4 May 2023 14:56:48 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3qckun89q5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 04 May 2023 14:56:48 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 4 May 2023 14:56:47 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Thu, 4 May 2023 14:56:47 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id B7E9B3F7084; Thu, 4 May 2023 14:56:46 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] PHIOPT: Fix diamond case of match_simplify_replacement Date: Thu, 4 May 2023 14:56:38 -0700 Message-ID: <20230504215638.988177-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: EgZN1kzpv5EuVrR2pk-Uyct9ABVdwdxJ X-Proofpoint-GUID: EgZN1kzpv5EuVrR2pk-Uyct9ABVdwdxJ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-04_13,2023-05-04_01,2023-02-09_01 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski 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?1765002397750786046?= X-GMAIL-MSGID: =?utf-8?q?1765002397750786046?= So it turns out I messed checking which edge was true/false for the diamond form. The edges, e0 and e1 here are edges from the merge block but the true/false edges are from the conditional block and with diamond/threeway, there is a bb inbetween on both edges. Most of the time, the check that was in match_simplify_replacement would happen to be correct for diamond form as most of the time the first edge in the conditional is the edge for the true side of the conditional. This is why I didn't see the issue during bootstrap/testing. I added a fragile gimple testcase which exposed the issue. Since there is no way to specify the order of the edges in the gimple fe, we have to have forwprop to swap the false/true edges (not order of them, just swapping true/false flags) and hope not to do cleanupcfg inbetween forwprop and the first phiopt pass. This is the fragile part really, it is not that we will produce wrong code, just we won't hit what was the failing case. OK? Bootstrapped and tested on x86_64-linux-gnu. PR tree-optimization/109732 gcc/ChangeLog: * tree-ssa-phiopt.cc (match_simplify_replacement): Fix the selection of the argtrue/argfalse. gcc/testsuite/ChangeLog: * gcc.dg/pr109732.c: New test. --- gcc/testsuite/gcc.dg/pr109732.c | 40 +++++++++++++++++++++++++++++++++ gcc/tree-ssa-phiopt.cc | 29 +++++++++++++++++++++--- 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr109732.c diff --git a/gcc/testsuite/gcc.dg/pr109732.c b/gcc/testsuite/gcc.dg/pr109732.c new file mode 100644 index 00000000000..d8374705cd8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr109732.c @@ -0,0 +1,40 @@ +/* { dg-do run } */ +/* We need to disable passes which might cause cfg cleanup */ +/* { dg-options "-O1 -fgimple -fdisable-tree-ethread -fdisable-tree-fre1" } */ + +/* This code is done this way to have the false edge as 1st + successor edge of BB2. Normally the true edge would be + the first and you would not hit the bug. */ +[[gnu::noipa]] +_Bool __GIMPLE (ssa, startwith("forwprop1")) +f3 (_Bool a) +{ + _Bool i; + _Bool tt; + + __BB(2): + tt_4 = a_1(D) == _Literal (_Bool)0; + if (tt_4 != _Literal (_Bool)0) + goto __BB3; + else + goto __BB4; + + __BB(3): + goto __BB5; + + __BB(4): + goto __BB5; + + __BB(5): + i_2 = __PHI (__BB4: a_1(D), __BB3: _Literal (_Bool)0); + + return i_2; +} + +int main() +{ + if (f3(0)) + __builtin_abort(); + if (!f3(1)) + __builtin_abort(); +} diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 14aeaadd6f6..2fb28b4e60e 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -726,6 +726,7 @@ match_simplify_replacement (basic_block cond_bb, basic_block middle_bb, gimple *stmt_to_move = NULL; gimple *stmt_to_move_alt = NULL; auto_bitmap inserted_exprs; + tree arg_true, arg_false; /* Special case A ? B : B as this will always simplify to B. */ if (operand_equal_for_phi_arg_p (arg0, arg1)) @@ -756,12 +757,34 @@ match_simplify_replacement (basic_block cond_bb, basic_block middle_bb, /* We need to know which is the true edge and which is the false edge so that we know when to invert the condition below. */ extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge); - if (e1 == true_edge || e0 == false_edge) - std::swap (arg0, arg1); + + /* Forward the edges over the middle basic block. */ + if (true_edge->dest == middle_bb) + true_edge = EDGE_SUCC (true_edge->dest, 0); + if (false_edge->dest == middle_bb) + false_edge = EDGE_SUCC (false_edge->dest, 0); + + /* When THREEWAY_P then e1 will point to the edge of the final transition + from middle-bb to end. */ + if (true_edge == e0) + { + if (!threeway_p) + gcc_assert (false_edge == e1); + arg_true = arg0; + arg_false = arg1; + } + else + { + gcc_assert (false_edge == e0); + if (!threeway_p) + gcc_assert (true_edge == e1); + arg_true = arg1; + arg_false = arg0; + } tree type = TREE_TYPE (gimple_phi_result (phi)); result = gimple_simplify_phiopt (early_p, type, stmt, - arg0, arg1, + arg_true, arg_false, &seq); if (!result) return false;