From patchwork Sat Jul 22 23:26:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 124392 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1019676vqg; Sat, 22 Jul 2023 16:27:55 -0700 (PDT) X-Google-Smtp-Source: APBJJlHZ6Mur83CA9Zq56r8AGA3wGXq6xYDvHUoxpVOYgHttpf/fUVeEkcKZ3p7NGWOHyDB5X1sg X-Received: by 2002:a17:906:2dc:b0:99b:59bb:c4a4 with SMTP id 28-20020a17090602dc00b0099b59bbc4a4mr5711761ejk.60.1690068475197; Sat, 22 Jul 2023 16:27:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690068475; cv=none; d=google.com; s=arc-20160816; b=Pi98JKzB/+IaHDhcLgtdbZAx3oY4yArG8B2XNkN+XNe+g9nPf0J8PLc2x7v1TFpn6n TmpxXBi7vmvFpzDJxWSkbiNM26CmdgCY9dUKLpViE+SlTmPy9MUALUwABwzHAwTqs0oL A8zrqVuXmA99nEpLnXn5G8Qy28ukcTLdMWWKc73dXU++QnyVh01SNJymWMddUec/myBZ HLEqLleeKFd35byCwJodTm+fF2Rulpd3UOshDIvYDWgOeFQflZLIUvTRmNukNF626yWx 2cmTYsJCAEoF5Gj7PxTqZ05OofRpxRUYkbdXrADFMh1HieFmVgj7pL8bdab3Z5wBJEKs w28g== 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=K8qIB9cbAirkwN/sgtkJ/RflqUch2fVqWCBpnGb6if8=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=HACGJ98/UgpST8SIADgQkmNGPMOxSzyGaGT984yh0FiL3CpvCEcp6sKS85IIkzLUfi jH+uMLxKbrCIxKCiEKAdXEEyHh59ggl1a05Iqtlomsp2yf3tMFczga68Eaj0jnkMh/pA VzCCnr+Re/bu0w81eU3yb7hVt/I+vvapHARRbEukHrOQdEyOluXYoYJERsMk3GPcjEjy D7ExYmW96SV06xXE+AwgER1WHisrVqVD/J2xkPLxZ3W4Bd4LfoTuv9Lrp79RpUVti971 LbvZGNhl0y1upW6yzGBhzeKbHmrvbdjhqwGuocJqLMUJi3Wmm7jJ05wtu3pX60zieKQ9 mJKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=tjz0XxaT; 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 u1-20020a05640207c100b005218c27b17bsi4337834edy.499.2023.07.22.16.27.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Jul 2023 16:27:55 -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=tjz0XxaT; 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 228EF3858033 for ; Sat, 22 Jul 2023 23:27:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 228EF3858033 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690068474; bh=K8qIB9cbAirkwN/sgtkJ/RflqUch2fVqWCBpnGb6if8=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=tjz0XxaT39KYEuWejOFX3ACywfRJtsnPd00BUefKB8LhGsy4OmwbLBZTM+HBhluU3 RqqJFA7Q7Sy4jK9c9WB7V86W4XKsvkAVCBRo00afEGG60UXNXk4SObvG0a1NDsb5+m Dy1I8JUYpiWKLzkdyiOA61SMSVRzYpMb7N6Jk/xI= 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 EE25F3858D1E for ; Sat, 22 Jul 2023 23:27:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE25F3858D1E 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 36MLNAvP031286 for ; Sat, 22 Jul 2023 16:27:08 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3s0emkrwsa-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sat, 22 Jul 2023 16:27:08 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sat, 22 Jul 2023 16:27:05 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Sat, 22 Jul 2023 16:27:05 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.76.242.112]) by maili.marvell.com (Postfix) with ESMTP id 7352A3F70B2; Sat, 22 Jul 2023 16:27:04 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] Fix 100864: `(a&!b) | b` is not opimized to `a | b` for comparisons Date: Sat, 22 Jul 2023 16:26:49 -0700 Message-ID: <20230722232649.1617746-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: 8uBKr1mxekWMKi4YYmL9vQJ1QcC0Eufs X-Proofpoint-ORIG-GUID: 8uBKr1mxekWMKi4YYmL9vQJ1QcC0Eufs 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-22_12,2023-07-20_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: 1772165241214406664 X-GMAIL-MSGID: 1772165241214406664 This adds a special case of the `(a&~b) | b` pattern where `b` and `~b` are comparisons. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/100864 * match.pd ((~x & y) | x -> x | y): Add comparison variant. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/bitops-3.c: New test. --- gcc/match.pd | 17 +++++- gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c | 67 ++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c diff --git a/gcc/match.pd b/gcc/match.pd index bfd15d6cd4a..dd4a2df537d 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1928,7 +1928,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* (~x & y) | x -> x | y */ (simplify (bitop:c (rbitop:c (bit_not @0) @1) @0) - (bitop @0 @1))) + (bitop @0 @1)) + /* Similar but for comparisons which have been inverted already, + Note it is hard to simulate the inverted tcc_comparison due + NaNs; That is == and != are sometimes inversions and sometimes not. + So a double for loop is needed and then compare the inverse code + with the result of invert_tree_comparison is needed. + This works fine for vector compares as -1 and 0 are bitwise + inverses. */ + (for cmp (tcc_comparison) + (for icmp (tcc_comparison) + (simplify + (bitop:c (rbitop:c (icmp @0 @1) @2) (cmp@3 @0 @1)) + (with { enum tree_code ic = invert_tree_comparison + (cmp, HONOR_NANS (@0)); } + (if (ic == icmp) + (bitop @3 @2))))))) /* ((x | y) & z) | x -> (z & y) | x */ (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c b/gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c new file mode 100644 index 00000000000..68fff4edce9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-3.c @@ -0,0 +1,67 @@ +/* PR tree-optimization/100864 */ + +/* { dg-do run } */ +/* { dg-options "-O1 -fdump-tree-optimized-raw" } */ + +#define op_ne != +#define op_eq == +#define op_lt < +#define op_le <= +#define op_gt > +#define op_ge >= + +#define operators(t) \ +t(ne) \ +t(eq) \ +t(lt) \ +t(le) \ +t(gt) \ +t(ge) + +#define cmpfunc(v, op) \ +__attribute__((noipa)) \ +_Bool func_##op##_##v(v int a, v int b, v _Bool e) \ +{ \ + v _Bool c = (a op_##op b); \ + v _Bool d = !c; \ + return (e & d) | c; \ +} + +#define cmp_funcs(op) \ +cmpfunc(, op) \ +cmpfunc(volatile , op) + +operators(cmp_funcs) + +#define test(op) \ +if (func_##op##_ (a, b, e) != func_##op##_volatile (a, b, e)) \ + __builtin_abort(); + +int main() +{ + for(int a = -3; a <= 3; a++) + for(int b = -3; b <= 3; b++) + { + _Bool e = 0; + operators(test) + e = 1; + operators(test) + } + return 0; +} + +/* Check to make sure we optimize `(a&!b) | b` -> `a | b`. */ +/* There are 6 different comparison operators testing here. */ +/* bit_not_expr and bit_and_expr should show up for each one (volatile). */ +/* Each operator should show up twice + (except for `!=` which shows up 2*6 (each tester) + 2 (the 2 loops) extra = 16). */ +/* bit_ior_expr will show up for each operator twice (non-volatile and volatile). */ +/* { dg-final { scan-tree-dump-times "ne_expr," 16 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "eq_expr," 2 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "lt_expr," 2 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "le_expr," 2 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "gt_expr," 2 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "ge_expr," 2 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_not_expr," 6 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_and_expr," 6 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr," 12 "optimized"} } */ \ No newline at end of file