From patchwork Thu Aug 10 22:51:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 134251 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp732947vqi; Thu, 10 Aug 2023 15:52:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGmNKMXJCak4L2jg6U8CnQtWgXrZtUDGqX/XzbRCWIGKKsJerz3oTwMwTFJGbjKfvPjt9bm X-Received: by 2002:a05:6402:1216:b0:523:41e1:705e with SMTP id c22-20020a056402121600b0052341e1705emr275342edw.21.1691707950137; Thu, 10 Aug 2023 15:52:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691707950; cv=none; d=google.com; s=arc-20160816; b=CF3V9a/emOxJju8BzxJMVWr8lnmto6ehwWZjeos4UpBpK3H2X2v1JJkCFwTyGmUivm 4o87UNKV86rWIGBEiOJ2GW6Rg9jvr3K5sL1JsPRmeG06fW78UcEtLRwHCvQYdSkhUudL rpk4XHG1TWxJWgA9vaQm/wjPbezzWMG/B7NBJYPJPv9rdgJwE+IFxw8ZsTwedjBOGpSX H8gzS8Z8Rxubxg8ENKeb0gWtWVJQpqR4+ZSX1Op1oij4mKH4mBy3bcR+Pda8vCAOtXgS ttrPfGnKN874d6aNi/9XoNZ4njH/Dcmx5FEhA908ZYfGVWKeKJsYWcBZht8CMbKntc8P GNnQ== 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=JoYYbpsul6skBPEUMloA3oEQJmsJUAayFoEyo6h1FJQ=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=qKVdl7uirbm2BmPCRmdGA006vCxc7autUkB5bJqPPxrNr3gm6Xjsa1+P3QM4Dzb9Y3 nK5OCUP8QdlSC9QYFJ5jPjJbWHhc8a/c5BVV4sbJV9myRHT5LbURg/ZzCZJJJbqyXWrQ 5m6TOS3PzRADf8nKXSlJtQzUMiAkXgdkglXoOwUpGL+181xBJKW3OCtuP2AlM9moK4Vx iM91NkPBoLTmspF0+TUKpyJSUgSnxqcp0uFlpEer1jlc8ZGk0weL3xb2C2BzgXBE7QHF DKiGemuARibu2jLnbbovsWvCB8TUroKInBfHYLllWuopj0hVAs8yZP69Aci41QZJsded bMPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ZWxC8fut; 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 z26-20020aa7d41a000000b005218c27b18csi2182258edq.498.2023.08.10.15.52.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 15:52:30 -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=ZWxC8fut; 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 DAD393857701 for ; Thu, 10 Aug 2023 22:52:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DAD393857701 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691707948; bh=JoYYbpsul6skBPEUMloA3oEQJmsJUAayFoEyo6h1FJQ=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ZWxC8futsYVAqyOWIkPL4j9OwmMN7EauA8ySPD6Ob0maQ2kYEDoYIvh//lDxYJgOA F2fmuLLSzTFJu4XpBc5ahZRVav8x7LlS1PqKFNX87E0F4e54WFw8TcnhSIjfYE+mHo Xv3XAB2snAN1FnTr6HS8PGKW9BnvBEq52MGs9LGY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 418E93858D32 for ; Thu, 10 Aug 2023 22:51:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 418E93858D32 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37AMj1SP014277 for ; Thu, 10 Aug 2023 15:51:37 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3sd8ya00kh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 10 Aug 2023 15:51:37 -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, 10 Aug 2023 15:51:35 -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, 10 Aug 2023 15:51:35 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id A5A393F7051; Thu, 10 Aug 2023 15:51:34 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCHv2] Fix PR 110954: wrong code with cmp | !cmp Date: Thu, 10 Aug 2023 15:51:21 -0700 Message-ID: <20230810225121.2732876-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: 5FljQsRLOFjQYq6x4ke8iQrgL9x-B9Xa X-Proofpoint-GUID: 5FljQsRLOFjQYq6x4ke8iQrgL9x-B9Xa X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-10_16,2023-08-10_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 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: 1773799367664302406 X-GMAIL-MSGID: 1773884355619785427 This was an oversight on my part forgetting that cmp will might have a different true value than all ones but will have a value of 1 in most cases. This means if we have `(f < 0) | !(f < 0)` we would optimize this to -1 rather than just 1. This is version 2 of the patch. Decided to go down a different route than just checking if the precission was 1 inside bitwise_inverted_equal_p. So instead bitwise_inverted_equal_p gets passed an argument that will be set if there was a comparison that was being compared and the user of bitwise_inverted_equal_p decides what needs to be done. In most uses of bitwise_inverted_equal_p, the check will be `!wascmp || element_precision (type) == 1` . But in the case of `a & ~a` and `a ^| ~a` we can handle the case of wascmp by using constant_boolean_node isntead. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR 110954 gcc/ChangeLog: * generic-match-head.cc (bitwise_inverted_equal_p): Add wascmp argument and set it accordingly. * gimple-match-head.cc (bitwise_inverted_equal_p): Add wascmp argument to the macro. (gimple_bitwise_inverted_equal_p): Add wascmp argument and set it accordingly. * match.pd (`a & ~a`, `a ^| ~a`): Update call to bitwise_inverted_equal_p and handle wascmp case. (`(~x | y) & x`, `(~x | y) & x`, `a?~t:t`): Update call to bitwise_inverted_equal_p and check to see if was !wascmp or if precision was 1. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/pr110954-1.c: New test. --- gcc/generic-match-head.cc | 4 +- gcc/gimple-match-head.cc | 8 ++-- gcc/match.pd | 41 +++++++++++-------- .../gcc.c-torture/execute/pr110954-1.c | 10 +++++ 4 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110954-1.c diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc index ddaf22f2179..f40a35c48a6 100644 --- a/gcc/generic-match-head.cc +++ b/gcc/generic-match-head.cc @@ -127,10 +127,11 @@ bitwise_equal_p (tree expr1, tree expr2) The types can differ through nop conversions. */ static inline bool -bitwise_inverted_equal_p (tree expr1, tree expr2) +bitwise_inverted_equal_p (tree expr1, tree expr2, bool &wascmp) { STRIP_NOPS (expr1); STRIP_NOPS (expr2); + wascmp = false; if (expr1 == expr2) return false; if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2))) @@ -150,6 +151,7 @@ bitwise_inverted_equal_p (tree expr1, tree expr2) { tree op10 = TREE_OPERAND (expr1, 0); tree op20 = TREE_OPERAND (expr2, 0); + wascmp = true; if (!operand_equal_p (op10, op20)) return false; tree op11 = TREE_OPERAND (expr1, 1); diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc index a097a494c39..ea6387a1099 100644 --- a/gcc/gimple-match-head.cc +++ b/gcc/gimple-match-head.cc @@ -267,8 +267,8 @@ gimple_bitwise_equal_p (tree expr1, tree expr2, tree (*valueize) (tree)) /* Return true if EXPR1 and EXPR2 have the bitwise opposite value, but not necessarily same type. The types can differ through nop conversions. */ -#define bitwise_inverted_equal_p(expr1, expr2) \ - gimple_bitwise_inverted_equal_p (expr1, expr2, valueize) +#define bitwise_inverted_equal_p(expr1, expr2, wascmp) \ + gimple_bitwise_inverted_equal_p (expr1, expr2, wascmp, valueize) bool gimple_bit_not_with_nop (tree, tree *, tree (*) (tree)); @@ -277,8 +277,9 @@ bool gimple_maybe_cmp (tree, tree *, tree (*) (tree)); /* Helper function for bitwise_equal_p macro. */ static inline bool -gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, tree (*valueize) (tree)) +gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, bool &wascmp, tree (*valueize) (tree)) { + wascmp = false; if (expr1 == expr2) return false; if (!tree_nop_conversion_p (TREE_TYPE (expr1), TREE_TYPE (expr2))) @@ -331,6 +332,7 @@ gimple_bitwise_inverted_equal_p (tree expr1, tree expr2, tree (*valueize) (tree) tree op21 = do_valueize (valueize, gimple_assign_rhs2 (a2)); if (!operand_equal_p (op11, op21)) return false; + wascmp = true; if (invert_tree_comparison (gimple_assign_rhs_code (a1), HONOR_NANS (op10)) == gimple_assign_rhs_code (a2)) diff --git a/gcc/match.pd b/gcc/match.pd index fc630b63563..6791060891d 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1179,9 +1179,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Simplify ~X & X as zero. */ (simplify (bit_and (convert? @0) (convert? @1)) - (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) - && bitwise_inverted_equal_p (@0, @1)) - { build_zero_cst (type); })) + (with { bool wascmp; } + (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) + && bitwise_inverted_equal_p (@0, @1, wascmp)) + { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); }))) /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */ (simplify @@ -1419,9 +1420,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (for op (bit_ior bit_xor) (simplify (op (convert? @0) (convert? @1)) - (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) - && bitwise_inverted_equal_p (@0, @1)) - (convert { build_all_ones_cst (TREE_TYPE (@0)); })))) + (with { bool wascmp; } + (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) + && bitwise_inverted_equal_p (@0, @1, wascmp)) + (convert + { wascmp + ? constant_boolean_node (true, type) + : build_all_ones_cst (TREE_TYPE (@0)); }))))) /* x ^ x -> 0 */ (simplify @@ -1969,8 +1974,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* (~x & y) | x -> x | y */ (simplify (bitop:c (rbitop:c @2 @1) @0) - (if (bitwise_inverted_equal_p (@0, @2)) - (bitop @0 @1)))) + (with { bool wascmp; } + (if (bitwise_inverted_equal_p (@0, @2, wascmp) + && (!wascmp || element_precision (type) == 1)) + (bitop @0 @1))))) /* ((x | y) & z) | x -> (z & y) | x */ (simplify @@ -6464,14 +6471,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* a?~t:t -> (-(a))^t */ (simplify (cond @0 @1 @2) - (if (INTEGRAL_TYPE_P (type) - && bitwise_inverted_equal_p (@1, @2)) - (with { - auto prec = TYPE_PRECISION (type); - auto unsign = TYPE_UNSIGNED (type); - tree inttype = build_nonstandard_integer_type (prec, unsign); - } - (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))) + (with { bool wascmp; } + (if (INTEGRAL_TYPE_P (type) + && bitwise_inverted_equal_p (@1, @2, wascmp) + && (!wascmp || element_precision (type) == 1)) + (with { + auto prec = TYPE_PRECISION (type); + auto unsign = TYPE_UNSIGNED (type); + tree inttype = build_nonstandard_integer_type (prec, unsign); + } + (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2))))))) #endif /* Simplify pointer equality compares using PTA. */ diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c b/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c new file mode 100644 index 00000000000..8aad758e10f --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr110954-1.c @@ -0,0 +1,10 @@ + +#define comparison (f < 0) +int main() { + int f = 0; + int d = comparison | !comparison; + if (d != 1) + __builtin_abort(); + return 0; +} +