From patchwork Tue Jul 23 16:30:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1963974 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=MtFDJCq2; 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 4WT2gm66dVz1yYm for ; Wed, 24 Jul 2024 02:30:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 20CDF386482E for ; Tue, 23 Jul 2024 16:30:27 +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 [69.48.154.134]) by sourceware.org (Postfix) with ESMTPS id 9F5A03850203 for ; Tue, 23 Jul 2024 16:30:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9F5A03850203 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9F5A03850203 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=69.48.154.134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721752208; cv=none; b=KfkWajjiSo4RRPVvAbGOKdKBtwGBitEKszC/FvADr4HQMzjNaquuWm/ZQfMIWju5I5FBrOzOVHUI2fiSdVPzDgvrKxUj6kmNPLfYFAEh/AJzQtJWjX4Q77ScAEn3/maq3HqyNaUr5iPCe7dT1gO8mc2aSZyVcx1h1VYJFnRxVaU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721752208; c=relaxed/simple; bh=1hTWQkAgRwz7vl/lwUEMh1i4ZpEAxam767yP5NDsHYo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jF1sAwdlNlVzA8Vdgk3blWEG/yK00dxPuUaLA7VMmVI9Qo8dxLYJVBXiPoElWCfXZ3AtvPVOzQk39aXssfy0kXS/Ls+GoNowp/vHICVE3aWLMAZbzpjfi9HmW27STfD6070/U3VVQ3tZlN4ap6+H/3ysKt/Ib5lWF1F2KtwFfyo= ARC-Authentication-Results: i=1; server2.sourceware.org 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:To:From:Sender:Reply-To:Cc: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=kqiWLjSBWomKWVSGQNGsFQepWT8kahB7eCSHeaztVv0=; b=MtFDJCq2SYWyPN5mWtl+hn299+ t6I4+7e5DeLv/BCKO7zchrVPMrZs6QucQ2rhmYvHV4BlYUvdwDCW8Ev62D9YmWkUrdjEPpQXjJsaN LlexSwEnBCThjCB3JP/bGyEtvnwc88rULhWjwumOAyl7zBpUYn136CHQvokO6ehA+MhNqfJ/eyeL7 wSSxmakaQb6g3cXXDVSHmSeuvH24s91Jd586ONnAvvPC1uiytt4xakTlfcFcEFPOFnReXSomXSSMf ygYqRdUenL0Vg5lm1E27uSX3HW3BzBMeCyhTZToQ7lCbLziYvlpEV/LM5oEPMEAxSTl5+l3tlOkca FySKNA7g==; Received: from [185.62.158.67] (port=64978 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1sWIOr-0000000GsNq-2Pjv for gcc-patches@gcc.gnu.org; Tue, 23 Jul 2024 12:30:05 -0400 From: "Roger Sayle" To: Subject: [match.pd PATCH] Fold ctz(-x) as ctz(x). Date: Tue, 23 Jul 2024 17:30:02 +0100 Message-ID: <019801dadd1d$936d12c0$ba473840$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdrdHQ2MiHg9q106SritOzXJBzcUUg== 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.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org The subject line pretty much says it all; the count-trailing-zeros function of -X produces the same result as count-trailing-zeros of X. This transformation eliminates a negation which may potentially overflow with an equivalent expression that doesn't [much like the analogous abs(-X) simplification in match.pd]. Likewise, the undefined at zero remains undefined. I'd noticed this equivalence, which isn't mentioned in Hacker's Delight, investigating whether ranger's non_zero_bits can help determine whether an integer variable may be converted to a floating point type exactly (without raising FE_INEXACT), but it turns out this observation isn't novel, as (disappointingly) LLVM already performs this same folding. 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? 2024-07-23 Roger Sayle gcc/ChangeLog * match.pd (ctz (-X) => ctz (X)): New simplification. gcc/testsuite/ChangeLog * gcc.dg/fold-ctz-1.c: New test case. Thanks in advance, Roger diff --git a/gcc/match.pd b/gcc/match.pd index 6818856..d6d61eb 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -9056,6 +9056,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* CTZ simplifications. */ (for ctz (CTZ) + /* ctz (-x) => ctz (x). */ + (simplify + (ctz (nop_convert?@0 (negate @1))) + (with { tree t = TREE_TYPE (@0); } + (ctz (convert:t @1)))) (for op (ge gt le lt) cmp (eq eq ne ne) (simplify diff --git a/gcc/testsuite/gcc.dg/fold-ctz-1.c b/gcc/testsuite/gcc.dg/fold-ctz-1.c new file mode 100644 index 0000000..dcc444c --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-ctz-1.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int foo(int x) +{ + return __builtin_ctz (-x); +} + +/* { dg-final { scan-tree-dump-not "-x_" "optimized"} } */