From patchwork Tue Aug 13 04:30:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1971732 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=quicinc.com header.i=@quicinc.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=SYHTdoGX; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wjdk81p6qz1yXl for ; Tue, 13 Aug 2024 14:31:08 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B5BE03858431 for ; Tue, 13 Aug 2024 04:31:05 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by sourceware.org (Postfix) with ESMTPS id 0F08E3858D20 for ; Tue, 13 Aug 2024 04:30:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0F08E3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0F08E3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723523440; cv=none; b=jnnFTdzpDBqd8ltcPcJQKYCcCkkg528wrwlgHPnsdYUXg2YPmX9sqKzUk/LwQupxhgWnQiPw04Pu3v0w2ewPCOwECePn1947Q8lIeZ/OrniVOQ4Z8Xw2e4D9AAOUAPw+/x+pVjuEcr9vzN0/8Agvj0Cjc+L+j8J8v2NSOHr7wJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723523440; c=relaxed/simple; bh=2XIqTQFEv8t/X6C9hXk8xfNPASybyW//1L+SqE794kE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Qllzzq5Sqcy6GsQuUgIbfnzQSDirHkiK0J/ZvqnS5eNC16eha174lsmyMLRSEdjGD+SZLbeW9FT1EWSLcbOdHKqcsl6FJ+UKtlThK4ZaonPGgJchpeZqh7VNccKLIu2GyIr0CC6UX+KneorpiTOr2P7qJ5IDaKBWCSwBaYYwn5g= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 47D4R9rw001040 for ; Tue, 13 Aug 2024 04:30:37 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= lBMhZPTdrZ4pTkAerJwmnT5Lm28hvn4vi4C2tNw+nco=; b=SYHTdoGXCaJMh8C1 2Y0hY+pA4ckcnEgjM0GK4H5Rf6yT0p0OizT2ENVWc6i1eJuir4sT9c3R+33PWRkn ct+JnE/HslWqc/rJjYbsRl40WRGe8lDUUo80D8dzQh1iNu9n8Kx98wyBQSxX+Kup aGQOK0OJUz8sOeW6v/nKxlo47ITzk/vpHv6k7kJVdYOID33fLKafAQZEITLk+6Bx gbbCAWPXB0sZM9dnTl+BSvcQaFtIV/stqCgjXpAxsYT4KoVSIvYaKC47127Hd5OG TVkCs8e75MogAPxd0bVDZGs1yVJOgwiaBmfXTyZlHbmFPzJiKzev8p41F5lFislM mrQ7aA== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 40x3et63jw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 13 Aug 2024 04:30:36 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA04.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 47D4UZji012047 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 13 Aug 2024 04:30:35 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Mon, 12 Aug 2024 21:30:35 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH 3/3] Match: Add pattern for `(a ? b : 0) | (a ? 0 : c)` into `a ? b : c` [PR103660] Date: Mon, 12 Aug 2024 21:30:23 -0700 Message-ID: <20240813043023.3685386-3-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240813043023.3685386-1-quic_apinski@quicinc.com> References: <20240813043023.3685386-1-quic_apinski@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: qAcGEFREXvC8HhC2ROdN30oaLclCjop2 X-Proofpoint-ORIG-GUID: qAcGEFREXvC8HhC2ROdN30oaLclCjop2 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-12_12,2024-08-12_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 bulkscore=0 suspectscore=0 impostorscore=0 phishscore=0 clxscore=1015 mlxlogscore=970 lowpriorityscore=0 priorityscore=1501 spamscore=0 adultscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2408130030 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org This adds a pattern to convert `(a ? b : 0) | (a ? 0 : c)` into `a ? b : c` which is simplier. It adds both for cond and vec_cond; even though vec_cond is handled via a different pattern currently but requires extra steps for matching so this should be slightly faster. Also handle it for xor and plus too since those can be handled the same way. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/103660 gcc/ChangeLog: * match.pd (`(a ? b : 0) | (a ? 0 : c)`): New pattern. gcc/testsuite/ChangeLog: * g++.dg/tree-ssa/pr103660-4.C: New test. * gcc.dg/tree-ssa/pr103660-4.c: New test. Signed-off-by: Andrew Pinski --- gcc/match.pd | 10 +++++ gcc/testsuite/g++.dg/tree-ssa/pr103660-4.C | 35 ++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr103660-4.c | 43 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr103660-4.C create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr103660-4.c diff --git a/gcc/match.pd b/gcc/match.pd index b43ceb6def0..65a3aae2243 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2339,6 +2339,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (type)) (bit_and @0 @1))) +/* Fold `(a ? b : 0) | (a ? 0 : c)` into (a ? b : c). + Handle also ^ and + in replacement of `|`. */ +(for cnd (cond vec_cond) + (for op (bit_ior bit_xor plus) + (simplify + (op:c + (cnd:s @0 @00 integer_zerop) + (cnd:s @0 integer_zerop @01)) + (cnd @0 @00 @01)))) + (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/g++.dg/tree-ssa/pr103660-4.C b/gcc/testsuite/g++.dg/tree-ssa/pr103660-4.C new file mode 100644 index 00000000000..47727f86e24 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr103660-4.C @@ -0,0 +1,35 @@ +/* PR tree-optimization/103660 */ +/* Vector type version. */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-forwprop2-raw -Wno-psabi" } */ + +typedef int v4si __attribute((__vector_size__(4 * sizeof(int)))); +#define funcs(OP,n) \ +v4si min_##n(v4si a, v4si b) { \ + v4si t = {0,0,0,0}; \ + v4si X = a < b ? a : t; \ + v4si Y = a < b ? t : b; \ + return (X OP Y); \ +} \ +v4si f_##n(v4si a, v4si b, \ + v4si c, v4si d) { \ + v4si t = {0,0,0,0}; \ + v4si X = a < b ? c : t; \ + v4si Y = a < b ? t : d; \ + return (X OP Y); \ +} + + +funcs(|, ior) +funcs(^, xor) +funcs(+, plus) + +/* min_ior/min_xor/min_plus should produce min or `a < b ? a : b` depending on if the target + supports min on the vector type or not. */ +/* f_ior/f_xor/f_plus should produce (a < b) ? c : d */ +/* { dg-final { scan-tree-dump-not "bit_xor_expr, " "forwprop2" } } */ +/* { dg-final { scan-tree-dump-not "bit_ior_expr, " "forwprop2" } } */ +/* { dg-final { scan-tree-dump-not "plus_expr, " "forwprop2" } } */ +/* { dg-final { scan-tree-dump-not "bit_ior_expr, " "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "(?:lt_expr|min_expr), " 6 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "(?:vec_cond_expr|min_expr), " 6 "forwprop2" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr103660-4.c b/gcc/testsuite/gcc.dg/tree-ssa/pr103660-4.c new file mode 100644 index 00000000000..26c956fdcec --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr103660-4.c @@ -0,0 +1,43 @@ +/* PR tree-optimization/103660 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fgimple -fdump-tree-forwprop1-raw" } */ + +#define funcs(OP,n) \ +__GIMPLE() \ +int min_##n(int a, int b) { \ + _Bool X; \ + int t; \ + int t1; \ + int t2; \ + X = a < b; \ + t1 = X ? a : 0; \ + t2 = X ? 0 : b; \ + t = t1 OP t2; \ + return t; \ +} \ +__GIMPLE() \ +int f_##n(int a, int b, int c, \ + int d) { \ + _Bool X; \ + int t; \ + int t1; \ + int t2; \ + X = a < b; \ + t1 = X ? c : 0; \ + t2 = X ? 0 : d; \ + t = t1 OP t2; \ + return t; \ +} + +funcs(|, ior) +funcs(^, xor) +funcs(+, plus) + +/* min_i/min_ioror/min_plus should produce min */ +/* f_xor/f_ior/f_plus should produce (a < b) ? c : d */ +/* { dg-final { scan-tree-dump-not "bit_xor_expr, " "forwprop1" } } */ +/* { dg-final { scan-tree-dump-not "bit_ior_expr, " "forwprop1" } } */ +/* { dg-final { scan-tree-dump-not "plus_expr, " "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "min_expr, " 3 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "lt_expr, " 3 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "cond_expr, " 3 "forwprop1" } } */