From patchwork Sun Jan 1 15:55:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 37954 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp3817323wrt; Sun, 1 Jan 2023 07:56:03 -0800 (PST) X-Google-Smtp-Source: AMrXdXuHhDY0rHzs1bR6WNV4XXbYHMvREP2PT3Bq0T3QlZMR/54frhU7spZ1ErAsqbN0VtHzdNsY X-Received: by 2002:a17:906:171b:b0:7e8:ced8:cb9b with SMTP id c27-20020a170906171b00b007e8ced8cb9bmr32542013eje.42.1672588563508; Sun, 01 Jan 2023 07:56:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672588563; cv=none; d=google.com; s=arc-20160816; b=xBEb/F/GUTI5n8SljwBJkZJJqzuXfpvZIeyJc1BRbObjgSw7qet/J0iwxnTn2vehVK nILpuPTbX6edyXom/W4DkHDDW5iYSDSnNLr8xO8uO7NWQ64YR6wLInkTX9aWtn7yZ5vu T9I2V6T/r5FhGqDA6aNoOIrgBSBy7pqqt62KK5z0QncFEAwJbZxZVK5nXwaWJA6D9qSL 2NfaYkEYMvdgUoz3vvDpnY1XTDlwtzX/SHPEz8ZPpaKJrN7XvNSbVd4YL32Nc49ukRKJ R4eNVaSw4RFAMTVno1sgqrFItuaDhqDI6osQfz5wNk+OCfuorCYDF6i3obCGWLzHtyAi m0lQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-language:thread-index :mime-version:message-id:date:subject:cc:to:from:dkim-signature :delivered-to; bh=Hkswn6OkbeSo1Pij49/iqMhlEcU6ShsUnhFS0zjNdD8=; b=BBabQ8kFYY0cw0Q5qTCFPqDs0z0LF1k7hIbkt8zkrx6oHIUoY8/gyVBoDUrkha9pNa 5iyzVUjqm0QhylZjT/sh2ursg/eREBiCy4UVSnhXCmFFJKtF4MiOHKa1WaBvTOL8iVAO emUmD8UUgDXVP1jXaMgnLpQ5XKBTsu5w8EZ9muB0BQXHPlSUcYL6ruTQ7hCZZQg8H97c WxJNL+pRnnW+GHrGnHyMuCjfGjZiBlCx984akmFh/WsjOWhF098Rsri6fAyXPLpDadwy akggiDkE8oVCZa59q+bFqLL1LR4qn6jsAS/U99+yxLiWvhirY7rBHl/j3VqkkbKrYZ5q jY0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=sZ9PvlhG; 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" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hv9-20020a17090760c900b007adcc8fb7a0si23892302ejc.399.2023.01.01.07.56.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Jan 2023 07:56:03 -0800 (PST) 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=fail header.i=@nextmovesoftware.com header.s=default header.b=sZ9PvlhG; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3AC0D3857B93 for ; Sun, 1 Jan 2023 15:55:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id BBD623858D1E for ; Sun, 1 Jan 2023 15:55:33 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Hkswn6OkbeSo1Pij49/iqMhlEcU6ShsUnhFS0zjNdD8=; b=sZ9PvlhGssCNXmj970AJrGjfU2 5yrDlOm237Wu3BYzSWgMmwhUOTzuv2WD0KIgsIdQCHZeC3uVuE6X2zIXXBJAsx1qr23m5hNSljpI9 ROHLZKKG32SUZ4bzkjC4m+GHM51IcIZZleYHl7JSvw+GmBRLDbs5+S04lYu3CCBY/Py6ftI0N1+Px kETSZlFtlzky+ah08SMlVmPAiY/nXqJMg1emLXOpiv7MkDh0c9jeu9K1IxaLxzrGp9J28H/2xLraU FCKObm+RvpjUorjnu1lsdqX3jBAtPrqSoCcYPIKXqFSZNVPp4fAurBjAaMCTl16SVCAUxtJwuQNUA YexThreQ==; Received: from [185.62.158.67] (port=59270 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pC0gN-0006QL-Bx; Sun, 01 Jan 2023 10:55:31 -0500 From: "Roger Sayle" To: "'GCC Patches'" Cc: "'Segher Boessenkool'" Subject: [PATCH] Fix RTL simplifications of FFS, POPCOUNT and PARITY. Date: Sun, 1 Jan 2023 15:55:26 -0000 Message-ID: <002e01d91df9$79df2670$6d9d7350$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adkd9/i0dLBeRmvBRJ+alVvfufOlXw== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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?1753836225754630768?= X-GMAIL-MSGID: =?utf-8?q?1753836225754630768?= In 2011, the rtl.texi documentation was updated to reflect that the modes of the RTX unary operations FFS, POPCOUNT and PARITY must match those of their operands. Unfortunately, some of the transformations in simplify-rtx.cc predate this tightening of RTL semantics, and have not (until now) been updated/fixed. i.e. The POPCOUNT and PARITY optimizations were "correct" when I originally added them back in 2007. Segher requested that I split this piece out from a fix for PR 106594 in https://gcc.gnu.org/pipermail/gcc-patches/2022-September/601501.html This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32}, with no new failures. Ok for mainline? 2023-01-01 Roger Sayle gcc/ChangeLog * gcc/simplify-rtx.cc (simplify_unary_operation_1) : Avoid generating FFS with mismatched operand and result modes, by using an explicit SIGN_EXTEND/ZERO_EXTEND instead. : Likewise, for POPCOUNT of ZERO_EXTEND. : Likewise, for PARITY of {ZERO,SIGN}_EXTEND. Thanks in advance, Roger diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index fc0d6c3..698ca6e 100644 --- a/gcc/simplify-rtx.cc +++ b/gcc/simplify-rtx.cc @@ -1404,22 +1404,32 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, break; case FFS: - /* (ffs (*_extend )) = (ffs ) */ + /* (ffs (*_extend )) = (*_extend (ffs )). */ if (GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND) - return simplify_gen_unary (FFS, mode, XEXP (op, 0), - GET_MODE (XEXP (op, 0))); + { + temp = simplify_gen_unary (FFS, GET_MODE (XEXP (op, 0)), + XEXP (op, 0), GET_MODE (XEXP (op, 0))); + return simplify_gen_unary (GET_CODE (op), mode, temp, + GET_MODE (temp)); + } break; case POPCOUNT: switch (GET_CODE (op)) { case BSWAP: - case ZERO_EXTEND: - /* (popcount (zero_extend )) = (popcount ) */ + /* (popcount (bswap )) = (popcount ). */ return simplify_gen_unary (POPCOUNT, mode, XEXP (op, 0), GET_MODE (XEXP (op, 0))); + case ZERO_EXTEND: + /* (popcount (zero_extend )) = (zero_extend (popcount )). */ + temp = simplify_gen_unary (POPCOUNT, GET_MODE (XEXP (op, 0)), + XEXP (op, 0), GET_MODE (XEXP (op, 0))); + return simplify_gen_unary (ZERO_EXTEND, mode, temp, + GET_MODE (temp)); + case ROTATE: case ROTATERT: /* Rotations don't affect popcount. */ @@ -1438,11 +1448,16 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, { case NOT: case BSWAP: - case ZERO_EXTEND: - case SIGN_EXTEND: return simplify_gen_unary (PARITY, mode, XEXP (op, 0), GET_MODE (XEXP (op, 0))); + case ZERO_EXTEND: + case SIGN_EXTEND: + temp = simplify_gen_unary (PARITY, GET_MODE (XEXP (op, 0)), + XEXP (op, 0), GET_MODE (XEXP (op, 0))); + return simplify_gen_unary (GET_CODE (op), mode, temp, + GET_MODE (temp)); + case ROTATE: case ROTATERT: /* Rotations don't affect parity. */