Message ID | 01c401d8a666$d48c9e50$7da5daf0$@nextmovesoftware.com |
---|---|
State | New, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp2924667pxt; Tue, 2 Aug 2022 04:56:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t4N4FzUcu3CYyLomX2Hb9scurbnIe1mnRAP+sSMcRVZF7I2GH9sMZDpudDaHN7fg7IhIj4 X-Received: by 2002:a17:907:948e:b0:72f:b536:57 with SMTP id dm14-20020a170907948e00b0072fb5360057mr15756056ejc.491.1659441395706; Tue, 02 Aug 2022 04:56:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659441395; cv=none; d=google.com; s=arc-20160816; b=mRblmGt9PlvYk1bYwRhhJRzXyqvXPcPfzS8xgMnUBUoZCG2lyhJwkkV/Yp9PCjZY9j g4KGWLVTO7JJst6FTRhmcBpb6YLne8iX9TU4dsI7bFHnFdSukcxKI/yJVV4SST7OiCZR vzTkdHWmuOk30WcyNFHw6SBg3Tg3GNQqhj+HXv8a3ZoKq7NIfLKlwcNGeVHddv7WOmfn rG3kE5bo0+/Mt+e3fpGkBYL+UAhgx2Xwnl8BhEzSwa4JWaJCcdOw/8XfWINRC8y+TSMz rGM8GdTD9+Usfvq6zeL+60VslrLWnVf5XpixxJiEjVgASgpeWo9nQ3Lz0G6iFmSNMvmb nzYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-language:thread-index :mime-version:message-id:date:subject:in-reply-to:references:to:from :dkim-signature:dmarc-filter:delivered-to; bh=JugxZ4w69FxWm7P8JJMM5caSPBlf3MM5BBxRK+00OU4=; b=ZwlKD8USZfy1L3H0CwSN+/9hYRmJCVOmkvUiTMEBcscVqQmBqx0KLt4wknaCEsnxE0 wm4mBw+FccFRtkJRAq/9PrNcRy8CcfBsyY5/m5ZUUv+lU4pZXrVy/NZc+Mf2z2I+9E1Q 18xlt3+Tl4nE6sLl4P4E9K5aZLHYW/s5CeUjQDtsZRXnjj1wSirkb2icnmRDwfMfkkEA zIgJNGzY6Us17LEbcEnZ3AOuT7A+96pGiO4M2hqx1HJMEGKAa6hR5ex/mX2kllzf1Rl7 YfbBj/A+R5HFK1zI2PDaoW4KG8k5Obr91EDDTZ82CI68m2wzRckDYwvI1OHzM/RCQQzQ fzvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=SyAp2yXM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id m5-20020a17090672c500b007262b057924si858742ejl.900.2022.08.02.04.56.35 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Aug 2022 04:56:35 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=SyAp2yXM; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 2FFEB384B0D5 for <ouuuleilei@gmail.com>; Tue, 2 Aug 2022 11:56:25 +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 C72E03856DE8 for <gcc-patches@gcc.gnu.org>; Tue, 2 Aug 2022 11:55:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C72E03856DE8 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com 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:In-Reply-To:References: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:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=JugxZ4w69FxWm7P8JJMM5caSPBlf3MM5BBxRK+00OU4=; b=SyAp2yXMWzBuKvCGsYqF65nCft B9d28Inq8JQ7UOoXzBc1yetceUSNsv8SKB9SkeVQikiKC/uEIRdt9DFih+mEody2v4fSQMH0U6V3X NGZWwiB3yniagykd8vTMT+mbaz6Pm4qNRgHJA7rEnrXfmY1YP8QGyxtaQ4BBam9L9EM4j6G0b3m3W NHRDFFgO2si2r9SgRjBBbvVjKOAO8JMQ1vMsQ3YOMGoWv6KYnOX33icoURw0m//xutzLnYqxeACJd VUtgeqepCvatEBa/hpCIKokkc5Ar39Ji6F1gZ6Ot4KO+PMyNsXx/MvWDu6SgXgksRUlXdhxrr7qPp asw26HCA==; Received: from host86-169-41-119.range86-169.btcentralplus.com ([86.169.41.119]:54332 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from <roger@nextmovesoftware.com>) id 1oIqVC-00049a-Q1; Tue, 02 Aug 2022 07:55:59 -0400 From: "Roger Sayle" <roger@nextmovesoftware.com> To: "'GCC Patches'" <gcc-patches@gcc.gnu.org> References: <009501d8a1be$b6199e20$224cda60$@nextmovesoftware.com> <mpto7x3q928.fsf@arm.com> In-Reply-To: <mpto7x3q928.fsf@arm.com> Subject: [PATCH take #2] Some additional zero-extension related optimizations in simplify-rtx. Date: Tue, 2 Aug 2022 12:55:56 +0100 Message-ID: <01c401d8a666$d48c9e50$7da5daf0$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_01C5_01D8A66F.36537750" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdimZEr8OquuAI/HSburP1uyCP2bMA== 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=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Cc: 'Richard Sandiford' <richard.sandiford@arm.com>, 'Segher Boessenkool' <segher@kernel.crashing.org> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739513582239946658?= X-GMAIL-MSGID: =?utf-8?q?1740050420850120051?= |
Series |
[take,#2] Some additional zero-extension related optimizations in simplify-rtx.
|
|
Commit Message
Roger Sayle
Aug. 2, 2022, 11:55 a.m. UTC
Many thanks to Segher and Richard for pointing out that my removal of optimizations of ABS(ABS(x)) and ABS(FFS(x)) in the original version of this patch was incorrect, and my assumption that these would be subsumed by val_signbit_known_clear_p was mistaken. That the tests for ABS and FFS looked out of place, was not an indication that they were not required, but that we were missing simplifications for the related SS_ABS, PARITY, POPCOUNT, CLRSB, CLZ and CTZ etc. To make up for this mistake, in this revised patch I've not only restored the tests for ABS and FFS, but also added the many sibling RTX codes that I'd also expect to see optimized here, such as ABS(PARITY(x)). 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? 2022-08-02 Roger Sayle <roger@nextmovesoftware.com> Segher Boessenkool <segher@kernel.crashing.org> Richard Sandiford <richard.sandiford@arm.com> gcc/ChangeLog * simplify_rtx.cc (simplify_unary_operation_1) <ABS>: Add optimizations for CLRSB, PARITY, POPCOUNT, SS_ABS, CLZ, CTZ and LSHIFTRT that are all positive to complement the existing FFS and (idempotent) ABS simplifications. <SIGN_EXTEND>: Canonicalize SIGN_EXTEND to ZERO_EXTEND when val_signbit_known_clear_p is true of the operand. Simplify sign extensions of SUBREG truncations of operands that are already suitably (zero) extended. <ZERO_EXTEND>: Simplify zero extensions of SUBREG truncations of operands that are already suitably zero extended. Thanks in advance, Roger -- > -----Original Message----- > From: Richard Sandiford <richard.sandiford@arm.com> > Sent: 02 August 2022 10:39 > To: Roger Sayle <roger@nextmovesoftware.com> > Cc: gcc-patches@gcc.gnu.org; 'Segher Boessenkool' > <segher@kernel.crashing.org> > Subject: Re: [PATCH] Some additional zero-extension related optimizations in > simplify-rtx. > > "Roger Sayle" <roger@nextmovesoftware.com> writes: > > This patch implements some additional zero-extension and > > sign-extension related optimizations in simplify-rtx.cc. The original > > motivation comes from PR rtl-optimization/71775, where in comment #2 > Andrew Pinski sees: > > > > Failed to match this instruction: > > (set (reg:DI 88 [ _1 ]) > > (sign_extend:DI (subreg:SI (ctz:DI (reg/v:DI 86 [ x ])) 0))) > > > > On many platforms the result of DImode CTZ is constrained to be a > > small unsigned integer (between 0 and 64), hence the truncation to > > 32-bits (using a SUBREG) and the following sign extension back to > > 64-bits are effectively a no-op, so the above should ideally (often) > > be simplified to "(set (reg:DI 88) (ctz:DI (reg/v:DI 86 [ x ]))". > > > > To implement this, and some closely related transformations, we build > > upon the existing val_signbit_known_clear_p predicate. In the first > > chunk, nonzero_bits knows that FFS and ABS can't leave the sign-bit > > bit set, so the simplification of of ABS (ABS (x)) and ABS (FFS (x)) > > can itself be simplified. > > I think I misunderstood, but just in case: RTL ABS is well-defined for the minimum > integer (giving back the minimum integer), so we can't assume that ABS leaves > the sign bit clear. > > Thanks, > Richard > > > The second transformation is that we can canonicalized SIGN_EXTEND to > > ZERO_EXTEND (as in the PR 71775 case above) when the operand's > > sign-bit is known to be clear. The final two chunks are for > > SIGN_EXTEND of a truncating SUBREG, and ZERO_EXTEND of a truncating > > SUBREG respectively. The nonzero_bits of a truncating SUBREG > > pessimistically thinks that the upper bits may have an arbitrary value > > (by taking the SUBREG), so we need look deeper at the SUBREG's operand > > to confirm that the high bits are known to be zero. > > > > Unfortunately, for PR rtl-optimization/71775, ctz:DI on x86_64 with > > default architecture options is undefined at zero, so we can't be sure > > the upper bits of reg:DI 88 will be sign extended (all zeros or all ones). > > nonzero_bits knows this, so the above transformations don't trigger, > > but the transformations themselves are perfectly valid for other > > operations such as FFS, POPCOUNT and PARITY, and on other > > targets/-march settings where CTZ is defined at zero. > > > > 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. Testing with CSiBE shows these transformations > > trigger on several source files (and with -Os reduces the size of the > > code). Ok for mainline? > > > > > > 2022-07-27 Roger Sayle <roger@nextmovesoftware.com> > > > > gcc/ChangeLog > > * simplify_rtx.cc (simplify_unary_operation_1) <ABS>: Simplify > > test as both FFS and ABS result in nonzero_bits returning a > > mask that satisfies val_signbit_known_clear_p. > > <SIGN_EXTEND>: Canonicalize SIGN_EXTEND to ZERO_EXTEND when > > val_signbit_known_clear_p is true of the operand. > > Simplify sign extensions of SUBREG truncations of operands > > that are already suitably (zero) extended. > > <ZERO_EXTEND>: Simplify zero extensions of SUBREG truncations > > of operands that are already suitably zero extended. > > > > > > Thanks in advance, > > Roger > > -- > > > > diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index > > fa20665..e62bf56 100644 > > --- a/gcc/simplify-rtx.cc > > +++ b/gcc/simplify-rtx.cc > > @@ -1366,9 +1366,8 @@ simplify_context::simplify_unary_operation_1 > (rtx_code code, machine_mode mode, > > break; > > > > /* If operand is something known to be positive, ignore the ABS. */ > > - if (GET_CODE (op) == FFS || GET_CODE (op) == ABS > > - || val_signbit_known_clear_p (GET_MODE (op), > > - nonzero_bits (op, GET_MODE (op)))) > > + if (val_signbit_known_clear_p (GET_MODE (op), > > + nonzero_bits (op, GET_MODE (op)))) > > return op; > > > > /* If operand is known to be only -1 or 0, convert ABS to NEG. > > */ @@ -1615,6 +1614,24 @@ simplify_context::simplify_unary_operation_1 > (rtx_code code, machine_mode mode, > > } > > } > > > > + /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when > > + we know the sign bit of OP must be clear. */ > > + if (val_signbit_known_clear_p (GET_MODE (op), > > + nonzero_bits (op, GET_MODE (op)))) > > + return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op)); > > + > > + /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ > > + if (GET_CODE (op) == SUBREG > > + && subreg_lowpart_p (op) > > + && GET_MODE (SUBREG_REG (op)) == mode > > + && is_a <scalar_int_mode> (mode, &int_mode) > > + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) > > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT > > + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION > (int_mode) > > + && (nonzero_bits (SUBREG_REG (op), mode) > > + & ~(GET_MODE_MASK (op_mode)>>1)) == 0) > > + return SUBREG_REG (op); > > + > > #if defined(POINTERS_EXTEND_UNSIGNED) > > /* As we do not know which address space the pointer is referring to, > > we can do this only if the target does not support different > > pointer @@ -1765,6 +1782,18 @@ > simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode > mode, > > op0_mode); > > } > > > > + /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ > > + if (GET_CODE (op) == SUBREG > > + && subreg_lowpart_p (op) > > + && GET_MODE (SUBREG_REG (op)) == mode > > + && is_a <scalar_int_mode> (mode, &int_mode) > > + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) > > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT > > + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION > (int_mode) > > + && (nonzero_bits (SUBREG_REG (op), mode) > > + & ~GET_MODE_MASK (op_mode)) == 0) > > + return SUBREG_REG (op); > > + > > #if defined(POINTERS_EXTEND_UNSIGNED) > > /* As we do not know which address space the pointer is referring to, > > we can do this only if the target does not support different > > pointer
Comments
"Roger Sayle" <roger@nextmovesoftware.com> writes: > Many thanks to Segher and Richard for pointing out that my removal > of optimizations of ABS(ABS(x)) and ABS(FFS(x)) in the original version > of this patch was incorrect, and my assumption that these would be > subsumed by val_signbit_known_clear_p was mistaken. That the > tests for ABS and FFS looked out of place, was not an indication that > they were not required, but that we were missing simplifications for > the related SS_ABS, PARITY, POPCOUNT, CLRSB, CLZ and CTZ etc. > To make up for this mistake, in this revised patch I've not only restored > the tests for ABS and FFS, but also added the many sibling RTX codes > that I'd also expect to see optimized here, such as ABS(PARITY(x)). > > 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? > > > 2022-08-02 Roger Sayle <roger@nextmovesoftware.com> > Segher Boessenkool <segher@kernel.crashing.org> > Richard Sandiford <richard.sandiford@arm.com> Thanks, but I didn't actually write anything. :-) > gcc/ChangeLog > * simplify_rtx.cc (simplify_unary_operation_1) <ABS>: Add > optimizations for CLRSB, PARITY, POPCOUNT, SS_ABS, CLZ, CTZ > and LSHIFTRT that are all positive to complement the existing > FFS and (idempotent) ABS simplifications. > <SIGN_EXTEND>: Canonicalize SIGN_EXTEND to ZERO_EXTEND when > val_signbit_known_clear_p is true of the operand. > Simplify sign extensions of SUBREG truncations of operands > that are already suitably (zero) extended. > <ZERO_EXTEND>: Simplify zero extensions of SUBREG truncations > of operands that are already suitably zero extended. > > Thanks in advance, > Roger > -- > >> -----Original Message----- >> From: Richard Sandiford <richard.sandiford@arm.com> >> Sent: 02 August 2022 10:39 >> To: Roger Sayle <roger@nextmovesoftware.com> >> Cc: gcc-patches@gcc.gnu.org; 'Segher Boessenkool' >> <segher@kernel.crashing.org> >> Subject: Re: [PATCH] Some additional zero-extension related optimizations > in >> simplify-rtx. >> >> "Roger Sayle" <roger@nextmovesoftware.com> writes: >> > This patch implements some additional zero-extension and >> > sign-extension related optimizations in simplify-rtx.cc. The original >> > motivation comes from PR rtl-optimization/71775, where in comment #2 >> Andrew Pinski sees: >> > >> > Failed to match this instruction: >> > (set (reg:DI 88 [ _1 ]) >> > (sign_extend:DI (subreg:SI (ctz:DI (reg/v:DI 86 [ x ])) 0))) >> > >> > On many platforms the result of DImode CTZ is constrained to be a >> > small unsigned integer (between 0 and 64), hence the truncation to >> > 32-bits (using a SUBREG) and the following sign extension back to >> > 64-bits are effectively a no-op, so the above should ideally (often) >> > be simplified to "(set (reg:DI 88) (ctz:DI (reg/v:DI 86 [ x ]))". >> > >> > To implement this, and some closely related transformations, we build >> > upon the existing val_signbit_known_clear_p predicate. In the first >> > chunk, nonzero_bits knows that FFS and ABS can't leave the sign-bit >> > bit set, so the simplification of of ABS (ABS (x)) and ABS (FFS (x)) >> > can itself be simplified. >> >> I think I misunderstood, but just in case: RTL ABS is well-defined for the > minimum >> integer (giving back the minimum integer), so we can't assume that ABS > leaves >> the sign bit clear. >> >> Thanks, >> Richard >> >> > The second transformation is that we can canonicalized SIGN_EXTEND to >> > ZERO_EXTEND (as in the PR 71775 case above) when the operand's >> > sign-bit is known to be clear. The final two chunks are for >> > SIGN_EXTEND of a truncating SUBREG, and ZERO_EXTEND of a truncating >> > SUBREG respectively. The nonzero_bits of a truncating SUBREG >> > pessimistically thinks that the upper bits may have an arbitrary value >> > (by taking the SUBREG), so we need look deeper at the SUBREG's operand >> > to confirm that the high bits are known to be zero. >> > >> > Unfortunately, for PR rtl-optimization/71775, ctz:DI on x86_64 with >> > default architecture options is undefined at zero, so we can't be sure >> > the upper bits of reg:DI 88 will be sign extended (all zeros or all > ones). >> > nonzero_bits knows this, so the above transformations don't trigger, >> > but the transformations themselves are perfectly valid for other >> > operations such as FFS, POPCOUNT and PARITY, and on other >> > targets/-march settings where CTZ is defined at zero. >> > >> > 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. Testing with CSiBE shows these transformations >> > trigger on several source files (and with -Os reduces the size of the >> > code). Ok for mainline? >> > >> > >> > 2022-07-27 Roger Sayle <roger@nextmovesoftware.com> >> > >> > gcc/ChangeLog >> > * simplify_rtx.cc (simplify_unary_operation_1) <ABS>: Simplify >> > test as both FFS and ABS result in nonzero_bits returning a >> > mask that satisfies val_signbit_known_clear_p. >> > <SIGN_EXTEND>: Canonicalize SIGN_EXTEND to ZERO_EXTEND when >> > val_signbit_known_clear_p is true of the operand. >> > Simplify sign extensions of SUBREG truncations of operands >> > that are already suitably (zero) extended. >> > <ZERO_EXTEND>: Simplify zero extensions of SUBREG truncations >> > of operands that are already suitably zero extended. >> > >> > >> > Thanks in advance, >> > Roger >> > -- >> > >> > diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index >> > fa20665..e62bf56 100644 >> > --- a/gcc/simplify-rtx.cc >> > +++ b/gcc/simplify-rtx.cc >> > @@ -1366,9 +1366,8 @@ simplify_context::simplify_unary_operation_1 >> (rtx_code code, machine_mode mode, >> > break; >> > >> > /* If operand is something known to be positive, ignore the ABS. > */ >> > - if (GET_CODE (op) == FFS || GET_CODE (op) == ABS >> > - || val_signbit_known_clear_p (GET_MODE (op), >> > - nonzero_bits (op, GET_MODE (op)))) >> > + if (val_signbit_known_clear_p (GET_MODE (op), >> > + nonzero_bits (op, GET_MODE (op)))) >> > return op; >> > >> > /* If operand is known to be only -1 or 0, convert ABS to NEG. >> > */ @@ -1615,6 +1614,24 @@ simplify_context::simplify_unary_operation_1 >> (rtx_code code, machine_mode mode, >> > } >> > } >> > >> > + /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when >> > + we know the sign bit of OP must be clear. */ >> > + if (val_signbit_known_clear_p (GET_MODE (op), >> > + nonzero_bits (op, GET_MODE (op)))) >> > + return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op)); >> > + >> > + /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ >> > + if (GET_CODE (op) == SUBREG >> > + && subreg_lowpart_p (op) >> > + && GET_MODE (SUBREG_REG (op)) == mode >> > + && is_a <scalar_int_mode> (mode, &int_mode) >> > + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) >> > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT >> > + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION >> (int_mode) >> > + && (nonzero_bits (SUBREG_REG (op), mode) >> > + & ~(GET_MODE_MASK (op_mode)>>1)) == 0) >> > + return SUBREG_REG (op); >> > + >> > #if defined(POINTERS_EXTEND_UNSIGNED) >> > /* As we do not know which address space the pointer is referring > to, >> > we can do this only if the target does not support different >> > pointer @@ -1765,6 +1782,18 @@ >> simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode >> mode, >> > op0_mode); >> > } >> > >> > + /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ >> > + if (GET_CODE (op) == SUBREG >> > + && subreg_lowpart_p (op) >> > + && GET_MODE (SUBREG_REG (op)) == mode >> > + && is_a <scalar_int_mode> (mode, &int_mode) >> > + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) >> > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT >> > + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION >> (int_mode) >> > + && (nonzero_bits (SUBREG_REG (op), mode) >> > + & ~GET_MODE_MASK (op_mode)) == 0) >> > + return SUBREG_REG (op); >> > + >> > #if defined(POINTERS_EXTEND_UNSIGNED) >> > /* As we do not know which address space the pointer is referring > to, >> > we can do this only if the target does not support different >> > pointer > > diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc > index fa20665..b53272b 100644 > --- a/gcc/simplify-rtx.cc > +++ b/gcc/simplify-rtx.cc > @@ -1366,11 +1366,57 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, > break; > > /* If operand is something known to be positive, ignore the ABS. */ > - if (GET_CODE (op) == FFS || GET_CODE (op) == ABS > - || val_signbit_known_clear_p (GET_MODE (op), > - nonzero_bits (op, GET_MODE (op)))) > + if (val_signbit_known_clear_p (GET_MODE (op), > + nonzero_bits (op, GET_MODE (op)))) > return op; > > + /* Using nonzero_bits doesn't (currently) work for modes wider than > + HOST_WIDE_INT, so the following transformations help simplify > + ABS for TImode and wider. */ > + switch (GET_CODE (op)) > + { > + case ABS: > + case CLRSB: > + case FFS: > + case PARITY: > + case POPCOUNT: > + case SS_ABS: > + return op; > + > + case CLZ: > + if (is_a <scalar_int_mode> (mode, &int_mode) > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT) > + { > + HOST_WIDE_INT val0; > + if (CLZ_DEFINED_VALUE_AT_ZERO (int_mode, val0) > + && (val0 >> (GET_MODE_PRECISION (int_mode) - 1)) == 0) > + return op; Shifts right of negative numbers are implementation-defined, so I guess this should be unsigned HOST_WIDE_INT instead. (Or use (val0 & (GET_MODE_MASK (int_mode) >> 1)) == 0, like you do below.) Same for CTZ. OK with that change, thanks. Richard > + } > + break; > + > + case CTZ: > + if (is_a <scalar_int_mode> (mode, &int_mode) > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT) > + { > + HOST_WIDE_INT val0; > + if (CTZ_DEFINED_VALUE_AT_ZERO (int_mode, val0) > + && (val0 >> (GET_MODE_PRECISION (int_mode) - 1)) == 0) > + return op; > + } > + break; > + > + case LSHIFTRT: > + if (CONST_INT_P (XEXP (op, 1)) > + && INTVAL (XEXP (op, 1)) > 0 > + && is_a <scalar_int_mode> (mode, &int_mode) > + && INTVAL (XEXP (op, 1)) < GET_MODE_PRECISION (int_mode)) > + return op; > + break; > + > + default: > + break; > + } > + > /* If operand is known to be only -1 or 0, convert ABS to NEG. */ > if (is_a <scalar_int_mode> (mode, &int_mode) > && (num_sign_bit_copies (op, int_mode) > @@ -1615,6 +1661,24 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, > } > } > > + /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when > + we know the sign bit of OP must be clear. */ > + if (val_signbit_known_clear_p (GET_MODE (op), > + nonzero_bits (op, GET_MODE (op)))) > + return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op)); > + > + /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ > + if (GET_CODE (op) == SUBREG > + && subreg_lowpart_p (op) > + && GET_MODE (SUBREG_REG (op)) == mode > + && is_a <scalar_int_mode> (mode, &int_mode) > + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT > + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode) > + && (nonzero_bits (SUBREG_REG (op), mode) > + & ~(GET_MODE_MASK (op_mode) >> 1)) == 0) > + return SUBREG_REG (op); > + > #if defined(POINTERS_EXTEND_UNSIGNED) > /* As we do not know which address space the pointer is referring to, > we can do this only if the target does not support different pointer > @@ -1765,6 +1829,18 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, > op0_mode); > } > > + /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ > + if (GET_CODE (op) == SUBREG > + && subreg_lowpart_p (op) > + && GET_MODE (SUBREG_REG (op)) == mode > + && is_a <scalar_int_mode> (mode, &int_mode) > + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT > + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode) > + && (nonzero_bits (SUBREG_REG (op), mode) > + & ~GET_MODE_MASK (op_mode)) == 0) > + return SUBREG_REG (op); > + > #if defined(POINTERS_EXTEND_UNSIGNED) > /* As we do not know which address space the pointer is referring to, > we can do this only if the target does not support different pointer
Richard Sandiford via Gcc-patches <gcc-patches@gcc.gnu.org> writes: > "Roger Sayle" <roger@nextmovesoftware.com> writes: >> Many thanks to Segher and Richard for pointing out that my removal >> of optimizations of ABS(ABS(x)) and ABS(FFS(x)) in the original version >> of this patch was incorrect, and my assumption that these would be >> subsumed by val_signbit_known_clear_p was mistaken. That the >> tests for ABS and FFS looked out of place, was not an indication that >> they were not required, but that we were missing simplifications for >> the related SS_ABS, PARITY, POPCOUNT, CLRSB, CLZ and CTZ etc. >> To make up for this mistake, in this revised patch I've not only restored >> the tests for ABS and FFS, but also added the many sibling RTX codes >> that I'd also expect to see optimized here, such as ABS(PARITY(x)). >> >> 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? >> >> >> 2022-08-02 Roger Sayle <roger@nextmovesoftware.com> >> Segher Boessenkool <segher@kernel.crashing.org> >> Richard Sandiford <richard.sandiford@arm.com> > > Thanks, but I didn't actually write anything. :-) > >> gcc/ChangeLog >> * simplify_rtx.cc (simplify_unary_operation_1) <ABS>: Add >> optimizations for CLRSB, PARITY, POPCOUNT, SS_ABS, CLZ, CTZ >> and LSHIFTRT that are all positive to complement the existing >> FFS and (idempotent) ABS simplifications. >> <SIGN_EXTEND>: Canonicalize SIGN_EXTEND to ZERO_EXTEND when >> val_signbit_known_clear_p is true of the operand. >> Simplify sign extensions of SUBREG truncations of operands >> that are already suitably (zero) extended. >> <ZERO_EXTEND>: Simplify zero extensions of SUBREG truncations >> of operands that are already suitably zero extended. >> >> Thanks in advance, >> Roger >> -- >> >>> -----Original Message----- >>> From: Richard Sandiford <richard.sandiford@arm.com> >>> Sent: 02 August 2022 10:39 >>> To: Roger Sayle <roger@nextmovesoftware.com> >>> Cc: gcc-patches@gcc.gnu.org; 'Segher Boessenkool' >>> <segher@kernel.crashing.org> >>> Subject: Re: [PATCH] Some additional zero-extension related optimizations >> in >>> simplify-rtx. >>> >>> "Roger Sayle" <roger@nextmovesoftware.com> writes: >>> > This patch implements some additional zero-extension and >>> > sign-extension related optimizations in simplify-rtx.cc. The original >>> > motivation comes from PR rtl-optimization/71775, where in comment #2 >>> Andrew Pinski sees: >>> > >>> > Failed to match this instruction: >>> > (set (reg:DI 88 [ _1 ]) >>> > (sign_extend:DI (subreg:SI (ctz:DI (reg/v:DI 86 [ x ])) 0))) >>> > >>> > On many platforms the result of DImode CTZ is constrained to be a >>> > small unsigned integer (between 0 and 64), hence the truncation to >>> > 32-bits (using a SUBREG) and the following sign extension back to >>> > 64-bits are effectively a no-op, so the above should ideally (often) >>> > be simplified to "(set (reg:DI 88) (ctz:DI (reg/v:DI 86 [ x ]))". >>> > >>> > To implement this, and some closely related transformations, we build >>> > upon the existing val_signbit_known_clear_p predicate. In the first >>> > chunk, nonzero_bits knows that FFS and ABS can't leave the sign-bit >>> > bit set, so the simplification of of ABS (ABS (x)) and ABS (FFS (x)) >>> > can itself be simplified. >>> >>> I think I misunderstood, but just in case: RTL ABS is well-defined for the >> minimum >>> integer (giving back the minimum integer), so we can't assume that ABS >> leaves >>> the sign bit clear. >>> >>> Thanks, >>> Richard >>> >>> > The second transformation is that we can canonicalized SIGN_EXTEND to >>> > ZERO_EXTEND (as in the PR 71775 case above) when the operand's >>> > sign-bit is known to be clear. The final two chunks are for >>> > SIGN_EXTEND of a truncating SUBREG, and ZERO_EXTEND of a truncating >>> > SUBREG respectively. The nonzero_bits of a truncating SUBREG >>> > pessimistically thinks that the upper bits may have an arbitrary value >>> > (by taking the SUBREG), so we need look deeper at the SUBREG's operand >>> > to confirm that the high bits are known to be zero. >>> > >>> > Unfortunately, for PR rtl-optimization/71775, ctz:DI on x86_64 with >>> > default architecture options is undefined at zero, so we can't be sure >>> > the upper bits of reg:DI 88 will be sign extended (all zeros or all >> ones). >>> > nonzero_bits knows this, so the above transformations don't trigger, >>> > but the transformations themselves are perfectly valid for other >>> > operations such as FFS, POPCOUNT and PARITY, and on other >>> > targets/-march settings where CTZ is defined at zero. >>> > >>> > 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. Testing with CSiBE shows these transformations >>> > trigger on several source files (and with -Os reduces the size of the >>> > code). Ok for mainline? >>> > >>> > >>> > 2022-07-27 Roger Sayle <roger@nextmovesoftware.com> >>> > >>> > gcc/ChangeLog >>> > * simplify_rtx.cc (simplify_unary_operation_1) <ABS>: Simplify >>> > test as both FFS and ABS result in nonzero_bits returning a >>> > mask that satisfies val_signbit_known_clear_p. >>> > <SIGN_EXTEND>: Canonicalize SIGN_EXTEND to ZERO_EXTEND when >>> > val_signbit_known_clear_p is true of the operand. >>> > Simplify sign extensions of SUBREG truncations of operands >>> > that are already suitably (zero) extended. >>> > <ZERO_EXTEND>: Simplify zero extensions of SUBREG truncations >>> > of operands that are already suitably zero extended. >>> > >>> > >>> > Thanks in advance, >>> > Roger >>> > -- >>> > >>> > diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index >>> > fa20665..e62bf56 100644 >>> > --- a/gcc/simplify-rtx.cc >>> > +++ b/gcc/simplify-rtx.cc >>> > @@ -1366,9 +1366,8 @@ simplify_context::simplify_unary_operation_1 >>> (rtx_code code, machine_mode mode, >>> > break; >>> > >>> > /* If operand is something known to be positive, ignore the ABS. >> */ >>> > - if (GET_CODE (op) == FFS || GET_CODE (op) == ABS >>> > - || val_signbit_known_clear_p (GET_MODE (op), >>> > - nonzero_bits (op, GET_MODE (op)))) >>> > + if (val_signbit_known_clear_p (GET_MODE (op), >>> > + nonzero_bits (op, GET_MODE (op)))) >>> > return op; >>> > >>> > /* If operand is known to be only -1 or 0, convert ABS to NEG. >>> > */ @@ -1615,6 +1614,24 @@ simplify_context::simplify_unary_operation_1 >>> (rtx_code code, machine_mode mode, >>> > } >>> > } >>> > >>> > + /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when >>> > + we know the sign bit of OP must be clear. */ >>> > + if (val_signbit_known_clear_p (GET_MODE (op), >>> > + nonzero_bits (op, GET_MODE (op)))) >>> > + return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op)); >>> > + >>> > + /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ >>> > + if (GET_CODE (op) == SUBREG >>> > + && subreg_lowpart_p (op) >>> > + && GET_MODE (SUBREG_REG (op)) == mode >>> > + && is_a <scalar_int_mode> (mode, &int_mode) >>> > + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) >>> > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT >>> > + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION >>> (int_mode) >>> > + && (nonzero_bits (SUBREG_REG (op), mode) >>> > + & ~(GET_MODE_MASK (op_mode)>>1)) == 0) >>> > + return SUBREG_REG (op); >>> > + >>> > #if defined(POINTERS_EXTEND_UNSIGNED) >>> > /* As we do not know which address space the pointer is referring >> to, >>> > we can do this only if the target does not support different >>> > pointer @@ -1765,6 +1782,18 @@ >>> simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode >>> mode, >>> > op0_mode); >>> > } >>> > >>> > + /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ >>> > + if (GET_CODE (op) == SUBREG >>> > + && subreg_lowpart_p (op) >>> > + && GET_MODE (SUBREG_REG (op)) == mode >>> > + && is_a <scalar_int_mode> (mode, &int_mode) >>> > + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) >>> > + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT >>> > + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION >>> (int_mode) >>> > + && (nonzero_bits (SUBREG_REG (op), mode) >>> > + & ~GET_MODE_MASK (op_mode)) == 0) >>> > + return SUBREG_REG (op); >>> > + >>> > #if defined(POINTERS_EXTEND_UNSIGNED) >>> > /* As we do not know which address space the pointer is referring >> to, >>> > we can do this only if the target does not support different >>> > pointer >> >> diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc >> index fa20665..b53272b 100644 >> --- a/gcc/simplify-rtx.cc >> +++ b/gcc/simplify-rtx.cc >> @@ -1366,11 +1366,57 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, >> break; >> >> /* If operand is something known to be positive, ignore the ABS. */ >> - if (GET_CODE (op) == FFS || GET_CODE (op) == ABS >> - || val_signbit_known_clear_p (GET_MODE (op), >> - nonzero_bits (op, GET_MODE (op)))) >> + if (val_signbit_known_clear_p (GET_MODE (op), >> + nonzero_bits (op, GET_MODE (op)))) >> return op; >> >> + /* Using nonzero_bits doesn't (currently) work for modes wider than >> + HOST_WIDE_INT, so the following transformations help simplify >> + ABS for TImode and wider. */ >> + switch (GET_CODE (op)) >> + { >> + case ABS: >> + case CLRSB: >> + case FFS: >> + case PARITY: >> + case POPCOUNT: >> + case SS_ABS: >> + return op; >> + >> + case CLZ: >> + if (is_a <scalar_int_mode> (mode, &int_mode) >> + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT) >> + { >> + HOST_WIDE_INT val0; >> + if (CLZ_DEFINED_VALUE_AT_ZERO (int_mode, val0) >> + && (val0 >> (GET_MODE_PRECISION (int_mode) - 1)) == 0) >> + return op; > > Shifts right of negative numbers are implementation-defined, > so I guess this should be unsigned HOST_WIDE_INT instead. > (Or use (val0 & (GET_MODE_MASK (int_mode) >> 1)) == 0, Oops, missing ~ :) > like you do below.) > > Same for CTZ. > > OK with that change, thanks. > > Richard > >> + } >> + break; >> + >> + case CTZ: >> + if (is_a <scalar_int_mode> (mode, &int_mode) >> + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT) >> + { >> + HOST_WIDE_INT val0; >> + if (CTZ_DEFINED_VALUE_AT_ZERO (int_mode, val0) >> + && (val0 >> (GET_MODE_PRECISION (int_mode) - 1)) == 0) >> + return op; >> + } >> + break; >> + >> + case LSHIFTRT: >> + if (CONST_INT_P (XEXP (op, 1)) >> + && INTVAL (XEXP (op, 1)) > 0 >> + && is_a <scalar_int_mode> (mode, &int_mode) >> + && INTVAL (XEXP (op, 1)) < GET_MODE_PRECISION (int_mode)) >> + return op; >> + break; >> + >> + default: >> + break; >> + } >> + >> /* If operand is known to be only -1 or 0, convert ABS to NEG. */ >> if (is_a <scalar_int_mode> (mode, &int_mode) >> && (num_sign_bit_copies (op, int_mode) >> @@ -1615,6 +1661,24 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, >> } >> } >> >> + /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when >> + we know the sign bit of OP must be clear. */ >> + if (val_signbit_known_clear_p (GET_MODE (op), >> + nonzero_bits (op, GET_MODE (op)))) >> + return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op)); >> + >> + /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ >> + if (GET_CODE (op) == SUBREG >> + && subreg_lowpart_p (op) >> + && GET_MODE (SUBREG_REG (op)) == mode >> + && is_a <scalar_int_mode> (mode, &int_mode) >> + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) >> + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT >> + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode) >> + && (nonzero_bits (SUBREG_REG (op), mode) >> + & ~(GET_MODE_MASK (op_mode) >> 1)) == 0) >> + return SUBREG_REG (op); >> + >> #if defined(POINTERS_EXTEND_UNSIGNED) >> /* As we do not know which address space the pointer is referring to, >> we can do this only if the target does not support different pointer >> @@ -1765,6 +1829,18 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, >> op0_mode); >> } >> >> + /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ >> + if (GET_CODE (op) == SUBREG >> + && subreg_lowpart_p (op) >> + && GET_MODE (SUBREG_REG (op)) == mode >> + && is_a <scalar_int_mode> (mode, &int_mode) >> + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) >> + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT >> + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode) >> + && (nonzero_bits (SUBREG_REG (op), mode) >> + & ~GET_MODE_MASK (op_mode)) == 0) >> + return SUBREG_REG (op); >> + >> #if defined(POINTERS_EXTEND_UNSIGNED) >> /* As we do not know which address space the pointer is referring to, >> we can do this only if the target does not support different pointer
diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index fa20665..b53272b 100644 --- a/gcc/simplify-rtx.cc +++ b/gcc/simplify-rtx.cc @@ -1366,11 +1366,57 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, break; /* If operand is something known to be positive, ignore the ABS. */ - if (GET_CODE (op) == FFS || GET_CODE (op) == ABS - || val_signbit_known_clear_p (GET_MODE (op), - nonzero_bits (op, GET_MODE (op)))) + if (val_signbit_known_clear_p (GET_MODE (op), + nonzero_bits (op, GET_MODE (op)))) return op; + /* Using nonzero_bits doesn't (currently) work for modes wider than + HOST_WIDE_INT, so the following transformations help simplify + ABS for TImode and wider. */ + switch (GET_CODE (op)) + { + case ABS: + case CLRSB: + case FFS: + case PARITY: + case POPCOUNT: + case SS_ABS: + return op; + + case CLZ: + if (is_a <scalar_int_mode> (mode, &int_mode) + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT) + { + HOST_WIDE_INT val0; + if (CLZ_DEFINED_VALUE_AT_ZERO (int_mode, val0) + && (val0 >> (GET_MODE_PRECISION (int_mode) - 1)) == 0) + return op; + } + break; + + case CTZ: + if (is_a <scalar_int_mode> (mode, &int_mode) + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT) + { + HOST_WIDE_INT val0; + if (CTZ_DEFINED_VALUE_AT_ZERO (int_mode, val0) + && (val0 >> (GET_MODE_PRECISION (int_mode) - 1)) == 0) + return op; + } + break; + + case LSHIFTRT: + if (CONST_INT_P (XEXP (op, 1)) + && INTVAL (XEXP (op, 1)) > 0 + && is_a <scalar_int_mode> (mode, &int_mode) + && INTVAL (XEXP (op, 1)) < GET_MODE_PRECISION (int_mode)) + return op; + break; + + default: + break; + } + /* If operand is known to be only -1 or 0, convert ABS to NEG. */ if (is_a <scalar_int_mode> (mode, &int_mode) && (num_sign_bit_copies (op, int_mode) @@ -1615,6 +1661,24 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, } } + /* We can canonicalize SIGN_EXTEND (op) as ZERO_EXTEND (op) when + we know the sign bit of OP must be clear. */ + if (val_signbit_known_clear_p (GET_MODE (op), + nonzero_bits (op, GET_MODE (op)))) + return simplify_gen_unary (ZERO_EXTEND, mode, op, GET_MODE (op)); + + /* (sign_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ + if (GET_CODE (op) == SUBREG + && subreg_lowpart_p (op) + && GET_MODE (SUBREG_REG (op)) == mode + && is_a <scalar_int_mode> (mode, &int_mode) + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode) + && (nonzero_bits (SUBREG_REG (op), mode) + & ~(GET_MODE_MASK (op_mode) >> 1)) == 0) + return SUBREG_REG (op); + #if defined(POINTERS_EXTEND_UNSIGNED) /* As we do not know which address space the pointer is referring to, we can do this only if the target does not support different pointer @@ -1765,6 +1829,18 @@ simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode, op0_mode); } + /* (zero_extend:DI (subreg:SI (ctz:DI ...))) is (ctz:DI ...). */ + if (GET_CODE (op) == SUBREG + && subreg_lowpart_p (op) + && GET_MODE (SUBREG_REG (op)) == mode + && is_a <scalar_int_mode> (mode, &int_mode) + && is_a <scalar_int_mode> (GET_MODE (op), &op_mode) + && GET_MODE_PRECISION (int_mode) <= HOST_BITS_PER_WIDE_INT + && GET_MODE_PRECISION (op_mode) < GET_MODE_PRECISION (int_mode) + && (nonzero_bits (SUBREG_REG (op), mode) + & ~GET_MODE_MASK (op_mode)) == 0) + return SUBREG_REG (op); + #if defined(POINTERS_EXTEND_UNSIGNED) /* As we do not know which address space the pointer is referring to, we can do this only if the target does not support different pointer