From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id E5AF03858C54 for ; Mon, 7 Nov 2022 13:29:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E5AF03858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id B4EC522417; Mon, 7 Nov 2022 13:29:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1667827765; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=CTscqLkJjzT6Qi5A1legkQbGSQ/qrGWFBE5kJONhpHo=; b=t7Ei/Yb42tOSHW9NTKnTf8R1w8mV3GF5bS8p802c1j9JRAor6SaHQXasoiqb9G7VOJmiyx qs2ex+uXNtviO34fGhxXb1fPuUOZXtSpcYPtRJkg32Fmrj5KpqfDNGXXRXMthmfEidKJ5C rVoh9+MQ5l6uKrFoIwsBidRaka/tvog= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1667827765; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=CTscqLkJjzT6Qi5A1legkQbGSQ/qrGWFBE5kJONhpHo=; b=z2+w/g2OJeKppGgbytwTfC7+l9xYtaXuEl814DX5w8c7Y5epJ4VLgA+SWHoY9tU+gLexCY i0GlFexumkcmRnCg== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id AC52A2C141; Mon, 7 Nov 2022 13:29:25 +0000 (UTC) Date: Mon, 7 Nov 2022 13:29:25 +0000 (UTC) From: Richard Biener To: Tamar Christina cc: Richard Biener , Andrew Pinski , nd , "gcc-patches@gcc.gnu.org" Subject: RE: [PATCH 1/2]middle-end Fold BIT_FIELD_REF and Shifts into BIT_FIELD_REFs alone In-Reply-To: Message-ID: References: User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII 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,KAM_LOTSOFHASH,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 Mon, 31 Oct 2022, Tamar Christina wrote: > Hi All, > > Here's a respin addressing review comments. > > Bootstrapped Regtested on aarch64-none-linux-gnu, x86_64-pc-linux-gnu > and no issues. > > Ok for master? > > Thanks, > Tamar > > gcc/ChangeLog: > > * match.pd: Add bitfield and shift folding. > > gcc/testsuite/ChangeLog: > > * gcc.dg/bitshift_1.c: New. > * gcc.dg/bitshift_2.c: New. > > --- inline copy of patch --- > > diff --git a/gcc/match.pd b/gcc/match.pd > index 70e90cdbfa902830e6b58be84e114e86ff7b4dff..a4ad465b2b074b21835be74732dce295f8db03bc 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -7245,6 +7245,45 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > && ANY_INTEGRAL_TYPE_P (type) && ANY_INTEGRAL_TYPE_P (TREE_TYPE(@0))) > (IFN_REDUC_PLUS_WIDEN @0))) > > +/* Canonicalize BIT_FIELD_REFS and right shift to BIT_FIELD_REFS. */ > +(simplify > + (rshift (BIT_FIELD_REF @0 @1 @2) INTEGER_CST@3) > + (if (INTEGRAL_TYPE_P (type) > + && tree_fits_uhwi_p (@1) > + && tree_fits_uhwi_p (@3)) > + (with { /* Can't use wide-int here as the precision differs between > + @1 and @3. */ > + unsigned HOST_WIDE_INT size = tree_to_uhwi (@1); > + unsigned HOST_WIDE_INT shiftc = tree_to_uhwi (@3); > + unsigned HOST_WIDE_INT newsize = size - shiftc; > + tree nsize = wide_int_to_tree (bitsizetype, newsize); > + tree ntype > + = build_nonstandard_integer_type (newsize, TYPE_UNSIGNED (type)); } > + (switch > + (if (INTEGRAL_TYPE_P (ntype) && !BYTES_BIG_ENDIAN) > + (convert:type (BIT_FIELD_REF:ntype @0 { nsize; } (plus @2 @3)))) the :type is not necessary. Don't you need to verify that (plus/minus @2 @3) is in bounds? > + (if (INTEGRAL_TYPE_P (ntype) && BYTES_BIG_ENDIAN) > + (convert:type (BIT_FIELD_REF:ntype @0 { nsize; } (minus @2 @3)))))))) > + > +/* Canonicalize BIT_FIELD_REFS and converts to BIT_FIELD_REFS. */ > +(simplify > + (convert (BIT_FIELD_REF@3 @0 @1 @2)) > + (if (INTEGRAL_TYPE_P (type) > + && INTEGRAL_TYPE_P (TREE_TYPE (@3))) > + (with { unsigned int size_inner = element_precision (TREE_TYPE (@3)); > + unsigned int size_outer = element_precision (type); } since you check for INTEGRAL_TYPE_P using element_precision is odd, just use TYPE_PRECISION here. > + (if (size_inner > size_outer) > + /* Truncating convert, we can shrink the bit field similar to the > + shift case. */ > + (with { > + tree nsize = wide_int_to_tree (bitsizetype, size_outer); bitsize_int > + auto sign = TYPE_UNSIGNED (type); > + tree ntype > + = build_nonstandard_integer_type (size_outer, sign); > + gcc_assert (useless_type_conversion_p (type, ntype)); } if it's the same type why re-build it? > + (if (INTEGRAL_TYPE_P (ntype)) since you build a nonstandard integer type that's always going to be INTEGRAL_TYPE_P. > + (BIT_FIELD_REF:ntype @0 { nsize; } @2))))))) so why not simply (if (size_inner > size_outer) (BIT_FIELD_REF @0 { bitsize_int (size_outer); } @2)) ? > + > (simplify > (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4) > (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); })) > diff --git a/gcc/testsuite/gcc.dg/bitshift_1.c b/gcc/testsuite/gcc.dg/bitshift_1.c > new file mode 100644 > index 0000000000000000000000000000000000000000..5995d0746d2301eb48304629cb4b779b079f1270 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/bitshift_1.c > @@ -0,0 +1,50 @@ > +/* { dg-do compile { target le } } */ > +/* { dg-additional-options "-O2 -save-temps -fdump-tree-optimized" } */ > + > +typedef int v4si __attribute__ ((vector_size (16))); > +typedef unsigned int v4usi __attribute__ ((vector_size (16))); > +typedef unsigned short v8uhi __attribute__ ((vector_size (16))); > + > +unsigned int foor (v4usi x) > +{ > + return x[1] >> 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned int fool (v4usi x) > +{ > + return x[1] << 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned short foor2 (v4usi x) > +{ > + return x[3] >> 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned int fool2 (v4usi x) > +{ > + return x[0] << 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned char foor3 (v8uhi x) > +{ > + return x[3] >> 9; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned short fool3 (v8uhi x) > +{ > + return x[0] << 9; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned short foo2 (v4si x) > +{ > + int y = x[0] + x[1]; > + return y >> 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > diff --git a/gcc/testsuite/gcc.dg/bitshift_2.c b/gcc/testsuite/gcc.dg/bitshift_2.c > new file mode 100644 > index 0000000000000000000000000000000000000000..406b4def9d4aebbc83bd5bef92dab825b85f2aa4 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/bitshift_2.c > @@ -0,0 +1,49 @@ > +/* { dg-do compile { target be } } */ > +/* { dg-additional-options "-O2 -save-temps -fdump-tree-optimized" } */ > + > +typedef int v4si __attribute__ ((vector_size (16))); > +typedef unsigned int v4usi __attribute__ ((vector_size (16))); > +typedef unsigned short v8uhi __attribute__ ((vector_size (16))); > + > +unsigned int foor (v4usi x) > +{ > + return x[1] >> 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned int fool (v4usi x) > +{ > + return x[1] << 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned short foor2 (v4usi x) > +{ > + return x[3] >> 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned int fool2 (v4usi x) > +{ > + return x[0] << 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned char foor3 (v8uhi x) > +{ > + return x[3] >> 9; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned short fool3 (v8uhi x) > +{ > + return x[0] << 9; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > + > +unsigned short foo2 (v4si x) > +{ > + int y = x[0] + x[1]; > + return y >> 16; > +} > +/* { dg-final { scan-tree-dump {BIT_FIELD_REF ;} "optimized" } } */ > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)