From patchwork Mon Jul 17 02:42:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 120997 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp870133vqt; Sun, 16 Jul 2023 19:43:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlEE90wl1FUlvavKPw1W9WZd4txMCdQTXku713x0Pge4/z5VnBsT5cFGHgKQaRLp08wyDtbo X-Received: by 2002:a17:906:3f4f:b0:993:f497:adbe with SMTP id f15-20020a1709063f4f00b00993f497adbemr11792418ejj.19.1689561837657; Sun, 16 Jul 2023 19:43:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689561837; cv=none; d=google.com; s=arc-20160816; b=mHAIf/FawBigd5fi4jrXeEsEbHkCmttjhIK1eIVvcJ6eFo8MN8hxhua4+S33N/Y+Wu acZkbwjwmrxEXR7qF2CBpa+Bq2LAfz/NgsupxFFol+MosvRHQX000UHKqFoFpVjzLDeK cWSjlDIhPaWR4EthuMN4K4aFiTEQ1Q1LOyvj+HxsiXjK2FC3s1xtP6cyYyzdd2yUTiZy W7F+gGpQHzt3hlq5sXNeFLaZ/G/TsTdlTZivA+fmhPWBfVxxcdSpDk31OPt9zg9tIRuE ZYH+rOqTb/1IsfzclNntDvWYIKOZ8rr4ywu1LdW/V5NqRGa8KtEMyT77ZSDe5j2CrsmC DoaQ== 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=7I/B9mO1NmDnGi3W1+MjH0qIsG2YvSBE9LKJpqGOQ9Y=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=s9KmYMfWimgUPFhz1BQZr7XvHnolz1ihjSA6rojPxnfI9ckzbI/JBB+j0GqlCE+bGu PiGEe+xzSEelqejxV7fncL2w+xwixn5YOzCrUEf8r7SqVYuco7wv4ELCnlWZGox1Uavt OKtQuFm4PebKE77PpyCpXPtZMaZkYxLqH/PL7lqgW1qHjC3kpsYBg5AblbU4MizZSEeJ DinojPMIY25x1ZNQtT/h3PEm5HRd6qa0yduB2EN5hppaxyA9DUjtK5TW7TZQtY10TBFG 59+Rm7+9s0aKQhaDaSKKJeB0ZhTmzLleLg8fkm5kY2Y2BC8u7GIi7X0q/wC35cP0vQ06 YMjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=A73cUaz0; 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 i25-20020a170906265900b00988479dc7f1si11412833ejc.764.2023.07.16.19.43.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jul 2023 19:43:57 -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=A73cUaz0; 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 8038D385841B for ; Mon, 17 Jul 2023 02:43:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8038D385841B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689561836; bh=7I/B9mO1NmDnGi3W1+MjH0qIsG2YvSBE9LKJpqGOQ9Y=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=A73cUaz0t03bsP/dtUb1nqIMKsU7OpJC9++9JI1B4cLXYqR/NU6EGrOojCDVDuzFU DVu8EkFv4DDswFKlj4vqSftyPLUcPgrlFu1GpRnbzDCGs6xsPzCTfLJQeUl6U8f5zR 1LmLsVWF+Oa5R2wyhVq4XUYmKpwUOKjgsddjwKg0= 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 4AC533858D1E for ; Mon, 17 Jul 2023 02:43:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4AC533858D1E 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 36H00SLt012207 for ; Sun, 16 Jul 2023 19:43:12 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3rus6dbp1u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 16 Jul 2023 19:43:12 -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; Sun, 16 Jul 2023 19:43:11 -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; Sun, 16 Jul 2023 19:43:11 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id ED0273F7063; Sun, 16 Jul 2023 19:43:10 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] PR 95923: More (boolean) bitop simplifications in match.pd Date: Sun, 16 Jul 2023 19:42:47 -0700 Message-ID: <20230717024247.1263484-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: uSq-gZ3VRqgaPS50oKQWNxQoIKWjPis9 X-Proofpoint-ORIG-GUID: uSq-gZ3VRqgaPS50oKQWNxQoIKWjPis9 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-07-17_01,2023-07-13_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: 1771633993714399254 X-GMAIL-MSGID: 1771633993714399254 This adds the boolean version of some of the simplifications that were added with r8-4395-ge268a77b59cb78. That are the following: (a | b) & (a == b) --> a & b a | (a == b) --> a | (b ^ 1) (a & b) | (a == b) --> a == b OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/95923 * match.pd ((a|b)&(a==b),a|(a==b),(a&b)|(a==b)): New transformation. gcc/testsuite/ChangeLog: PR tree-optimization/95923 * gcc.dg/tree-ssa/bitops-2.c: New test. * gcc.dg/tree-ssa/bool-checks-1.c: New test. --- gcc/match.pd | 15 +++++++ gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c | 41 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c | 22 ++++++++++ 3 files changed, 78 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 351d9285e92..5e812d804d0 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1226,11 +1226,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1))) (bit_and @0 @1)) +/* (a | b) & (a == b) --> a & b (boolean version of the above). */ +(simplify + (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1))) + (bit_and @0 @1)) + /* a | ~(a ^ b) --> a | ~b */ (simplify (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1))) (bit_ior @0 (bit_not @1))) +/* a | (a == b) --> a | (b^1) (boolean version of the above). */ +(simplify + (bit_ior:c @0 (nop_convert? (eq:c @0 @1))) + (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))) + /* (a | b) | (a &^ b) --> a | b */ (for op (bit_and bit_xor) (simplify @@ -1242,6 +1252,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1))) @2) +/* (a & b) | (a == b) --> a == b */ +(simplify + (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1))) + @2) + /* ~(~a & b) --> a | ~b */ (simplify (bit_not (bit_and:cs (bit_not @0) @1)) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c new file mode 100644 index 00000000000..d8128f3ca53 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-2.c @@ -0,0 +1,41 @@ +/* { dg-do run } */ +/* { dg-options "-O -fdump-tree-optimized-raw" } */ + +#define DECLS(n,VOL) \ +__attribute__((noinline,noclone)) \ +_Bool h##n(_Bool A,_Bool B){ \ + VOL _Bool C = A | B; \ + VOL _Bool D = A == B; \ + return C & D; \ +} \ +__attribute__((noinline,noclone)) \ +_Bool i##n(_Bool A,_Bool B){ \ + VOL _Bool C = A == B; \ + return A | C; \ +} \ +__attribute__((noinline,noclone)) \ +_Bool k##n(_Bool A,_Bool B){ \ + VOL _Bool C = A & B; \ + VOL _Bool D = A == B; \ + return C | D; \ +} \ + +DECLS(0,) +DECLS(1,volatile) + +int main(){ + for(int A = 0; A <= 1; ++A) + for(int B = 0; B <= 1; ++B) + { + if (h0 (A, B) != h1 (A, B)) __builtin_abort(); + if (i0 (A, B) != i1 (A, B)) __builtin_abort(); + if (k0 (A, B) != k1 (A, B)) __builtin_abort(); + } +} + +/* { dg-final { scan-tree-dump-times "bit_not_expr," 1 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_and_expr," 3 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr," 4 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "eq_expr," 4 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "ne_expr," 7 "optimized"} } */ +/* { dg-final { scan-tree-dump-not "bit_xor_expr," "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c new file mode 100644 index 00000000000..370a23aad3e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-checks-1.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized-raw" } */ +/* PR tree-optimization/95923 */ + +_Bool f(_Bool a, _Bool b) +{ + if (!a && !b) + return 0; + if (!a && b) + return 0; + if (a && !b) + return 0; + return 1; +} + +/* { dg-final { scan-tree-dump-times "bit_and_expr," 1 "optimized"} } */ +/* { dg-final { scan-tree-dump-not "bit_not_expr," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "bit_ior_expr," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "bit_xor_expr," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "eq_expr," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "ne_expr," "optimized"} } */ +/* { dg-final { scan-tree-dump-not "gimple_cond" "optimized"} } */