From patchwork Fri Jul 28 11:17:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 127526 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp359886vqg; Fri, 28 Jul 2023 04:18:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlHEaikr5ya1rIijtSkfNumB/6N5QvMKbrRhmyHTD6s/GzHTOzKqTh4hBNynIpkQbO69P4+y X-Received: by 2002:a17:906:32d4:b0:99b:4bab:2838 with SMTP id k20-20020a17090632d400b0099b4bab2838mr1890790ejk.0.1690543110881; Fri, 28 Jul 2023 04:18:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690543110; cv=none; d=google.com; s=arc-20160816; b=NjiSTeQTnFNY75wWbpGHncHTt+QzhJ9AJpsF3+1IZZHP6zzZuSsk3AihcSjjapO4Gy 6cOX9XQ/tvZSB7KTXOeCNH8pLxrG35FEbmhfPAX967dsWI/vDzm3iorVVyknpVIGMqmF 4V5WZNo6fzzTt+gYoL7TJRxZNGPzOQ3WVE03uZzOr/nYIK7LY3HxSLtOwuue9KkBZy8N 8hDh4EuDoJSiET9rpHkGCLKeLXwm6yioS+ALinp1pG+XeWgClApZXY5e8vS4Hn6/zt8m iN0mGX5zU7tyiUGCf32i0i4ElGDRdO6OydJ8Xf5TCayGmNg/EnspU7/MlcS0Ll87rAtp /d8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=NN/UwMVbz7lznX0+M17/Hrv/oD0tt66/FAnbyVJ+aqc=; fh=EAaRr2pUuOZkpjtb1V+TgUxI6x0upVVKdGL7jgyvRYU=; b=YiODOny+xER3h3V0cmtJPn96f1U5sTE3cAeVx8k9gRvTQdgu1kSc9c6qFKF3RsVYYy hWj42x/Fg4j+YOxZjdJVM6IEAAlKqyKyYZ0RwDnJwh9KLMV+FtfN1plyd0v9lAb5ifuL yctjCqPcf+R1p57RJpjAP+dvIJtpc9wAMP5rYaqOXvb1dzl2Cc+jDhoIalM+g05iKqWE SgG9pQ2tRBcqoh9GEyHMPMMHVqUQusn24cm6lsH40x2+aqxxrowiIEZG+V/8N8yLd5lv OPjEUd/7KJ4Krsw6PMzz87iOfCsyR1nUKQZbW8THSMaM2mtmil4cZWl2x9/hO2Lv1IDt +/+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HGDLbBIn; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id rl19-20020a170907217300b0098718777182si2465711ejb.685.2023.07.28.04.18.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jul 2023 04:18:30 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HGDLbBIn; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC602385AF8A for ; Fri, 28 Jul 2023 11:18:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC602385AF8A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690543109; bh=NN/UwMVbz7lznX0+M17/Hrv/oD0tt66/FAnbyVJ+aqc=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=HGDLbBInLREoKH2tOtTSkPEkN/TuisvJ3RJPBY22c0DZxYPKCDgd71QEEt3vKBpt0 gn5wOH0zggc3FDjcjt3gIf1YJRNK6GzTsFivHysadm6f6pQp7F1sfDlrrwv6MRK6Ep oq6InKjatqytE6z3hH0pKU0CKc3QwLBkhhNK7h0o= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 18E653858D20 for ; Fri, 28 Jul 2023 11:17:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 18E653858D20 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-222-sLHKjiphP3yWDkm5eNsm8g-1; Fri, 28 Jul 2023 07:17:38 -0400 X-MC-Unique: sLHKjiphP3yWDkm5eNsm8g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BF1211C05145; Fri, 28 Jul 2023 11:17:37 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7D0DDC5B8C0; Fri, 28 Jul 2023 11:17:37 +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 36SBHY7s2837713 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 28 Jul 2023 13:17:35 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 36SBHXYu2837712; Fri, 28 Jul 2023 13:17:33 +0200 Date: Fri, 28 Jul 2023 13:17:33 +0200 To: Richard Biener , "Joseph S. Myers" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] gimple-fold: Handle _BitInt in __builtin_clear_padding [PR102989] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 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: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772662933245118479 X-GMAIL-MSGID: 1772662933245118479 Hi! The comments about _Atomic _BitInt made me figure out I forgot (although earlier was planning to do that) to implement __builtin_clear_padding support for _BitInt. The following patch (incremental to the _BitInt series) does that. 2023-07-28 Jakub Jelinek PR c/102989 * gimple-fold.cc (clear_padding_unit): Mention in comment that _BitInt types don't need to fit either. (clear_padding_bitint_needs_padding_p): New function. (clear_padding_type_may_have_padding_p): Handle BITINT_TYPE. (clear_padding_type): Likewise. * gcc.dg/bitint-16.c: New test. Jakub --- gcc/gimple-fold.cc.jj 2023-07-11 15:28:54.704679510 +0200 +++ gcc/gimple-fold.cc 2023-07-28 12:37:18.971789595 +0200 @@ -4103,8 +4103,8 @@ gimple_fold_builtin_realloc (gimple_stmt return false; } -/* Number of bytes into which any type but aggregate or vector types - should fit. */ +/* Number of bytes into which any type but aggregate, vector or + _BitInt types should fit. */ static constexpr size_t clear_padding_unit = MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT; /* Buffer size on which __builtin_clear_padding folding code works. */ @@ -4595,6 +4595,26 @@ clear_padding_real_needs_padding_p (tree && (fmt->signbit_ro == 79 || fmt->signbit_ro == 95)); } +/* _BitInt has padding bits if it isn't extended in the ABI and has smaller + precision than bits in limb or corresponding number of limbs. */ + +static bool +clear_padding_bitint_needs_padding_p (tree type) +{ + struct bitint_info info; + gcc_assert (targetm.c.bitint_type_info (TYPE_PRECISION (type), &info)); + if (info.extended) + return false; + scalar_int_mode limb_mode = as_a (info.limb_mode); + if (TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode)) + return true; + else if (TYPE_PRECISION (type) == GET_MODE_PRECISION (limb_mode)) + return false; + else + return (((unsigned) TYPE_PRECISION (type)) + % GET_MODE_PRECISION (limb_mode)) != 0; +} + /* Return true if TYPE might contain any padding bits. */ bool @@ -4611,6 +4631,8 @@ clear_padding_type_may_have_padding_p (t return clear_padding_type_may_have_padding_p (TREE_TYPE (type)); case REAL_TYPE: return clear_padding_real_needs_padding_p (type); + case BITINT_TYPE: + return clear_padding_bitint_needs_padding_p (type); default: return false; } @@ -4855,6 +4877,57 @@ clear_padding_type (clear_padding_struct memset (buf->buf + buf->size, ~0, sz); buf->size += sz; break; + case BITINT_TYPE: + { + struct bitint_info info; + gcc_assert (targetm.c.bitint_type_info (TYPE_PRECISION (type), &info)); + scalar_int_mode limb_mode = as_a (info.limb_mode); + if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode)) + { + gcc_assert ((size_t) sz <= clear_padding_unit); + if ((unsigned HOST_WIDE_INT) sz + buf->size + > clear_padding_buf_size) + clear_padding_flush (buf, false); + if (!info.extended + && TYPE_PRECISION (type) < GET_MODE_PRECISION (limb_mode)) + { + int tprec = GET_MODE_PRECISION (limb_mode); + int prec = TYPE_PRECISION (type); + tree t = build_nonstandard_integer_type (tprec, 1); + tree cst = wide_int_to_tree (t, wi::mask (prec, true, tprec)); + int len = native_encode_expr (cst, buf->buf + buf->size, sz); + gcc_assert (len > 0 && (size_t) len == (size_t) sz); + } + else + memset (buf->buf + buf->size, 0, sz); + buf->size += sz; + break; + } + tree limbtype + = build_nonstandard_integer_type (GET_MODE_PRECISION (limb_mode), 1); + fldsz = int_size_in_bytes (limbtype); + nelts = int_size_in_bytes (type) / fldsz; + for (HOST_WIDE_INT i = 0; i < nelts; i++) + { + if (!info.extended + && i == (info.big_endian ? 0 : nelts - 1) + && (((unsigned) TYPE_PRECISION (type)) + % TYPE_PRECISION (limbtype)) != 0) + { + int tprec = GET_MODE_PRECISION (limb_mode); + int prec = (((unsigned) TYPE_PRECISION (type)) % tprec); + tree cst = wide_int_to_tree (limbtype, + wi::mask (prec, true, tprec)); + int len = native_encode_expr (cst, buf->buf + buf->size, + fldsz); + gcc_assert (len > 0 && (size_t) len == (size_t) fldsz); + buf->size += fldsz; + } + else + clear_padding_type (buf, limbtype, fldsz, for_auto_init); + } + break; + } default: gcc_assert ((size_t) sz <= clear_padding_unit); if ((unsigned HOST_WIDE_INT) sz + buf->size > clear_padding_buf_size) --- gcc/testsuite/gcc.dg/bitint-16.c.jj 2023-07-28 12:47:37.641180784 +0200 +++ gcc/testsuite/gcc.dg/bitint-16.c 2023-07-28 12:47:30.488280320 +0200 @@ -0,0 +1,31 @@ +/* PR c/102989 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-O2 -std=c2x -pedantic-errors" } */ + +_BitInt(15) a; +_BitInt(42) b; +#if __BITINT_MAXWIDTH__ >= 115 +_BitInt(115) c; +#endif +#if __BITINT_MAXWIDTH__ >= 192 +_BitInt(192) d; +#endif +#if __BITINT_MAXWIDTH__ >= 575 +_BitInt(575) e; +#endif + +int +main () +{ + __builtin_clear_padding (&a); + __builtin_clear_padding (&b); +#if __BITINT_MAXWIDTH__ >= 115 + __builtin_clear_padding (&c); +#endif +#if __BITINT_MAXWIDTH__ >= 192 + __builtin_clear_padding (&d); +#endif +#if __BITINT_MAXWIDTH__ >= 575 + __builtin_clear_padding (&e); +#endif +}