From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 53A193858D33 for ; Sat, 16 Sep 2023 05:49:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53A193858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=marvell.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=marvell.com 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 38G5iAoH009542 for ; Fri, 15 Sep 2023 22:49:31 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=3IfwpGq8OGnzFMIY27O0LT2Cv2Yvd587lOXdaM4GMUY=; b=fCq1VnMAVToZic7BeTgGBxtnVHy1BAIPZWVvA/eJBxorIzErH+vK3tCnTEPjiUQld6Wh sdSfeuxIBRQ2of8ku4ielHRqsTStTfHsfTkxsy5n4Zh23IJNIDojPIJZUZb8xz9uQ6E/ GHHyaEj1QNgf9fsoxkxbAl726Gy6Q6O2uzpKiR2rO6hXaF0XrbqEVAR2pB4UKMhqg6iM ge4gmfes+GhM3ir8uIcfFwOmjLtan3aoKHSCbywtPK7BUHq1e2Adl94irdm3q4uN+nuM aHGYWmBkEsGwa87HmyFdVE2aO13hvZ3IP0WPOK3fdK3BWsu3cbky4+IyPZ/Nskgy+k/T PQ== Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3t4nq6bfc8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 15 Sep 2023 22:49:31 -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; Fri, 15 Sep 2023 22:49:29 -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; Fri, 15 Sep 2023 22:49:29 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 0E7083F7040; Fri, 15 Sep 2023 22:49:28 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] MATCH: Add simplifications for `(a * zero_one) ==/!= CST` Date: Fri, 15 Sep 2023 22:49:20 -0700 Message-ID: <20230916054920.1653864-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-ORIG-GUID: 08pusBY5P_TeDdcoWAq5x7EWLOsQYe5j X-Proofpoint-GUID: 08pusBY5P_TeDdcoWAq5x7EWLOsQYe5j X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-15_20,2023-09-15_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 List-Id: Transforming `(a * b@[0,1]) != 0` into `((cast)b) & a != 0` will produce better code as a lot of the time b is defined by a comparison. Also since canonicalize `a & -zero_one` into `a * zero_one` we start to lose information when doing comparisons against 0. In the case of PR 110992, we lose that `a != 0` on the branch and then don't do a jump threading like we should. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/110992 gcc/ChangeLog: * match.pd (`a * zero_one !=/== CST`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp116.c: Update test to avoid the extra comparison. * gcc.c-torture/execute/pr110992-1.c: New test. * gcc.dg/tree-ssa/pr110992-1.c: New test. * gcc.dg/tree-ssa/pr110992-2.c: New test. --- gcc/match.pd | 15 +++++++ .../gcc.c-torture/execute/pr110992-1.c | 43 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c | 21 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c | 17 ++++++++ gcc/testsuite/gcc.dg/tree-ssa/vrp116.c | 2 +- 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110992-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c diff --git a/gcc/match.pd b/gcc/match.pd index 39c9c81966a..97405e6a5c3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2197,6 +2197,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (type)) (bit_and @0 @1))) +/* (a * b@[0,1]) == CST + -> + CST == 0 ? (a == CST | b == 0) : (a == CST & b != 0) + (a * b@[0,1]) != CST + -> + CST != 0 ? (a != CST | b == 0) : (a != CST & b != 0) */ +(for cmp (ne eq) + (simplify + (cmp (mult:cs @0 zero_one_valued_p@1) INTEGER_CST@2) + (if ((cmp == EQ_EXPR) ^ (wi::to_wide (@2) != 0)) + (bit_ior + (cmp @0 @2) + (convert (bit_xor @1 { build_one_cst (TREE_TYPE (@1)); }))) + (bit_and (cmp @0 @2) (convert @1))))) + (for cmp (tcc_comparison) icmp (inverted_tcc_comparison) /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */ diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110992-1.c b/gcc/testsuite/gcc.c-torture/execute/pr110992-1.c new file mode 100644 index 00000000000..edb7eb75ef2 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr110992-1.c @@ -0,0 +1,43 @@ +#define CST 5 +#define OP != +#define op_eq == +#define op_ne != + +#define function(vol,op, cst) \ +__attribute__((noipa)) \ +_Bool func_##op##_##cst##_##vol(vol int a, vol _Bool b) \ +{ \ + vol int d = (a * b); \ + return d op_##op cst; \ +} + +#define funcdefs(op,cst) \ +function(,op,cst) \ +function(volatile,op,cst) + +#define funcs(f) \ +f(eq,0) \ +f(eq,1) \ +f(eq,5) \ +f(ne,0) \ +f(ne,1) \ +f(ne,5) + +funcs(funcdefs) + +#define test(op,cst) \ +do { \ + if(func_##op##_##cst##_(a,b) != func_##op##_##cst##_volatile(a,b))\ + __builtin_abort(); \ +} while(0); + +int main(void) +{ + for(int a = -10; a <= 10; a++) + { + _Bool b = 0; + funcs(test) + b = 1; + funcs(test) + } +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c new file mode 100644 index 00000000000..825fd63f84c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-1.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ +static unsigned b; +static short c = 4; +void foo(void); +static short(a)(short d, short g) { return d * g; } +void e(); +static char f() { + b = 0; + return 0; +} +int main() { + int h = b; + if ((short)(a(c && e, 65535) & h)) { + foo(); + h || f(); + } +} + +/* There should be no calls to foo left. */ +/* { dg-final { scan-tree-dump-not " foo " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c new file mode 100644 index 00000000000..6082949a218 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110992-2.c @@ -0,0 +1,17 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -fdump-tree-optimized" } */ +static unsigned b; +static short c = 4; +void foo(void); +int main() { + int h = b; + int d = c != 0; + if (h*d) { + foo(); + if (!h) b = 20; + } +} + + +/* There should be no calls to foo left. */ +/* { dg-final { scan-tree-dump-not " foo " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c index 9e68a774aee..16b31e320a0 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp116.c @@ -6,7 +6,7 @@ f (int m1, int m2, int c) { int d = m1 > m2; int e = d * c; - return e ? m1 : m2; + return e; } /* { dg-final { scan-tree-dump-times "\\? c_\[0-9\]\\(D\\) : 0" 1 "optimized" } } */ -- 2.31.1