From patchwork Fri Jul 14 20:55:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 120696 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp2748272vqm; Fri, 14 Jul 2023 13:56:51 -0700 (PDT) X-Google-Smtp-Source: APBJJlFe36h3SWYEyi6uc+8GHv7Y3yvCwq7iKFUn6+oH1zsBf7776K5fGyqnlAhXXxaCoMD5c9TC X-Received: by 2002:a17:907:8f0f:b0:993:ff4b:5a0c with SMTP id wg15-20020a1709078f0f00b00993ff4b5a0cmr4867102ejc.30.1689368210971; Fri, 14 Jul 2023 13:56:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689368210; cv=none; d=google.com; s=arc-20160816; b=GsN2LISfDHEmRhD6R2Rwou+mW1lV69SlwFI4jq7LcnSvvXPU7GBsiyKHReM6WG9Ten dBZcwFcJvqlh07cnpiDn7pQ64GTyXmCByWQR2eVGPM5Fdaiws9ei9Jn3lBQRzKAVQSIn ghNtPEL3T4Cv7hZRQuVd20w/qGH14Kmz316yBFtzVtabDlnFSH8nul8RAfg702rTBeL8 UOeV94OBajloXXFbFaY2StOop6nU3PFLo7uUfz+ealw9vdgHEA6c1xvka5qCVks6XFRt 1o3gDo7FC/7O/d38x9bwzNMWDLwcL92DgnxsnFUcc6pizlExr+oKDl6hkAHF7VjGcYTl O+5A== 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=4HTNlssa9FWrScB6yOlsIX49aJOpXIb7p08+9Tuiht8=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=uOhgteFoabt71LOgvborRz7ZTXjhtMYCIGV+1g7sU6WvxVX9a8UL35BCYQlf3WO1ul +SouKII4aU/lklEXbOEft/RUQqzgwbHodEqM5CDWrgzhQYXvq7JG1/eL1/LE+YHylGsS lLAp4uULcYZf/B10lVfrblq0UvEDIbbeCYiT6hpJo0P9BjLgAv6MF1dpqOzAu6RGcp08 ewVVfXaqCr5LK5W1p70cn0vxpG1eM2wErmHolehzEYBoV+gjnAd5Ox1qF1Uf6ttNprky OfuJpwD8LjkzS0LkmbyVYvUzXujw2FQvzMpIMNZBb56fn5LDclFbeioCYkR1aNUo4krp 6Yrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=x1PCQGPZ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j13-20020a170906050d00b0098dfdc3f2desi3968691eja.676.2023.07.14.13.56.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jul 2023 13:56:50 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=x1PCQGPZ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 A1A9B3858291 for ; Fri, 14 Jul 2023 20:56:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A1A9B3858291 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689368209; bh=4HTNlssa9FWrScB6yOlsIX49aJOpXIb7p08+9Tuiht8=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=x1PCQGPZllYc4yYFevRlE9tsQf1lEvJ2T7FVX8DTe1xk7oIzq6eWTgknSVdM1NKkU yMtYTeg0DhjBQNfcFeEkWxew/NM2WdsboKmZFPBLssCP/tzxUXmrJP/6cg0AY2gq+y edEj9y06/MVuUVSDQxpxiqFoesabMBsOKs04xfW8= 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 0624D3858CD1 for ; Fri, 14 Jul 2023 20:56:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0624D3858CD1 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 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) 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 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 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: 1771430961344999395 X-GMAIL-MSGID: 1771430961344999395 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) + } +}