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 0624D3858CD1 for ; Fri, 14 Jul 2023 20:56:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0624D3858CD1 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 36EKpmr1026077 for ; Fri, 14 Jul 2023 13:56:01 -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=4HTNlssa9FWrScB6yOlsIX49aJOpXIb7p08+9Tuiht8=; b=SXxLdBzt6X0WOizVK/GHbHM5v6WLbIU45r6AQG/B1w/re4iA14TYNWJh3I5l7RzykAoW GMBk05fCOMIfftPBzXFi7WMgZKycOkgRTN9dxFO0p1K16mvo7DKgz8AZD+e0xPe4RoLU QbGJrj7yjr3aEJjum89l16wJUF+RwsjIiVxC4KcyeM0T+aFOYRoediGiNKoWueRkz1ol PTq3kjfNYbSjaAeEGQnj7mqFVFURXsWuOlYGXRAY8cpktlN5YILmxT8Rmev6L6Xsykd+ vjrKsOC7p4PAy8eRS9bIomTt9uhG8HE1nlF6Rcc5N1nnhmqRskdSIXcJU6SjpPLqgtTr 3Q== Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3rtptxbsry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 14 Jul 2023 13:56:00 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 14 Jul 2023 13:55:58 -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, 14 Jul 2023 13:55:58 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.76.242.112]) by maili.marvell.com (Postfix) with ESMTP id 3BA4D3F704D; Fri, 14 Jul 2023 13:55:58 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] Fix PR 110666: `(a != 2) == a` produces wrong code Date: Fri, 14 Jul 2023 13:55:42 -0700 Message-ID: <20230714205542.1131700-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-GUID: Ye3rjEi3eXpx9bEOU-et-TsnUc_OaRYT X-Proofpoint-ORIG-GUID: Ye3rjEi3eXpx9bEOU-et-TsnUc_OaRYT 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-14_10,2023-07-13_01,2023-05-22_02 X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,SCC_5_SHORT_WORD_LINES,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 List-Id: I had messed up the case where the outer operator is `==`. The check for the resulting should have been `==` and not `!=`. This patch fixes that and adds a full runtime testcase now for all cases to make sure it works. OK? Bootstrapped and tested on x86-64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/110666 * match.pd (A NEEQ (A NEEQ CST)): Fix Outer EQ case. gcc/testsuite/ChangeLog: PR tree-optimization/110666 * gcc.c-torture/execute/pr110666-1.c: New test. --- gcc/match.pd | 34 ++++++++----- .../gcc.c-torture/execute/pr110666-1.c | 51 +++++++++++++++++++ 2 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr110666-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 351d9285e92..88061fa4a6f 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -6431,8 +6431,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */ /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */ -/* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x != 0) */ -/* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x != 1) */ +/* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */ +/* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */ (for outer (ne eq) (for inner (ne eq) (simplify @@ -6443,23 +6443,29 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) bool innereq = inner == EQ_EXPR; bool outereq = outer == EQ_EXPR; } - (switch - (if (innereq ? cst0 : cst1) - { constant_boolean_node (!outereq, type); }) - (if (innereq ? cst1 : cst0) + (switch + (if (innereq ? cst0 : cst1) + { constant_boolean_node (!outereq, type); }) + (if (innereq ? cst1 : cst0) + (with { + tree utype = unsigned_type_for (TREE_TYPE (@0)); + tree ucst1 = build_one_cst (utype); + } + (if (!outereq) + (gt (convert:utype @0) { ucst1; }) + (le (convert:utype @0) { ucst1; }) + ) + ) + ) (with { - tree utype = unsigned_type_for (TREE_TYPE (@0)); - tree ucst1 = build_one_cst (utype); + tree value = build_int_cst (TREE_TYPE (@0), !innereq); } - (if (!outereq) - (gt (convert:utype @0) { ucst1; }) - (le (convert:utype @0) { ucst1; }) + (if (outereq) + (eq @0 { value; }) + (ne @0 { value; }) ) ) ) - (if (innereq) - (ne @0 { build_zero_cst (TREE_TYPE (@0)); })) - (ne @0 { build_one_cst (TREE_TYPE (@0)); })) ) ) ) diff --git a/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c b/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c new file mode 100644 index 00000000000..b22eb7781da --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr110666-1.c @@ -0,0 +1,51 @@ + +#define func_name(outer,inner,cst) outer##inner##_##cst +#define func_name_v(outer,inner,cst) outer##inner##_##cst##_v + +#define func_decl(outer,inner,cst) \ +int outer##inner##_##cst (int) __attribute__((noipa)); \ +int outer##inner##_##cst (int a) { \ + return (a op_##inner cst) op_##outer a; \ +} \ +int outer##inner##_##cst##_v (int) __attribute__((noipa)); \ +int outer##inner##_##cst##_v (volatile int a) { \ + return (a op_##inner cst) op_##outer a; \ +} + +#define functions_n(outer, inner) \ +func_decl(outer,inner,0) \ +func_decl(outer,inner,1) \ +func_decl(outer,inner,2) + +#define functions() \ +functions_n(eq,eq) \ +functions_n(eq,ne) \ +functions_n(ne,eq) \ +functions_n(ne,ne) + +#define op_ne != +#define op_eq == + +#define test(inner,outer,cst,arg) \ +func_name_v (inner,outer,cst)(arg) != func_name(inner,outer,cst)(arg) + +functions() + +#define tests_n(inner,outer,arg) \ +if (test(inner,outer,0,arg)) __builtin_abort(); \ +if (test(inner,outer,1,arg)) __builtin_abort(); \ +if (test(inner,outer,2,arg)) __builtin_abort(); + +#define tests(arg) \ +tests_n(eq,eq,arg) \ +tests_n(eq,ne,arg) \ +tests_n(ne,eq,arg) \ +tests_n(ne,ne,arg) + + +int main() +{ + for(int n = -1; n <= 2; n++) { + tests(n) + } +} -- 2.31.1