From patchwork Sat Sep 2 07:00:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 137420 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp29767vqo; Sat, 2 Sep 2023 00:01:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFg/yeQwngcYILEr7fcbHeuZ0V+6Gy3ucU58tgybVXGQ32KIdqI9QGP5mcmLTsx4iLZP6nO X-Received: by 2002:a17:906:5346:b0:9a1:d67c:b4f2 with SMTP id j6-20020a170906534600b009a1d67cb4f2mr2890743ejo.77.1693638063994; Sat, 02 Sep 2023 00:01:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693638063; cv=none; d=google.com; s=arc-20160816; b=Lp83/RXl3D+Ls6PWOcaiiv9UrxwzBeY9uqDYswNrYfcPOQrxaaa4364WeeO/cyKxX6 hbbDeH1sdoUlpSbkszRZJ5/NrFLId4gPU7bTBUS7REY/fxrgKfHjWN+WI2UCMSOUDOBt RAvhfBLKarzWced/H5oc2xn3Pr2o7w5vcdNiFhvIVl0yuAHR4FIsjs56oslJMz9qQRz4 FGk6uDw/KqcI0UN31OGxMwpohlDf1ZPrciIaZYVZVh612c+17wpx3/kulvf8Awj4PXjs NDCqA2s2WUzN453vp8n0x9pcFrWF9wpOrO2VTRq3VVO+uby6I+DIqgy1n62lZvKi8cjZ WZbA== 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=M+b4qKC24NUeQk5yI9+CrOcP7YK/spFUiA1Grpy2bH8=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=lRSuRaKJbM93ROfrsJIGwdMjChBcVoe76BJnpR6cjvEUyt2P8xaZ8xCj+CZOg4wrMX er8tbUwtu2wsggoFHtJt+wTt0BNefTflYp9Wgc1s84UXmAoP7VEbeY4RMPLfgMqYJ0KA 25lWKQ+LIHBJ1fY40S5mka4qQuSaizA7dLamNRwnGWnI9pO4i584eyjSWcn188v4llQx OWP4xRYqBV3l2mqpcmkOlUpBy666Vrrj9anxwY9x7wrEYzaB6DQj7e2edkvPoh0/jYAW QIMZacqXKD/q1jN59MizzPc8dsGupgEKGLUiNq3B+HL3ZeIChDdYi9SaQF48lFs3S9o2 tY/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=cktrokY4; 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 us11-20020a170906bfcb00b0099b6e6c095csi3465363ejb.698.2023.09.02.00.01.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Sep 2023 00:01:03 -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=cktrokY4; 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 A4D363858419 for ; Sat, 2 Sep 2023 07:01:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4D363858419 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693638062; bh=M+b4qKC24NUeQk5yI9+CrOcP7YK/spFUiA1Grpy2bH8=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=cktrokY4YQCjS7UFlnfEh0sIvIssjJzaoaKJqlEUI5TC29gzcwCLuVruTUT5FHf/5 VuF8GbGWLsDvFh6vSmDTjSI+PGphDji06eXVfjVsJVYinftGTgtk0p8lcuF6jBloGO oXfOFTSKYIepoovSwFSc1p0bUvY68D0U01R71gfc= 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 520073858D20 for ; Sat, 2 Sep 2023 07:00:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 520073858D20 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 3824Lk66025508 for ; Sat, 2 Sep 2023 00:00:16 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3sucjvue51-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sat, 02 Sep 2023 00:00:16 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sat, 2 Sep 2023 00:00:14 -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, 2 Sep 2023 00:00:14 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 4CC063F7083; Sat, 2 Sep 2023 00:00:14 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: `(nop_convert)-(convert)a` into -(convert)a if we are converting from something smaller Date: Sat, 2 Sep 2023 00:00:05 -0700 Message-ID: <20230902070005.824764-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: POzwHe1j79zVbnLwLTB9o9IdOEPT9zVG X-Proofpoint-ORIG-GUID: POzwHe1j79zVbnLwLTB9o9IdOEPT9zVG 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-09-02_04,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, 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: 1775908226255149941 X-GMAIL-MSGID: 1775908226255149941 This allows removal of one conversion and in the case of booleans, might be able to remove the negate and the other conversion later on. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/107137 gcc/ChangeLog: * match.pd (`(nop_convert)-(convert)a`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/neg-cast-2.c: New test. * gcc.dg/tree-ssa/neg-cast-3.c: New test. --- gcc/match.pd | 11 +++++++++++ gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c | 20 ++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c | 15 +++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c diff --git a/gcc/match.pd b/gcc/match.pd index 487a7e38719..3efc971f7f6 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -959,6 +959,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) #endif )))) +/* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var) + if var is smaller in precision. + This is always safe for both doing the negative in signed or unsigned + as the value for undefined will not show up. */ +(simplify + (convert (negate:s@1 (convert:s @0))) + (if (INTEGRAL_TYPE_P (type) + && tree_nop_conversion_p (type, TREE_TYPE (@1)) + && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))) + (negate (convert @0)))) + (for op (negate abs) /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ (for coss (COS COSH) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c new file mode 100644 index 00000000000..c1d5066cd4a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-2.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-fre1 -fdump-tree-optimized" } */ +/* part of PR tree-optimization/108397 */ + +long long +foo (unsigned char o) +{ + unsigned long long t1 = -(long long) (o == 0); + unsigned long long t2 = -(long long) (t1 != 0); + unsigned long long t3 = -(long long) (t1 <= t2); + return t3; +} + +/* Should be able to optimize this down to just `return -1;` during fre1. */ +/* { dg-final { scan-tree-dump "return -1;" "fre1" } } */ +/* FRE does not remove all dead statements so a few negate expressions are left behind. */ +/* { dg-final { scan-tree-dump-not " -\[^1\]" "fre1" { xfail *-*-* } } } */ + +/* { dg-final { scan-tree-dump "return -1;" "optimized" } } */ +/* { dg-final { scan-tree-dump-not " - " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c new file mode 100644 index 00000000000..7b23ca85d1f --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/neg-cast-3.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-forwprop1 -fdump-tree-optimized" } */ +/* PR tree-optimization/107137 */ + +unsigned f(_Bool a) +{ + int t = a; + t = -t; + return t; +} + +/* There should be no cast to int at all. */ +/* Forwprop1 does not remove all of the statements. */ +/* { dg-final { scan-tree-dump-not "\\\(int\\\)" "forwprop1" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-not "\\\(int\\\)" "optimized" } } */