From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 04C783858426 for ; Sat, 16 Mar 2024 07:10:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04C783858426 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 04C783858426 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710573044; cv=none; b=S7oi061aO7p4fEB0vzcy88blzJqKmD2T6CzNG/Uud1AXdtKQmBMO3PgZ6bugtlVO80q40EusSiingurpTS0yrTj5XJSphcOLUUxNEqplhZXKoqW1kdP/xVLn8T1OUHA+2Ji9JFIDfqI+K+qERpCGbwiLmovzGOwnf07zbnL91vE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710573044; c=relaxed/simple; bh=a/Lzu4FgHBQAmLVIWdYZSsMbSll6UWPTNxHwA6qMLLo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=mRuRT4gGxYi+RooKE1KjHJEAFNlTbMAGD29EVQusETu0VXs3OWbUNcl+qV4Q7zRJnchuHzYL5sRUc7MaumVO95/xr9BMPBvjFCDjei5tuiFNKI0sU24AiP4wOmCKzbgnfg4WCSJ9kyL8NAcfy7gxpECvISFcZVG8/tB8pKV6afw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710573042; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=h3aLaoRU+Y9ALUIxEylYyGL6HZ7G5po7EYtOc5AzPFE=; b=SA3yvzQKFRlbdvGEe08J9BMvpBQKktKhDNtq6AVl6Ko1POs09jetKLKb1XZSSdrt0jH4b0 3VQR21FFvGWUeX1/PlxgGuXTB2YEeTN8HaG7EDpc9r5fG7clAMbWIu+aW1+A3eCvWN1ZbK BR+6lqMyURaSP5QVS05OA70oAV6uX7Y= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-520-U_gyjSFiPKWHfpEPYfuziw-1; Sat, 16 Mar 2024 03:10:39 -0400 X-MC-Unique: U_gyjSFiPKWHfpEPYfuziw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F010485A58B; Sat, 16 Mar 2024 07:10:38 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.57]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B495E492BD1; Sat, 16 Mar 2024 07:10:38 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 42G7AWw51944214 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 16 Mar 2024 08:10:32 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 42G7AV391944213; Sat, 16 Mar 2024 08:10:31 +0100 Date: Sat, 16 Mar 2024 08:10:31 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] bitint: Fix up stores to large/huge _BitInt bitfields [PR114329] Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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 List-Id: Hi! The verifier requires BIT_FIELD_REFs with INTEGRAL_TYPE_P first operand to have mode precision. In most cases for the large/huge _BitInt bitfield stores the code uses bitfield representatives, which are typically arrays of chars, but if the bitfield starts at byte boundary on big endian, the code uses as nlhs in lower_mergeable_store COMPONENT_REF of the bitfield FIELD_DECL instead, which is fine for the limb accesses, but when used for the most significant limb can result in invalid BIT_FIELD_REF because the first operand then has BITINT_TYPE and usually VOIDmode. The following patch adds a helper method for the 4 creatikons of BIT_FIELD_REF which when needed adds a VIEW_CONVERT_EXPR. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-03-16 Jakub Jelinek PR tree-optimization/114329 * gimple-lower-bitint.cc (struct bitint_large_huge): Declare build_bit_field_ref method. (bitint_large_huge::build_bit_field_ref): New method. (bitint_large_huge::lower_mergeable_stmt): Use it. * gcc.dg/bitint-101.c: New test. --- gcc/gimple-lower-bitint.cc.jj 2024-03-15 09:16:36.464033118 +0100 +++ gcc/gimple-lower-bitint.cc 2024-03-15 17:53:30.200276578 +0100 @@ -427,6 +427,8 @@ struct bitint_large_huge void insert_before (gimple *); tree limb_access_type (tree, tree); tree limb_access (tree, tree, tree, bool); + tree build_bit_field_ref (tree, tree, unsigned HOST_WIDE_INT, + unsigned HOST_WIDE_INT); void if_then (gimple *, profile_probability, edge &, edge &); void if_then_else (gimple *, profile_probability, edge &, edge &); void if_then_if_then_else (gimple *g, gimple *, @@ -659,6 +661,31 @@ bitint_large_huge::limb_access (tree typ return ret; } +/* Build a BIT_FIELD_REF to access BITSIZE bits with FTYPE type at + offset BITPOS inside of OBJ. */ + +tree +bitint_large_huge::build_bit_field_ref (tree ftype, tree obj, + unsigned HOST_WIDE_INT bitsize, + unsigned HOST_WIDE_INT bitpos) +{ + if (INTEGRAL_TYPE_P (TREE_TYPE (obj)) + && !type_has_mode_precision_p (TREE_TYPE (obj))) + { + unsigned HOST_WIDE_INT nelts + = CEIL (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (obj))), limb_prec); + tree ltype = m_limb_type; + addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (obj)); + if (as != TYPE_ADDR_SPACE (ltype)) + ltype = build_qualified_type (ltype, TYPE_QUALS (ltype) + | ENCODE_QUAL_ADDR_SPACE (as)); + tree atype = build_array_type_nelts (ltype, nelts); + obj = build1 (VIEW_CONVERT_EXPR, atype, obj); + } + return build3 (BIT_FIELD_REF, ftype, obj, bitsize_int (bitsize), + bitsize_int (bitpos)); +} + /* Emit a half diamond, if (COND) |\ @@ -2651,9 +2678,9 @@ bitint_large_huge::lower_mergeable_stmt } tree ftype = build_nonstandard_integer_type (limb_prec - bo_bit, 1); - tree bfr = build3 (BIT_FIELD_REF, ftype, unshare_expr (nlhs), - bitsize_int (limb_prec - bo_bit), - bitsize_int (bo_idx * limb_prec + bo_bit)); + tree bfr = build_bit_field_ref (ftype, unshare_expr (nlhs), + limb_prec - bo_bit, + bo_idx * limb_prec + bo_bit); tree t = add_cast (ftype, rhs1); g = gimple_build_assign (bfr, t); insert_before (g); @@ -2714,12 +2741,11 @@ bitint_large_huge::lower_mergeable_stmt { tree ftype = build_nonstandard_integer_type (rprec + bo_bit, 1); - tree bfr = build3 (BIT_FIELD_REF, ftype, - unshare_expr (nlhs), - bitsize_int (rprec + bo_bit), - bitsize_int ((bo_idx - + tprec / limb_prec) - * limb_prec)); + tree bfr + = build_bit_field_ref (ftype, unshare_expr (nlhs), + rprec + bo_bit, + (bo_idx + tprec / limb_prec) + * limb_prec); tree t = add_cast (ftype, rhs1); g = gimple_build_assign (bfr, t); done = true; @@ -2860,11 +2886,11 @@ bitint_large_huge::lower_mergeable_stmt { tree ftype = build_nonstandard_integer_type (rprec + bo_bit, 1); - tree bfr = build3 (BIT_FIELD_REF, ftype, - unshare_expr (nlhs), - bitsize_int (rprec + bo_bit), - bitsize_int ((bo_idx + tprec / limb_prec) - * limb_prec)); + tree bfr + = build_bit_field_ref (ftype, unshare_expr (nlhs), + rprec + bo_bit, + (bo_idx + tprec / limb_prec) + * limb_prec); tree t = add_cast (ftype, rhs1); g = gimple_build_assign (bfr, t); done = true; @@ -2909,10 +2935,10 @@ bitint_large_huge::lower_mergeable_stmt unsigned int tprec = TYPE_PRECISION (type); unsigned int rprec = tprec % limb_prec; tree ftype = build_nonstandard_integer_type (rprec + bo_bit, 1); - tree bfr = build3 (BIT_FIELD_REF, ftype, unshare_expr (nlhs), - bitsize_int (rprec + bo_bit), - bitsize_int ((bo_idx + tprec / limb_prec) - * limb_prec)); + tree bfr = build_bit_field_ref (ftype, unshare_expr (nlhs), + rprec + bo_bit, + (bo_idx + tprec / limb_prec) + * limb_prec); rhs1 = bf_cur; if (bf_cur != ext) { --- gcc/testsuite/gcc.dg/bitint-101.c.jj 2024-03-15 18:59:01.386413294 +0100 +++ gcc/testsuite/gcc.dg/bitint-101.c 2024-03-15 18:58:14.063059994 +0100 @@ -0,0 +1,17 @@ +/* PR tree-optimization/114329 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23" } */ + +#if __BITINT_MAXWIDTH__ >= 129 +#define N 129 +#else +#define N 63 +#endif + +struct S { _BitInt(N) b : N; } s; + +void +foo (void) +{ + s.b ^= 42; +} Jakub