From patchwork Sun May 28 18:55:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 70197 Return-Path: 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 81F643856DFE for ; Sun, 28 May 2023 18:55:23 +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 DE77D3858D38 for ; Sun, 28 May 2023 18:55:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE77D3858D38 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=r7jvnY4S49JL6kgkxBPK/r/Hh/Y5pDKqGoyX2mJ+7Xc=; b=QrpvUJBYtx94x5qiTkJ/w5rP+e /goHZCXgBv3sSs82ZyPqDGq9z4/gjXwqwNIs2c/t1hcnVUyL042+BoQuSJdVoiJwvJ4ck0IE2F4E5 sYaqbC5Hd2lQA0Tcv/CNyoEGj+443kQpIftY64aj+IF4LET6nBRsjHtoJsp1Zd23HmzLnom4yLcus tjKzYD8O7NfAzUJl4IkQdHJ2t6noYLzr0Rb+IZemrmNeqo+UX3Rq89TseV8ZoCnrB0yJTGT8HymvW bMYnwe3gQrQ08Dy2fipgqQLhBzYaqHDaPkPjou0vV79BTuUNk8zlMvUqWeO86aORc6QE9rKx2cs9u UKw9zm+w==; Received: from host86-169-41-81.range86-169.btcentralplus.com ([86.169.41.81]:53209 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1q3LXk-0007JH-3D; Sun, 28 May 2023 14:55:05 -0400 From: "Roger Sayle" To: Cc: "'Richard Sandiford'" Subject: [PATCH] Refactor wi::bswap as a function (instead of a method). Date: Sun, 28 May 2023 19:55:00 +0100 Message-ID: <005301d99195$e9524d60$bbf6e820$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdmRk3EwjABUmw5STwqgkflLbqviOA== 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.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch implements Richard Sandiford's suggestion from https://gcc.gnu.org/pipermail/gcc-patches/2023-May/618215.html that wi::bswap (and a new wi::bitreverse) should be functions, and ideally only accessors are member functions. This patch implements the first step, moving/refactoring wi::bswap. 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-05-28 Roger Sayle gcc/ChangeLog * fold-const-call.cc (fold_const_call_ss) : Update call to wi::bswap. * simplify-rtx.cc (simplify_const_unary_operation) : Update call to wi::bswap. * tree-ssa-ccp.cc (evaluate_stmt) : Update calls to wi::bswap. * wide-int.cc (wide_int_storage::bswap): Remove/rename to... (wi::bswap_large): New function, with revised API. * wide-int.h (wi::bswap): New (template) function prototype. (wide_int_storage::bswap): Remove method. (sext_large, zext_large): Consistent indentation/line wrapping. (bswap_large): Prototype helper function containing implementation. (wi::bswap): New template wrapper around bswap_large. Thanks, Roger diff --git a/gcc/fold-const-call.cc b/gcc/fold-const-call.cc index 340cb66..663eae2 100644 --- a/gcc/fold-const-call.cc +++ b/gcc/fold-const-call.cc @@ -1060,7 +1060,8 @@ fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg, case CFN_BUILT_IN_BSWAP32: case CFN_BUILT_IN_BSWAP64: case CFN_BUILT_IN_BSWAP128: - *result = wide_int::from (arg, precision, TYPE_SIGN (arg_type)).bswap (); + *result = wi::bswap (wide_int::from (arg, precision, + TYPE_SIGN (arg_type))); return true; default: diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc index d4aeebc..d93d632 100644 --- a/gcc/simplify-rtx.cc +++ b/gcc/simplify-rtx.cc @@ -2111,7 +2111,7 @@ simplify_const_unary_operation (enum rtx_code code, machine_mode mode, break; case BSWAP: - result = wide_int (op0).bswap (); + result = wi::bswap (op0); break; case TRUNCATE: diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc index 6fb371c..26d5e44 100644 --- a/gcc/tree-ssa-ccp.cc +++ b/gcc/tree-ssa-ccp.cc @@ -2401,11 +2401,12 @@ evaluate_stmt (gimple *stmt) wide_int wval = wi::to_wide (val.value); val.value = wide_int_to_tree (type, - wide_int::from (wval, prec, - UNSIGNED).bswap ()); + wi::bswap (wide_int::from (wval, prec, + UNSIGNED))); val.mask - = widest_int::from (wide_int::from (val.mask, prec, - UNSIGNED).bswap (), + = widest_int::from (wi::bswap (wide_int::from (val.mask, + prec, + UNSIGNED)), UNSIGNED); if (wi::sext (val.mask, prec) != -1) break; diff --git a/gcc/wide-int.cc b/gcc/wide-int.cc index c0987aa..1e4c046 100644 --- a/gcc/wide-int.cc +++ b/gcc/wide-int.cc @@ -731,16 +731,13 @@ wi::set_bit_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval, } } -/* bswap THIS. */ -wide_int -wide_int_storage::bswap () const +/* Byte swap the integer represented by XVAL and LEN into VAL. Return + the number of blocks in VAL. Both XVAL and VAL have PRECISION bits. */ +unsigned int +wi::bswap_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval, + unsigned int len, unsigned int precision) { - wide_int result = wide_int::create (precision); unsigned int i, s; - unsigned int len = BLOCKS_NEEDED (precision); - unsigned int xlen = get_len (); - const HOST_WIDE_INT *xval = get_val (); - HOST_WIDE_INT *val = result.write_val (); /* This is not a well defined operation if the precision is not a multiple of 8. */ @@ -758,7 +755,7 @@ wide_int_storage::bswap () const unsigned int block = s / HOST_BITS_PER_WIDE_INT; unsigned int offset = s & (HOST_BITS_PER_WIDE_INT - 1); - byte = (safe_uhwi (xval, xlen, block) >> offset) & 0xff; + byte = (safe_uhwi (xval, len, block) >> offset) & 0xff; block = d / HOST_BITS_PER_WIDE_INT; offset = d & (HOST_BITS_PER_WIDE_INT - 1); @@ -766,8 +763,7 @@ wide_int_storage::bswap () const val[block] |= byte << offset; } - result.set_len (canonize (val, len, precision)); - return result; + return canonize (val, len, precision); } /* Fill VAL with a mask where the lower WIDTH bits are ones and the bits diff --git a/gcc/wide-int.h b/gcc/wide-int.h index 3d9b87c..a2b3371 100644 --- a/gcc/wide-int.h +++ b/gcc/wide-int.h @@ -552,6 +552,7 @@ namespace wi UNARY_FUNCTION sext (const T &, unsigned int); UNARY_FUNCTION zext (const T &, unsigned int); UNARY_FUNCTION set_bit (const T &, unsigned int); + UNARY_FUNCTION bswap (const T &); BINARY_FUNCTION min (const T1 &, const T2 &, signop); BINARY_FUNCTION smin (const T1 &, const T2 &); @@ -1086,9 +1087,6 @@ public: static wide_int from_array (const HOST_WIDE_INT *, unsigned int, unsigned int, bool = true); static wide_int create (unsigned int); - - /* FIXME: target-dependent, so should disappear. */ - wide_int bswap () const; }; namespace wi @@ -1743,13 +1741,14 @@ namespace wi int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int, const HOST_WIDE_INT *, unsigned int); unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, - unsigned int, - unsigned int, unsigned int); + unsigned int, unsigned int, unsigned int); unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, - unsigned int, - unsigned int, unsigned int); + unsigned int, unsigned int, unsigned int); unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int); + unsigned int bswap_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, + unsigned int, unsigned int); + unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int, unsigned int, unsigned int); unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, @@ -2267,6 +2266,18 @@ wi::set_bit (const T &x, unsigned int bit) return result; } +/* Byte swap the integer X. */ +template +inline WI_UNARY_RESULT (T) +wi::bswap (const T &x) +{ + WI_UNARY_RESULT_VAR (result, val, T, x); + unsigned int precision = get_precision (result); + WIDE_INT_REF_FOR (T) xi (x, precision); + result.set_len (bswap_large (val, xi.val, xi.len, precision)); + return result; +} + /* Return the mininum of X and Y, treating them both as having signedness SGN. */ template