From patchwork Mon Oct 25 07:42:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1545598 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=b2f3EaKV; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Hd6Ps0M3Fz9sXS for ; Mon, 25 Oct 2021 18:43:11 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EC25F385803D for ; Mon, 25 Oct 2021 07:43:08 +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 2D5883858C27 for ; Mon, 25 Oct 2021 07:42:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2D5883858C27 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: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=YwpWHnZxj8VS9m2+v0TyrpjKgUfATrJKiDOuLn7Ob+I=; b=b2f3EaKVMTRZ4yzXutdI3FG0rK vHjQaGRK50vidGwHbiootCQquC9k1VFSFJRGVp6D6OuBwIEQgByZSHLTqzzW6JQdwAhRE9pWykPuf rTRjT4dArWVye8CT9ULINSdha3QGVU+XbMLMDFJycNlJW9Jox+2fD7AH1RjSJ1JTw16J1li52vm2u VB/X3qHEoTn4KlFKLji2mCaLlRLamHG97NBgrBwylhbKopY9wIR6AbEUhw/lsY7O7HVe0fNkhAaEQ LcrXTEIYkdlOpyj37I4e2IiGZGA4IjQ8Nw/0ivZPo33I7xBkfdsZrQMSDNMkEk/TxkSaQnEyNye6O ZMmpjVgg==; Received: from host86-163-35-115.range86-163.btcentralplus.com ([86.163.35.115]:50539 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 ) id 1meud2-0008N6-CW for gcc-patches@gcc.gnu.org; Mon, 25 Oct 2021 03:42:44 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] Constant fold/simplify SS_ASHIFT and US_ASHIFT in simplify-rtx.c Date: Mon, 25 Oct 2021 08:42:42 +0100 Message-ID: <001101d7c973$e5de92b0$b19bb810$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdfJcs39PGjwHMZZRpCdPnbAGf2ySA== 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.7 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch adds compile-time evaluation of signed saturating left shift (SS_ASHIFT) and unsigned saturating left shift (US_ASHIFT) to simplify-rtx's simplify_const_binary_operation. US_ASHIFT saturates to the maximum unsigned value on overflow (which occurs when the shift is greater than the leading zero count), while SS_ASHIFT saturates on overflow to the maximum signed value for positive arguments, and the minimum signed value for negative arguments (which occurs when the shift count is greater than the number of leading redundant sign bits, clrsb). This suggests some additional simplifications that this patch implements in simplify_binary_operation_1; us_ashift:HI of 0xffff remains 0xffff (much like any ashift of 0x0000 remains 0x0000), and ss_ashift:HI of 0x7fff remains 0x7ffff, and of 0x8000 remains 0x8000. Conveniently the bfin backend provides instructions/built-ins that allow this functionality to be tested. The two functions below short stest_sat_max() { return __builtin_bfin_shl_fr1x16(10000,8); } short stest_sat_min() { return __builtin_bfin_shl_fr1x16(-10000,8); } previously on bfin-elf with -O2 generated: _stest_sat_max: nop; nop; R0 = 10000 (X); R0 = R0 << 8 (V,S); rts; _stest_sat_min: nop; nop; R0 = -10000 (X); R0 = R0 << 8 (V,S); rts; With this patch, bfin-elf now generates: _stest_sat_max: nop; nop; nop; R0 = 32767 (X); rts; _stest_sat_min: nop; nop; nop; R0 = -32768 (X); rts; This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check with no new failures, and on a cross-compiler to bfin-elf with no regressions. Ok for mainline? 2021-10-25 Roger Sayle gcc/ChangeLog * simplify-rtx (simplify_binary_operation_1) [SS_ASHIFT]: Simplify shifts of the mode's smin_value and smax_value when the bit count operand doesn't have side-effects. [US_ASHIFT]: Likewise, simplify shifts of the mode's umax_value when the bit count operand doesn't have side-effects. (simplify_const_binary_operation) [SS_ASHIFT, US_ASHIFT]: Perform compile-time evaluation of saturating left shifts with constant arguments. gcc/testsuite/ChangeLog * gcc.target/bfin/ssashift-1.c: New test case. Roger diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c index 2bb18fb..5903d55 100644 --- a/gcc/simplify-rtx.c +++ b/gcc/simplify-rtx.c @@ -4064,9 +4064,25 @@ simplify_context::simplify_binary_operation_1 (rtx_code code, } break; - case ASHIFT: case SS_ASHIFT: + if (CONST_INT_P (trueop0) + && HWI_COMPUTABLE_MODE_P (mode) + && (UINTVAL (trueop0) == (GET_MODE_MASK (mode) >> 1) + || mode_signbit_p (mode, trueop0)) + && ! side_effects_p (op1)) + return op0; + goto simplify_ashift; + case US_ASHIFT: + if (CONST_INT_P (trueop0) + && HWI_COMPUTABLE_MODE_P (mode) + && UINTVAL (trueop0) == GET_MODE_MASK (mode) + && ! side_effects_p (op1)) + return op0; + /* FALLTHRU */ + + case ASHIFT: +simplify_ashift: if (trueop1 == CONST0_RTX (mode)) return op0; if (trueop0 == CONST0_RTX (mode) && ! side_effects_p (op1)) @@ -5004,6 +5020,8 @@ simplify_const_binary_operation (enum rtx_code code, machine_mode mode, case LSHIFTRT: case ASHIFTRT: case ASHIFT: + case SS_ASHIFT: + case US_ASHIFT: { wide_int wop1 = pop1; if (SHIFT_COUNT_TRUNCATED) @@ -5025,6 +5043,27 @@ simplify_const_binary_operation (enum rtx_code code, machine_mode mode, result = wi::lshift (pop0, wop1); break; + case SS_ASHIFT: + if (wi::leu_p (wop1, wi::clrsb (pop0))) + result = wi::lshift (pop0, wop1); + else if (wi::neg_p (pop0)) + result = wi::min_value (GET_MODE_PRECISION (int_mode), + SIGNED); + else + result = wi::max_value (GET_MODE_PRECISION (int_mode), + SIGNED); + break; + + case US_ASHIFT: + if (wi::eq_p (pop0, 0)) + result = pop0; + else if (wi::leu_p (wop1, wi::clz (pop0))) + result = wi::lshift (pop0, wop1); + else + result = wi::max_value (GET_MODE_PRECISION (int_mode), + UNSIGNED); + break; + default: gcc_unreachable (); } diff --git a/gcc/testsuite/gcc.target/bfin/ssashift-1.c b/gcc/testsuite/gcc.target/bfin/ssashift-1.c new file mode 100644 index 0000000..aba90a6 --- /dev/null +++ b/gcc/testsuite/gcc.target/bfin/ssashift-1.c @@ -0,0 +1,52 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +int test_ok_pos() +{ + int x = 100; + return __builtin_bfin_shl_fr1x32(x,24); +} + +int test_ok_neg() +{ + int x = -100; + return __builtin_bfin_shl_fr1x32(x,24); +} + +int test_sat_max() +{ + int x = 10000; + return __builtin_bfin_shl_fr1x32(x,24); +} + +int test_sat_min() +{ + int x = -10000; + return __builtin_bfin_shl_fr1x32(x,24); +} + +short stest_ok_pos() +{ + short x = 100; + return __builtin_bfin_shl_fr1x16(x,8); +} + +short stest_ok_neg() +{ + short x = -100; + return __builtin_bfin_shl_fr1x16(x,8); +} + +short stest_sat_max() +{ + short x = 10000; + return __builtin_bfin_shl_fr1x16(x,8); +} + +short stest_sat_min() +{ + short x = -10000; + return __builtin_bfin_shl_fr1x16(x,8); +} +/* { dg-final { scan-assembler-not "\\(S\\)" } } */ +/* { dg-final { scan-assembler-not "\\(V,S\\)" } } */