From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id 5E1DA3858C52 for ; Fri, 29 Sep 2023 06:29:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5E1DA3858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-50307759b65so21863053e87.0 for ; Thu, 28 Sep 2023 23:29:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695968952; x=1696573752; darn=gcc.gnu.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=8hF3xO66g52gp3ke7dbpU/5D2TC/lHCcUFLcL2Ukzv0=; b=Bh5SVx2K87bfMGKeizD1tAjMyorbDDMy95DwgJglICQ+gXXakp3kr5LX0lvuu+NT7m vcyNmHrf+DJjaXSJw5jpskMpIMKeM62QuzxK2wdLJFRGRg5ujLnKSISegTI2yh+M36yh B1RFIK0L7hUkQheJiC8S5NH1ZEHTOlHSF1qzRvTD6czjXG+T0qJTEXTFr1MTRtKZBjn/ m0A8sh8jAilItWQJ9skrqau/GPp6iRVtPcgBF5AXCJm18cQPrv0Kk76RSZ10Q0GIExR4 B2HlFbkVHYloLtpYPv1Yap12X9pYTSGeonDrH819PA9yCd5RXjWym172lMLAH7JklF2N 6KPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695968952; x=1696573752; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8hF3xO66g52gp3ke7dbpU/5D2TC/lHCcUFLcL2Ukzv0=; b=ok+zHD7VNVldzjZkp31cVfNv0Vj6uucWNYrgdf4lm3WoQzKAicyy1AR9aXpL6wUDdb nId0f8BWseqVIxIu/ANgyHd3wYyV4+pLvSLcGSoF5DGikBW89X+1sHr03iveWjhvhg4K RiOEaSYOEQR8vM3P6hgVhkvWupVSQXkmPUgzC2VLFycjFRDDgKXQ8r+fPQM/pUAlyLHW 8Jl2/rieb/hvimmuDI5kV/eE+Xpmlbjl1F+i912seUm61dtMk3/ZpQBRclqppX0MbC8U dRefd1Bk75WkGSpdC49xHFFggZsrjq37IQ9rwcQ4Zpyz4Glf+IP24GY9FMbCCKRdpd4u v7xg== X-Gm-Message-State: AOJu0Yw350OaOGIRlUT1CMV1Uvz3QVNXok8jZElgukNd4vufxXZtPkVl ZCRYREpb/sBhcyg17kjtGydHrbFDFqcOBNAmzGw09VU8 X-Google-Smtp-Source: AGHT+IELY7XdrLmb+iL/zaJBVO3S24wbRdU/PA51yENG7+gThtX2f3sMz+8KEKGe5FGwLcYp4QhHfxgGS9eQ0WR0Vn4= X-Received: by 2002:a05:6512:47c:b0:500:c589:95fb with SMTP id x28-20020a056512047c00b00500c58995fbmr2835457lfd.55.1695968951766; Thu, 28 Sep 2023 23:29:11 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Biener Date: Fri, 29 Sep 2023 08:28:58 +0200 Message-ID: Subject: Re: [PATCH] Simplify & expand c_readstr To: gcc-patches@gcc.gnu.org, HAO CHEN GUI , "Kewen.Lin" , richard.sandiford@arm.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,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 List-Id: On Thu, Sep 28, 2023 at 3:37=E2=80=AFPM Richard Sandiford wrote: > > c_readstr only operated on integer modes. It worked by reading > the source string into an array of HOST_WIDE_INTs, converting > that array into a wide_int, and from there to an rtx. > > It's simpler to do this by building a target memory image and > using native_decode_rtx to convert that memory image into an rtx. > It avoids all the endianness shenanigans because both the string and > native_decode_rtx follow target memory order. It also means that the > function can handle all fixed-size modes, which simplifies callers > and allows vector modes to be used more widely. > > Tested on aarch64-linux-gnu so far. OK to install? OK. Richard. > Richard > > > gcc/ > * builtins.h (c_readstr): Take a fixed_size_mode rather than a > scalar_int_mode. > * builtins.cc (c_readstr): Likewise. Build a local array of > bytes and use native_decode_rtx to get the rtx image. > (builtin_memcpy_read_str): Simplify accordingly. > (builtin_strncpy_read_str): Likewise. > (builtin_memset_read_str): Likewise. > (builtin_memset_gen_str: Likewise. > * expr.cc (string_cst_read_str): Likewise. > --- > gcc/builtins.cc | 46 +++++++++++----------------------------------- > gcc/builtins.h | 2 +- > gcc/expr.cc | 5 ++--- > 3 files changed, 14 insertions(+), 39 deletions(-) > > diff --git a/gcc/builtins.cc b/gcc/builtins.cc > index 40dfd36a319..cb90bd03b3e 100644 > --- a/gcc/builtins.cc > +++ b/gcc/builtins.cc > @@ -743,39 +743,22 @@ c_strlen (tree arg, int only_value, c_strlen_data *= data, unsigned eltsize) > as needed. */ > > rtx > -c_readstr (const char *str, scalar_int_mode mode, > +c_readstr (const char *str, fixed_size_mode mode, > bool null_terminated_p/*=3Dtrue*/) > { > - HOST_WIDE_INT ch; > - unsigned int i, j; > - HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT]; > + auto_vec bytes; > > - gcc_assert (GET_MODE_CLASS (mode) =3D=3D MODE_INT); > - unsigned int len =3D (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_I= NT - 1) > - / HOST_BITS_PER_WIDE_INT; > + bytes.reserve (GET_MODE_SIZE (mode)); > > - gcc_assert (len <=3D MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT= ); > - for (i =3D 0; i < len; i++) > - tmp[i] =3D 0; > - > - ch =3D 1; > - for (i =3D 0; i < GET_MODE_SIZE (mode); i++) > + target_unit ch =3D 1; > + for (unsigned int i =3D 0; i < GET_MODE_SIZE (mode); ++i) > { > - j =3D i; > - if (WORDS_BIG_ENDIAN) > - j =3D GET_MODE_SIZE (mode) - i - 1; > - if (BYTES_BIG_ENDIAN !=3D WORDS_BIG_ENDIAN > - && GET_MODE_SIZE (mode) >=3D UNITS_PER_WORD) > - j =3D j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1; > - j *=3D BITS_PER_UNIT; > - > if (ch || !null_terminated_p) > ch =3D (unsigned char) str[i]; > - tmp[j / HOST_BITS_PER_WIDE_INT] |=3D ch << (j % HOST_BITS_PER_WIDE= _INT); > + bytes.quick_push (ch); > } > > - wide_int c =3D wide_int::from_array (tmp, len, GET_MODE_PRECISION (mod= e)); > - return immed_wide_int_const (c, mode); > + return native_decode_rtx (mode, bytes, 0); > } > > /* Cast a target constant CST to target CHAR and if that value fits into > @@ -3530,10 +3513,7 @@ builtin_memcpy_read_str (void *data, void *, HOST_= WIDE_INT offset, > string but the caller guarantees it's large enough for MODE. */ > const char *rep =3D (const char *) data; > > - /* The by-pieces infrastructure does not try to pick a vector mode > - for memcpy expansion. */ > - return c_readstr (rep + offset, as_a (mode), > - /*nul_terminated=3D*/false); > + return c_readstr (rep + offset, mode, /*nul_terminated=3D*/false); > } > > /* LEN specify length of the block of memcpy/memset operation. > @@ -3994,9 +3974,7 @@ builtin_strncpy_read_str (void *data, void *, HOST_= WIDE_INT offset, > if ((unsigned HOST_WIDE_INT) offset > strlen (str)) > return const0_rtx; > > - /* The by-pieces infrastructure does not try to pick a vector mode > - for strncpy expansion. */ > - return c_readstr (str + offset, as_a (mode)); > + return c_readstr (str + offset, mode); > } > > /* Helper to check the sizes of sequences and the destination of calls > @@ -4227,8 +4205,7 @@ builtin_memset_read_str (void *data, void *prev, > > memset (p, *c, size); > > - /* Vector modes should be handled above. */ > - return c_readstr (p, as_a (mode)); > + return c_readstr (p, mode); > } > > /* Callback routine for store_by_pieces. Return the RTL of a register > @@ -4275,8 +4252,7 @@ builtin_memset_gen_str (void *data, void *prev, > > p =3D XALLOCAVEC (char, size); > memset (p, 1, size); > - /* Vector modes should be handled above. */ > - coeff =3D c_readstr (p, as_a (mode)); > + coeff =3D c_readstr (p, mode); > > target =3D convert_to_mode (mode, (rtx) data, 1); > target =3D expand_mult (mode, target, coeff, NULL_RTX, 1); > diff --git a/gcc/builtins.h b/gcc/builtins.h > index 3b5c34c4802..88a26d70cd5 100644 > --- a/gcc/builtins.h > +++ b/gcc/builtins.h > @@ -105,7 +105,7 @@ struct c_strlen_data > }; > > extern tree c_strlen (tree, int, c_strlen_data * =3D NULL, unsigned =3D = 1); > -extern rtx c_readstr (const char *, scalar_int_mode, bool =3D true); > +extern rtx c_readstr (const char *, fixed_size_mode, bool =3D true); > extern void expand_builtin_setjmp_setup (rtx, rtx); > extern void expand_builtin_setjmp_receiver (rtx); > extern void expand_builtin_update_setjmp_buf (rtx); > diff --git a/gcc/expr.cc b/gcc/expr.cc > index 308ddc09e63..a57948bdf01 100644 > --- a/gcc/expr.cc > +++ b/gcc/expr.cc > @@ -6083,13 +6083,12 @@ string_cst_read_str (void *data, void *, HOST_WID= E_INT offset, > size_t l =3D TREE_STRING_LENGTH (str) - offset; > memcpy (p, TREE_STRING_POINTER (str) + offset, l); > memset (p + l, '\0', GET_MODE_SIZE (mode) - l); > - return c_readstr (p, as_a (mode), false); > + return c_readstr (p, mode, false); > } > > /* The by-pieces infrastructure does not try to pick a vector mode > for storing STRING_CST. */ > - return c_readstr (TREE_STRING_POINTER (str) + offset, > - as_a (mode), false); > + return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false); > } > > /* Generate code for computing expression EXP, > -- > 2.25.1 >