From patchwork Thu Aug 31 17:24:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 137328 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp389144vqu; Thu, 31 Aug 2023 10:25:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGl5oCTWzdZ384NKbslXhaqS4dIopWZ3gBwwiC6zksumjMHMwfzOpR24yGbfO+ZLdKIdyy4 X-Received: by 2002:a17:906:31d2:b0:987:4e89:577f with SMTP id f18-20020a17090631d200b009874e89577fmr2906ejf.24.1693502715107; Thu, 31 Aug 2023 10:25:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693502715; cv=none; d=google.com; s=arc-20160816; b=E2pdxUzp0YQjMB/0RA5gx4Fgt0ecXAwxH560JYRa6KR6mH6lNS/8t3az/Rf4QJpG8B 4wJ8U6Qeh0S1DkqKQA2kAF8IC4VkaTpQRStXY4ApAMHWTm9FJpTFErOi/9xFELUCLV0F Yem0HFYY45OWKgg81b/pqIdvHW+OZNxyMt5DMhkkqSqk6q7PZdDwwKqDyBCNyctRjzne tOXDswfRK+QfA4+Kr2RlX54GhNBoy5y5hiyyQHl2+8cU3/Pe3RRxSYMy6MAlWDFiheHG lb6jSD0T/SXinhf66AjL+QBEILcyzmGmYbwZchKoddVqRNAJmsoBtw5aie9/L0PicvCI tiwQ== 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=/tbW4/au0lH39oNdxbeBylh1Shk/ElA9Jla9XlvEMI0=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=iOSrB2S5iaWSVRyYJE51WhE0PfL/V90Ak397UXU5cVlkRg1UjyUm09U6pUMV6v3u7Z IEGbzdlMlr9f0/p6UEAxsQvSKJtLrKiiB0gCWnDVeR5ed5oaF76K2zWxuv8gv7H7upHD UEnIS+9sNhTQlk01B1iBhMHUADdEKiB55Aqn55Petdnq5UeyheqoRpE1+XTkcJkvxN6u +haU7Bfa5sEcQ8RgCReXCug807m+sezNMEROl6DUr+4t/Q7BmxkSbBj1th1lJucaPZ0+ rzL32YAg5LUjWDMjbhPiKCuxwkMj2VenFS/GlKlqqkd+GzEFIZbrIL4rWWZcPHmB5Y+X LJ1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iiU1KPHr; 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 f15-20020a1709067f8f00b00987ae307f0dsi1122278ejr.587.2023.08.31.10.25.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 10:25:15 -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=iiU1KPHr; 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 C14473858C3A for ; Thu, 31 Aug 2023 17:25:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C14473858C3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693502713; bh=/tbW4/au0lH39oNdxbeBylh1Shk/ElA9Jla9XlvEMI0=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=iiU1KPHrq6zk1hj6gd1Z1vuStLGeANuNvqMcoLTrftlSdqU7kkjm4/tkphtcFgtOq HC+EXulOgIOOv6A498JMWuk+HrFDo5SpDSaE8E8a3UZDMnWYuKmJPLZQxUl5/aseuM qLkhY5I3WohL7wPe02Gvaa6NXxoVoesNHV0VFkTM= 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 9950A3858D28 for ; Thu, 31 Aug 2023 17:24:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9950A3858D28 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 37VCRFaN016951 for ; Thu, 31 Aug 2023 10:24:21 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3sttvb96ey-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 31 Aug 2023 10:24:20 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Thu, 31 Aug 2023 10:24:19 -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; Thu, 31 Aug 2023 10:24:19 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 0E6973F7083; Thu, 31 Aug 2023 10:24:18 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH [PR19832]: Optimize some `(a != b) ? a OP b : c` Date: Thu, 31 Aug 2023 10:24:10 -0700 Message-ID: <20230831172410.731800-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: OBSAkzjBPYiOL4yZipRseaJDg264weE8 X-Proofpoint-GUID: OBSAkzjBPYiOL4yZipRseaJDg264weE8 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-08-31_14,2023-08-31_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, PLING_QUERY, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 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: 1775766303095915254 X-GMAIL-MSGID: 1775766303095915254 This patch adds the following match patterns to optimize these: /* (a != b) ? (a - b) : 0 -> (a - b) */ /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */ /* (a != b) ? (a & b) : a -> (a & b) */ /* (a != b) ? (a | b) : a -> (a | b) */ /* (a != b) ? min(a,b) : a -> min(a,b) */ /* (a != b) ? max(a,b) : a -> max(a,b) */ /* (a != b) ? (a * b) : (a * a) -> (a * b) */ /* (a != b) ? (a + b) : (a + a) -> (a + b) */ /* (a != b) ? (a + b) : (2 * a) -> (a + b) */ Note currently only integer types (include vector types) are handled. Floating point types can be added later on. OK? Bootstrapped and tested on x86_64-linux-gnu. The first pattern had still shows up in GCC in cse.c's preferable function which was the original motivation for this patch. PR tree-optimization/19832 gcc/ChangeLog: * match.pd: Add pattern to optimize `(a != b) ? a OP b : c`. gcc/testsuite/ChangeLog: * g++.dg/opt/vectcond-1.C: New test. * gcc.dg/tree-ssa/phi-opt-same-1.c: New test. --- gcc/match.pd | 31 ++++++++++ gcc/testsuite/g++.dg/opt/vectcond-1.C | 57 ++++++++++++++++++ .../gcc.dg/tree-ssa/phi-opt-same-1.c | 60 +++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 gcc/testsuite/g++.dg/opt/vectcond-1.C create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c diff --git a/gcc/match.pd b/gcc/match.pd index c01362ee359..487a7e38719 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5261,6 +5261,37 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (convert @c0)))))))) #endif +(for cnd (cond vec_cond) + /* (a != b) ? (a - b) : 0 -> (a - b) */ + (simplify + (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop) + @2) + /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */ + (simplify + (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop) + @2) + /* (a != b) ? (a & b) : a -> (a & b) */ + /* (a != b) ? (a | b) : a -> (a | b) */ + /* (a != b) ? min(a,b) : a -> min(a,b) */ + /* (a != b) ? max(a,b) : a -> max(a,b) */ + (for op (bit_and bit_ior min max) + (simplify + (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0) + @2)) + /* (a != b) ? (a * b) : (a * a) -> (a * b) */ + /* (a != b) ? (a + b) : (a + a) -> (a + b) */ + (for op (mult plus) + (simplify + (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0)) + (if (ANY_INTEGRAL_TYPE_P (type)) + @2))) + /* (a != b) ? (a + b) : (2 * a) -> (a + b) */ + (simplify + (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3)) + (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2) + @2)) +) + /* These was part of minmax phiopt. */ /* Optimize (a CMP b) ? minmax : minmax to minmax, c> */ diff --git a/gcc/testsuite/g++.dg/opt/vectcond-1.C b/gcc/testsuite/g++.dg/opt/vectcond-1.C new file mode 100644 index 00000000000..3877ad11414 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/vectcond-1.C @@ -0,0 +1,57 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ccp1 -fdump-tree-optimized" } */ +/* This is the vector version of these optimizations. */ +/* PR tree-optimization/19832 */ + +#define vector __attribute__((vector_size(sizeof(unsigned)*2))) + +static inline vector int max_(vector int a, vector int b) +{ + return (a > b)? a : b; +} +static inline vector int min_(vector int a, vector int b) +{ + return (a < b) ? a : b; +} + +vector int f_minus(vector int a, vector int b) +{ + return (a != b) ? a - b : (a - a); +} +vector int f_xor(vector int a, vector int b) +{ + return (a != b) ? a ^ b : (a ^ a); +} + +vector int f_ior(vector int a, vector int b) +{ + return (a != b) ? a | b : (a | a); +} +vector int f_and(vector int a, vector int b) +{ + return (a != b) ? a & b : (a & a); +} +vector int f_max(vector int a, vector int b) +{ + return (a != b) ? max_(a, b) : max_(a, a); +} +vector int f_min(vector int a, vector int b) +{ + return (a != b) ? min_(a, b) : min_(a, a); +} +vector int f_mult(vector int a, vector int b) +{ + return (a != b) ? a * b : (a * a); +} +vector int f_plus(vector int a, vector int b) +{ + return (a != b) ? a + b : (a + a); +} +vector int f_plus_alt(vector int a, vector int b) +{ + return (a != b) ? a + b : (a * 2); +} + +/* All of the above function's VEC_COND_EXPR should have been optimized away. */ +/* { dg-final { scan-tree-dump-not "VEC_COND_EXPR " "ccp1" } } */ +/* { dg-final { scan-tree-dump-not "VEC_COND_EXPR " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c new file mode 100644 index 00000000000..24e757b9b9f --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c @@ -0,0 +1,60 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-phiopt1 -fdump-tree-optimized" } */ +/* PR tree-optimization/19832 */ + +static inline int max_(int a, int b) +{ + if (a > b) return a; + return b; +} +static inline int min_(int a, int b) +{ + if (a < b) return a; + return b; +} + +int f_minus(int a, int b) +{ + if (a != b) return a - b; + return a - a; +} +int f_xor(int a, int b) +{ + if (a != b) return a ^ b; + return a ^ a; +} + +int f_ior(int a, int b) +{ + if (a != b) return a | b; + return a | a; +} +int f_and(int a, int b) +{ + if (a != b) return a & b; + return a & a; +} +int f_max(int a, int b) +{ + if (a != b) return max_(a,b); + return max_(a,a); +} +int f_min(int a, int b) +{ + if (a != b) return min_(a,b); + return min_(a,a); +} +int f_mult(int a, int b) +{ + if (a != b) return a * b; + return a * a; +} +int f_plus(int a, int b) +{ + if (a != b) return a + b; + return a + a; +} + +/* All of the above function's if should have been optimized away even in phiopt1. */ +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if " "optimized" } } */