From patchwork Thu Aug 3 16:40: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: 130733 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp1271449vqx; Thu, 3 Aug 2023 09:41:43 -0700 (PDT) X-Google-Smtp-Source: APBJJlHWXZvZ6IdRYW5uygsXGX+1hRXSIQ4OufXdxIPHjmLXbtvaeTznzrsW7iZHNJ72XWM3stga X-Received: by 2002:aa7:c589:0:b0:522:2f8c:8953 with SMTP id g9-20020aa7c589000000b005222f8c8953mr7368494edq.39.1691080903038; Thu, 03 Aug 2023 09:41:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691080903; cv=none; d=google.com; s=arc-20160816; b=bve8SMsLURkGrXtJAhH34Io9c/cN9jEZ+/rwBUPvG2snR/jjIdm3IRE6vaCeaTt7tu 8Kwtvrgjes0dqaI+y65gORGw0ey2I2RAkmyEBOLjabzW60cR9Hu6U9ZRlfXm8l0KRvgp A+52yag53mi6xIBcCZOfIURxXnCCXugLrskCTLJAN6PHSPw0ou305ujY9oHlHFnJ6nty rdgk9bfrB2k9PGdG2N/fJW+AVTwyIlOZSLH2AM+3axuTeoY+oyDRrqeJLQvXv5eP2lt+ bnEHool/eQZKLaB7YaO+5wlIm+TF2jo9iGmPRS16LbB84E41tfyQZeo+KxaegKZ7VUaE mR8w== 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=AEeNQjIaSDGe7rPtKVZyX6Fo+9f/8ztyu9fnE0qsIho=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=g8KzgzVQDIiqTAfcK74RqTf7rn9swd0jK/pb1Fgua1+aI03uXqu4W+1CBLRY5uIUVe 3BySQzUvdOvxf2ZekSC3U5rCjAuF4ap6gglUhveYFO5zeRuu2eLqsguZLeuV4WzEObyx +ootZsEEoad7egrRYNhN4E56RaA3HFiPYd83LmhMAtlFc34uwgwD7bNZa+vgjK/QQwU+ hrtpwuExJETgijMBouZ98372SbUezjxC+4eeACCx1w0mtD/jrIGHS9vAzit6HG1id1Vb uQcEAANYOPbL8gQdLcuXqnH+FQZxuFSQoeO+zSUlhK40PnzAKIzni7FDrR55Mx0w6bHu mP8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=feBYTPBc; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id f18-20020a50ee92000000b005227b6ff1c3si41983edr.75.2023.08.03.09.41.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 09:41:43 -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=feBYTPBc; 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 013623858D38 for ; Thu, 3 Aug 2023 16:41:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 013623858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691080902; bh=AEeNQjIaSDGe7rPtKVZyX6Fo+9f/8ztyu9fnE0qsIho=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=feBYTPBcYAr4edtdLdCV4ofxAOq7RT9p+r68kagOsl3pJWgeWF+hR83etPEWwIy8n 87t+6WRmZORLChh2TS4jU+xVBaGge6506mt/vlsJL61oBkjfXIqRiOeakEPAi0Vuif Lpe1f8ksJvPxTo+NJPtat0/e6bn+ItuxC0JzYX3w= 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 6679F3858D1E for ; Thu, 3 Aug 2023 16:40:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6679F3858D1E 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 373GXmKo000972 for ; Thu, 3 Aug 2023 09:40:58 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3s86xra0bk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 03 Aug 2023 09:40:58 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 3 Aug 2023 09:40:56 -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, 3 Aug 2023 09:40:56 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 7763E3F7040; Thu, 3 Aug 2023 09:40:56 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCHv2] Fix PR 110874: infinite loop in gimple_bitwise_inverted_equal_p with fre Date: Thu, 3 Aug 2023 09:40:38 -0700 Message-ID: <20230803164038.2306774-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: W-fu_EqAhuq6wmqFO9-82fRnVzdYZmGQ X-Proofpoint-GUID: W-fu_EqAhuq6wmqFO9-82fRnVzdYZmGQ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-03_17,2023-08-03_01,2023-05-22_02 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_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: 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: INBOX X-GMAIL-THRID: 1773173574005407199 X-GMAIL-MSGID: 1773226849069366813 This changes gimple_bitwise_inverted_equal_p to use a 2 different match patterns to try to match bit_not wrapped with a possible nop_convert and a comparison also wrapped with a possible nop_convert. This is to avoid being recursive. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/110874 * gimple-match-head.cc (gimple_bit_not_with_nop): New declaration. (gimple_maybe_cmp): Likewise. (gimple_bitwise_inverted_equal_p): Rewrite to use gimple_bit_not_with_nop and gimple_maybe_cmp instead of being recursive. * match.pd (bit_not_with_nop): New match pattern. (maybe_cmp): Likewise. gcc/testsuite/ChangeLog: PR tree-optimization/110874 * gcc.c-torture/compile/pr110874-a.c: New test. --- gcc/gimple-match-head.cc | 87 ++++++++++--------- gcc/match.pd | 17 ++++ .../gcc.c-torture/compile/pr110874-a.c | 17 ++++ 3 files changed, 79 insertions(+), 42 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr110874-a.c diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc index b1e96304d7c..a097a494c39 100644 --- a/gcc/gimple-match-head.cc +++ b/gcc/gimple-match-head.cc @@ -270,6 +270,10 @@ gimple_bitwise_equal_p (tree expr1, tree expr2, tree (*valueize) (tree)) #define bitwise_inverted_equal_p(expr1, expr2) \ gimple_bitwise_inverted_equal_p (expr1, expr2, valueize) + +bool gimple_bit_not_with_nop (tree, tree *, tree (*) (tree)); +bool gimple_maybe_cmp (tree, tree *, tree (*) (tree)); + /* Helper function for bitwise_equal_p macro. */ static inline bool @@ -285,52 +289,51 @@ gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, tree (*valueize) (tree) return false; tree other; - if (gimple_nop_convert (expr1, &other, valueize) - && gimple_bitwise_inverted_equal_p (other, expr2, valueize)) - return true; - - if (gimple_nop_convert (expr2, &other, valueize) - && gimple_bitwise_inverted_equal_p (expr1, other, valueize)) - return true; - - if (TREE_CODE (expr1) != SSA_NAME - || TREE_CODE (expr2) != SSA_NAME) - return false; - - gimple *d1 = get_def (valueize, expr1); - gassign *a1 = safe_dyn_cast (d1); - gimple *d2 = get_def (valueize, expr2); - gassign *a2 = safe_dyn_cast (d2); - if (a1 - && gimple_assign_rhs_code (a1) == BIT_NOT_EXPR - && gimple_bitwise_equal_p (do_valueize (valueize, - gimple_assign_rhs1 (a1)), - expr2, valueize)) + /* Try if EXPR1 was defined as ~EXPR2. */ + if (gimple_bit_not_with_nop (expr1, &other, valueize)) + { + if (operand_equal_p (other, expr2, 0)) return true; - if (a2 - && gimple_assign_rhs_code (a2) == BIT_NOT_EXPR - && gimple_bitwise_equal_p (expr1, - do_valueize (valueize, - gimple_assign_rhs1 (a2)), - valueize)) + tree expr4; + if (gimple_nop_convert (expr2, &expr4, valueize) + && operand_equal_p (other, expr4, 0)) return true; - - if (a1 && a2 - && TREE_CODE_CLASS (gimple_assign_rhs_code (a1)) == tcc_comparison - && TREE_CODE_CLASS (gimple_assign_rhs_code (a2)) == tcc_comparison) + } + /* Try if EXPR2 was defined as ~EXPR1. */ + if (gimple_bit_not_with_nop (expr2, &other, valueize)) { - tree op10 = do_valueize (valueize, gimple_assign_rhs1 (a1)); - tree op20 = do_valueize (valueize, gimple_assign_rhs1 (a2)); - if (!operand_equal_p (op10, op20)) - return false; - tree op11 = do_valueize (valueize, gimple_assign_rhs2 (a1)); - tree op21 = do_valueize (valueize, gimple_assign_rhs2 (a2)); - if (!operand_equal_p (op11, op21)) - return false; - if (invert_tree_comparison (gimple_assign_rhs_code (a1), - HONOR_NANS (op10)) - == gimple_assign_rhs_code (a2)) + if (operand_equal_p (other, expr1, 0)) + return true; + tree expr3; + if (gimple_nop_convert (expr1, &expr3, valueize) + && operand_equal_p (other, expr3, 0)) return true; } + + /* If neither are defined by BIT_NOT, try to see if + both are defined by comparisons and see if they are + complementary (inversion) of each other. */ + tree newexpr1, newexpr2; + if (!gimple_maybe_cmp (expr1, &newexpr1, valueize)) + return false; + if (!gimple_maybe_cmp (expr2, &newexpr2, valueize)) + return false; + + gimple *d1 = get_def (valueize, newexpr1); + gassign *a1 = dyn_cast (d1); + gimple *d2 = get_def (valueize, newexpr2); + gassign *a2 = dyn_cast (d2); + tree op10 = do_valueize (valueize, gimple_assign_rhs1 (a1)); + tree op20 = do_valueize (valueize, gimple_assign_rhs1 (a2)); + if (!operand_equal_p (op10, op20)) + return false; + tree op11 = do_valueize (valueize, gimple_assign_rhs2 (a1)); + tree op21 = do_valueize (valueize, gimple_assign_rhs2 (a2)); + if (!operand_equal_p (op11, op21)) + return false; + if (invert_tree_comparison (gimple_assign_rhs_code (a1), + HONOR_NANS (op10)) + == gimple_assign_rhs_code (a2)) + return true; return false; } diff --git a/gcc/match.pd b/gcc/match.pd index 53e622bf28f..50dd653c778 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -155,6 +155,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))) && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) +#if GIMPLE +/* These are used by gimple_bitwise_inverted_equal_p to simplify + detection of BIT_NOT and comparisons. */ +(match (bit_not_with_nop @0) + (bit_not @0)) +(match (bit_not_with_nop @0) + (convert (bit_not @0)) + (if (tree_nop_conversion_p (type, TREE_TYPE (@0))))) +(for cmp (tcc_comparison) + (match (maybe_cmp @0) + (cmp@0 @1 @2)) + (match (maybe_cmp @0) + (convert (cmp@0 @1 @2)) + (if (tree_nop_conversion_p (type, TREE_TYPE (@0))))) +) +#endif + /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR ABSU_EXPR returns unsigned absolute value of the operand and the operand of the ABSU_EXPR will have the corresponding signed type. */ diff --git a/gcc/testsuite/gcc.c-torture/compile/pr110874-a.c b/gcc/testsuite/gcc.c-torture/compile/pr110874-a.c new file mode 100644 index 00000000000..b314410a892 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr110874-a.c @@ -0,0 +1,17 @@ +struct S1 { + unsigned f0; +}; +static int g_161; +void func_109(unsigned g_227, unsigned t) { + struct S1 l_178; + int l_160 = 0x1FAE99D5L; + int *l_230[] = {&l_160}; + if (l_160) { + for (l_178.f0 = -7; l_178.f0;) { + ++g_227; + break; + } + (g_161) = g_227; + } + (g_161) &= t; +}