From patchwork Sun May 14 03:12:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 93638 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp6085601vqo; Sat, 13 May 2023 20:14:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7kQA0n/xZpOoWCfP7q+Alm1ul04c+cuvwhD2zBNup9/s8j+Y3Q+MdJ+iH7sxWau7ttNAak X-Received: by 2002:a17:906:58d2:b0:956:fdb6:bc77 with SMTP id e18-20020a17090658d200b00956fdb6bc77mr31526091ejs.30.1684034077706; Sat, 13 May 2023 20:14:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684034077; cv=none; d=google.com; s=arc-20160816; b=JmH68VzNj0XquNB9B6SZhulK5TNkLzMD2hAo9EoBtLVXPxFeUea0csOXvg4EbUmiJq W65dA28d5gV/6Pc2a6oPbnSmR87hhx7jMCP2mtmv4sdowoz8BpVT6AUWoYXDwNG4BxM0 /Rt0rPCVN+HOw1NxIIBDZhecHPKWtF6G2Ah0+qZAq/KIkHnxR7fG9NnGq5/oQ9nUo6Cf isWlfNjuWbTpxaoSR7J8SyG2v3/OIh3gVnNZYmhOCk72Db18f9yVcAfeBzMol9ILesA+ IIz8eIwOJWThgv2t+xTDMvdlmlt1MTL4aexpfN9Y+84c26p6aOZ3k6PQUGx8+iZKH76U w7dQ== 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=CuCQeoHVMKezGZ1SfYbufR/CndI85Qqnod/7q+zxunw=; b=jRlpKtzwlMydtXOHAXPnWua+qrQ9Tz9fhLuEUjgpZ3hetnfAqPxD8DBrWoCdVaxe79 JAxYELlIUS/uSjdGX5Ttih36cob79fU9amu7jpFseO2iymsfi9a974c6jIdTC46ciia8 fxSax4xtrtgytOzCeurRc0QpA6g6di7S+yCznTHCEg1+RJhiKIs/juSIv5IfDvm/MeUp RZrA3zC2kpw4RxgoPNQsfP9zN8Bl67NjfOMNejecdnTv5Z9KEopNW9KnnI29DgbVBeKH Ua5v/ElClEbuVFfOXkS4M22TryMhHa1fW3lLYSYmUDcAuWNcTTHVZ6RuliROtcvddge1 +mmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=eIJQdS+s; 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id p19-20020a17090653d300b0095ece5cf770si9746674ejo.695.2023.05.13.20.14.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 May 2023 20:14:37 -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=eIJQdS+s; 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 5AE793858414 for ; Sun, 14 May 2023 03:14:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AE793858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684034076; bh=CuCQeoHVMKezGZ1SfYbufR/CndI85Qqnod/7q+zxunw=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=eIJQdS+sdotUQevR3uo1xVlsAw//9kb1p06+zW8mQrpaSLIuXYKuEMGaO+pNxMRGk Uy7IKS+LcMfxmHbzFR5+G3x4WGU3kd/ZPPlVwDZslLTCd1YsIto378VDF6GTKXgT9t 6rOj0pF6p3uHji4Qi/b4yZEVUj776E0BnPfdvhpU= 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 BF77C3858D28 for ; Sun, 14 May 2023 03:13:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BF77C3858D28 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 34E2pl5w025643 for ; Sat, 13 May 2023 20:13:47 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3qj7wnt0ck-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sat, 13 May 2023 20:13:47 -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; Sat, 13 May 2023 20:13:45 -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; Sat, 13 May 2023 20:13:45 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 59E973F7072; Sat, 13 May 2023 20:13:41 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: Add pattern for `signbit(x) ? x : -x` into abs (and swapped) Date: Sat, 13 May 2023 20:12:58 -0700 Message-ID: <20230514031258.1542461-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: G_Zc1CO5do96syiQ3gvt5Jp05RrXQpoP X-Proofpoint-ORIG-GUID: G_Zc1CO5do96syiQ3gvt5Jp05RrXQpoP X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-13_17,2023-05-05_01,2023-02-09_01 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_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.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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765837716689537504?= X-GMAIL-MSGID: =?utf-8?q?1765837716689537504?= This adds a simple pattern to match.pd for `signbit(x) ? x : -x` into abs. This can be done for all types even ones that honor signed zeros and NaNs because both signbit and - are considered only looking at/touching the sign bit of those types and does not trap either. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/109829 gcc/ChangeLog: * match.pd: Add pattern for `signbit(x) !=/== 0 ? x : -x`. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/abs-3.c: New test. * gcc.dg/tree-ssa/abs-4.c: New test. --- gcc/match.pd | 10 ++++++++++ gcc/testsuite/gcc.dg/tree-ssa/abs-3.c | 13 +++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/abs-4.c | 13 +++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/abs-3.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/abs-4.c diff --git a/gcc/match.pd b/gcc/match.pd index 26395880047..b025fb8facf 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -7439,6 +7439,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TREE_CODE (@0) != INTEGER_CST) (op @0 (ext @1 @2))))) +/* signbit(x) != 0 ? -x : x -> abs(x) + signbit(x) == 0 ? -x : x -> -abs(x) */ +(for sign (SIGNBIT) + (for neeq (ne eq) + (simplify + (cond (neeq (sign @0) integer_zerop) (negate @0) @0) + (if (neeq == NE_EXPR) + (abs @0) + (negate (abs @0)))))) + (simplify /* signbit(x) -> 0 if x is nonnegative. */ (SIGNBIT tree_expr_nonnegative_p@0) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/abs-3.c b/gcc/testsuite/gcc.dg/tree-ssa/abs-3.c new file mode 100644 index 00000000000..d2638e8f4c5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/abs-3.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +/* PR tree-optimization/109829 */ + +float abs_f(float x) { return __builtin_signbit(x) ? -x : x; } +double abs_d(double x) { return __builtin_signbit(x) ? -x : x; } +long double abs_ld(long double x) { return __builtin_signbit(x) ? -x : x; } + + +/* __builtin_signbit(x) ? -x : x. Should be convert into ABS_EXP */ +/* { dg-final { scan-tree-dump-not "signbit" "optimized"} } */ +/* { dg-final { scan-tree-dump-not "= -" "optimized"} } */ +/* { dg-final { scan-tree-dump-times "= ABS_EXPR" 3 "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/abs-4.c b/gcc/testsuite/gcc.dg/tree-ssa/abs-4.c new file mode 100644 index 00000000000..6197519faf7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/abs-4.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +/* PR tree-optimization/109829 */ + +float abs_f(float x) { return __builtin_signbit(x) ? x : -x; } +double abs_d(double x) { return __builtin_signbit(x) ? x : -x; } +long double abs_ld(long double x) { return __builtin_signbit(x) ? x : -x; } + + +/* __builtin_signbit(x) ? x : -x. Should be convert into - ABS_EXP */ +/* { dg-final { scan-tree-dump-not "signbit" "optimized"} } */ +/* { dg-final { scan-tree-dump-times "= ABS_EXPR" 3 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "= -" 3 "optimized"} } */