Message ID | 001101d7c337$1db55680$59200380$@nextmovesoftware.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 36A74385742B for <patchwork@sourceware.org>; Sun, 17 Oct 2021 09:12:49 +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 758DE3858D35 for <gcc-patches@gcc.gnu.org>; Sun, 17 Oct 2021 09:12:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 758DE3858D35 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: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=V/XDAziQVDdmHIetDLrBjbdh4aSIU/xCFDpihVv6TFY=; b=HRK2Vpn8sxHDXnEME7/2POPpa9 5UxhOlKVryi+j9u6chq1v7OBSdTvSCpWH7OpvY4gSvicXisSlGLQbXwU50LR4HPFRGtffjcogamU5 xdsRETPDdVCgIibEcGEfHU0+8P8pjtzNqDH8CmmdErGR8/ih4A4XFEAEWPJW0XqmfqvAC4veXe9sx U0j/m/SoBlV5u/HIvfzyyyRVlfb5k63/UNbAUleF/DVgTjL+IFCHCFb29DwHikYtzXQYYJNL9CBQU 8qRynrpdeablDJ5RyJTo2lxhPTgNK7QycQ+tDEl5nxTK5kbu6Q9OfKP6lAZY/6k8l4yKwTXpSNlkX 4ujjuteg==; Received: from host86-163-35-115.range86-163.btcentralplus.com ([86.163.35.115]:64981 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 1mc2DX-0003EQ-PK; Sun, 17 Oct 2021 05:12:32 -0400 From: "Roger Sayle" <roger@nextmovesoftware.com> To: "'GCC Patches'" <gcc-patches@gcc.gnu.org> Subject: [PATCH] Constant fold SS_NEG and SS_ABS in simplify-rtx.c Date: Sun, 17 Oct 2021 10:12:30 +0100 Message-ID: <001101d7c337$1db55680$59200380$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0012_01D7C33F.7F7C0870" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdfDNu2o9LSiDcvVQbaaRFNjbcZeqQ== 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.6 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
Constant fold SS_NEG and SS_ABS in simplify-rtx.c
|
|
Commit Message
Roger Sayle
Oct. 17, 2021, 9:12 a.m. UTC
This simple patch performs compile-time constant folding of signed saturating negation and signed saturating absolute value in the RTL optimizers. Normally in two's complement arithmetic the lowest representable signed value overflows on negation, with these saturating operators they "saturate" to the maximum representable signed value, so SS_NEG:QI -128 is 127, and SS_ABS:HI -32768 is 32767. On bfin-elf, the following two short functions: short foo() { short t = -32768; short r = __builtin_bfin_negate_fr1x16(t); return r; } int bar() { int t = -2147483648; int r = __builtin_bfin_abs_fr1x32(t); return r; } currently compile to: _foo: nop; nop; R0 = -32768 (X); R0 = -R0 (V); rts; _bar: nop; R0 = -1 (X); R0 <<= 31; R0 = abs R0; rts; but with this middle-end patch now compile to: _foo: nop; nop; nop; R0 = 32767 (X); rts; _bar: nop; nop; R0 = -1 (X); R0.H = 32767; rts; This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap" and "make -k check" with no new failures. Ok for mainline? 2021-10-17 Roger Sayle <roger@nextmovesoftware.com> gcc/ChangeLog * simplify-rtx.c (simplify_const_unary_operation) [SS_NEG, SS_ABS]: Evalute SS_NEG and SS_ABS of a constant argument. gcc/testsuite/ChangeLog * gcc.target/bfin/ssabs.c: New test case. * gcc.target/bfin/ssneg.c: New test case. Thanks in advance, Roger -- /* { dg-do compile } */ /* { dg-options "-O2" } */ int foo() { int t = -2147483648; int r = __builtin_bfin_abs_fr1x32(t); return r; } /* { dg-final { scan-assembler "32767" } } */ /* { dg-do compile } */ /* { dg-options "-O2" } */ short foo() { short t = -32768; short r = __builtin_bfin_negate_fr1x16(t); return r; } /* { dg-final { scan-assembler "32767" } } */
Comments
On 10/17/2021 3:12 AM, Roger Sayle wrote: > This simple patch performs compile-time constant folding of > signed saturating negation and signed saturating absolute value > in the RTL optimizers. Normally in two's complement arithmetic > the lowest representable signed value overflows on negation, > with these saturating operators they "saturate" to the maximum > representable signed value, so SS_NEG:QI -128 is 127, and > SS_ABS:HI -32768 is 32767. > > On bfin-elf, the following two short functions: > > short foo() > { > short t = -32768; > short r = __builtin_bfin_negate_fr1x16(t); > return r; > } > > int bar() > { > int t = -2147483648; > int r = __builtin_bfin_abs_fr1x32(t); > return r; > } > > currently compile to: > _foo: nop; > nop; > R0 = -32768 (X); > R0 = -R0 (V); > rts; > > _bar: nop; > R0 = -1 (X); > R0 <<= 31; > R0 = abs R0; > rts; > > but with this middle-end patch now compile to: > > _foo: nop; > nop; > nop; > R0 = 32767 (X); > rts; > > _bar: nop; > nop; > R0 = -1 (X); > R0.H = 32767; > rts; > > > This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap" > and "make -k check" with no new failures. Ok for mainline? > > > 2021-10-17 Roger Sayle <roger@nextmovesoftware.com> > > gcc/ChangeLog > * simplify-rtx.c (simplify_const_unary_operation) [SS_NEG, SS_ABS]: > Evalute SS_NEG and SS_ABS of a constant argument. > > gcc/testsuite/ChangeLog > * gcc.target/bfin/ssabs.c: New test case. > * gcc.target/bfin/ssneg.c: New test case. OK. Jeff
diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c index e4fae0b..2bb18fb 100644 --- a/gcc/simplify-rtx.c +++ b/gcc/simplify-rtx.c @@ -2026,6 +2026,20 @@ simplify_const_unary_operation (enum rtx_code code, machine_mode mode, result = wide_int::from (op0, width, SIGNED); break; + case SS_NEG: + if (wi::only_sign_bit_p (op0)) + result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED); + else + result = wi::neg (op0); + break; + + case SS_ABS: + if (wi::only_sign_bit_p (op0)) + result = wi::max_value (GET_MODE_PRECISION (imode), SIGNED); + else + result = wi::abs (op0); + break; + case SQRT: default: return 0;