From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 7AC733858406 for ; Sat, 24 Sep 2022 18:57:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7AC733858406 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-pj1-x1033.google.com with SMTP id p14so321725pjd.3 for ; Sat, 24 Sep 2022 11:57:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=gS9f9tZzqQP+Gs2cEuCIebHdDZPkeDjhflydKEaav5c=; b=htn8z6E8Sn43UhmnPEbCkWTNpC0n3tX2s9i5tj7H7q3v/vTCg7FYvsjCu7fTFAAv2V xRgczTs4Jo7wMuRZrMayGRPCTTpCX5ISuBFzMXogtkrjGmxn66PqWfOge7WcUHkN9hiE lQmlWOTtIM/5lryNKTddpADS3aL4el3Vh2iiokt8njGxSuRdNuCUds7H4pSFbu0Sn+I3 d4GiU2wcCinqf2FjicZujtsUy87nrPVyAbePe9yhow6yNfp72ImsGd40y6aZUXF48JUz evULrt2yID99t5yy3IKSRLei4JjMnNmt0F6TUfSnFMbjwv02RQaVmzwtwKl4DvydXOU8 +oYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=gS9f9tZzqQP+Gs2cEuCIebHdDZPkeDjhflydKEaav5c=; b=0KLucRiES7LLKHlz8c5wzW1o+qZbp2DB8y0dj8FE5BcMR4yAzlomZgsxPC8+e3RMM4 l6NgOzOz3S678ZHXf8N0AckNG6Or4QK7nTSberi7aA9YQzSUdnib1pS7OKUTuGobmKCM cxj0Z3UzXKtTnMAfTnjaxnL3as3RwUGA0M9JSJ3LPO9d26g4/giFfiDWrSENR9etVZiM 9gLDp04k2Y84OHaJGWUVHpn0TiNQNeE2jEz5tAgp4tanctCVO++E3Nl1lKvN/z++7XMc rbR+G4uh/edCPJ4lvtId2MrPI+fyv7XCuhYPsnTQhsMCUWSk+JzRK1MtDmJP7/xnvSaO MI3A== X-Gm-Message-State: ACrzQf1WZuqQfH0c44qjXhoUPgfak+f0zdtqJiqPqzArG+A98ZASVsXi lS/u6V+gTpqCgiO0OOdHgO4BZaM5cmckEHSOsoc= X-Google-Smtp-Source: AMsMyM6F1ZOOv3IYp5xM1Roaky4FkaCZaw0f/qA4h9O+quaoHYLVolIgyktIx0Rqv4hAGr2sC7s/qcuU7LnMkv3+vKY= X-Received: by 2002:a17:902:ef93:b0:178:93cf:d267 with SMTP id iz19-20020a170902ef9300b0017893cfd267mr14341596plb.123.1664045847532; Sat, 24 Sep 2022 11:57:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Andrew Pinski Date: Sat, 24 Sep 2022 11:57:14 -0700 Message-ID: Subject: Re: [PATCH 1/2]middle-end Fold BIT_FIELD_REF and Shifts into BIT_FIELD_REFs alone To: Tamar Christina Cc: gcc-patches@gcc.gnu.org, nd@arm.com, rguenther@suse.de Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.8 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 Fri, Sep 23, 2022 at 4:43 AM Tamar Christina via Gcc-patches wrote: > > Hi All, > > This adds a match.pd rule that can fold right shifts and bit_field_refs of > integers into just a bit_field_ref by adjusting the offset and the size of the > extract and adds an extend to the previous size. > > Concretely turns: > > #include > > unsigned int foor (uint32x4_t x) > { > return x[1] >> 16; > } > > which used to generate: > > _1 = BIT_FIELD_REF ; > _3 = _1 >> 16; > > into > > _4 = BIT_FIELD_REF ; > _2 = (unsigned int) _4; > > I currently limit the rewrite to only doing it if the resulting extract is in > a mode the target supports. i.e. it won't rewrite it to extract say 13-bits > because I worry that for targets that won't have a bitfield extract instruction > this may be a de-optimization. It is only a de-optimization for the following case: * vector extraction All other cases should be handled correctly in the middle-end when expanding to RTL because they need to be handled for bit-fields anyways. Plus SIGN_EXTRACT and ZERO_EXTRACT would be used in the integer case for the RTL. Getting SIGN_EXTRACT/ZERO_EXTRACT early on in the RTL is better than waiting until combine really. > > Bootstrapped Regtested on aarch64-none-linux-gnu, x86_64-pc-linux-gnu > and no issues. > > Testcase are added in patch 2/2. > > Ok for master? > > Thanks, > Tamar > > gcc/ChangeLog: > > * match.pd: Add bitfield and shift folding. > > --- inline copy of patch -- > diff --git a/gcc/match.pd b/gcc/match.pd > index 1d407414bee278c64c00d425d9f025c1c58d853d..b225d36dc758f1581502c8d03761544bfd499c01 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -7245,6 +7245,23 @@ 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 shifts to BIT_FIELD_REFS. */ > +(for shift (rshift) > + op (plus) > + (simplify > + (shift (BIT_FIELD_REF @0 @1 @2) integer_pow2p@3) > + (if (INTEGRAL_TYPE_P (type)) > + (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, 1); } Maybe use `build_nonstandard_integer_type (newsize, /* unsignedp = */ true);` or better yet `build_nonstandard_integer_type (newsize, UNSIGNED);` I had started to convert some of the unsignedp into enum signop but I never finished or submitted the patch. Thanks, Andrew Pinski > + (if (ntype) > + (convert:type (BIT_FIELD_REF:ntype @0 { nsize; } (op @2 @3)))))))) > + > (simplify > (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4) > (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); })) > > > > > --