From patchwork Fri Jan 12 09:28:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 187627 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2614:b0:101:6a76:bbe3 with SMTP id mm20csp62001dyc; Fri, 12 Jan 2024 01:40:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IH0OEsiXlOQ1qZP9/JHQWzdLkdTupq4v/83gHbnH4ACxM2iFtYIYvC/Y0zB7hw5wF31FeUj X-Received: by 2002:a05:6122:2023:b0:4b8:681f:d43 with SMTP id l35-20020a056122202300b004b8681f0d43mr926722vkd.7.1705052434871; Fri, 12 Jan 2024 01:40:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705052434; cv=pass; d=google.com; s=arc-20160816; b=pZaSa8HCy2Kwf652SSK2mW/0hPupzFgW2nf+ES52D6u3jdZXz+FK3SsQZV3DIubtmr NTsTQ1wZPB2z532BkPvXM2KxHeViMfiY6SZDLJ34XTPGhGcdnpu43GxM7P3w2S/IZehc lgc1JKBP3DK2n9iRZL0+9zK+GdYte6IY/uYurpSRPU5srOO9m8yHYucQn31hXmUx2TPN cXXsRdTNWaUyQ0v3OujuHirwhDcfsk8ZDuI6m1IHEdV42RyBYDxPRxceFcz6tVp303Az VcdG3jBage0O0GlDoZlkyztN/V67PWQMWsnHbPpM2+kJJ7PAI/zUshq03gp67iqlzc1E p6+g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:resent-to :resent-message-id:resent-date:resent-from:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=SFSJq16KGHaDm2byJ/l2cIHAr5jzpaSLpeE3eC9iEls=; fh=6Jq54U7/S3hLEk833LS+nb8PUV/tyyLGSWSrl1I2JxE=; b=cQ8C1oZYW8hlRMYgzaQBjtPZlmfqmDGomVEhHCFa6+PGhkgOiMGO4ytUcfPeV3/qjh Cbk/JHnnjf6vDGAQGooIAxnGyBRrxzz8HA4+kI2PGhx/zseBRvoOI9RYuLJB4coxtUyb TS+3VDNLk6cXnxVBu9UTQPac1Q/1MU0WeyGK4FQuqrg8K1gfKfvRIDFQzcPwMCZDxeH4 nMAqBTSRAcsaOkZoJ0W7CtDVgfU5CVoBSjEBBbIB1snyWUtQ4thZaPy6CDnsWm6To/y9 rFCSHy6qhd8Sqg9+HP7Q7s+/Gwv/4GGSb8mraZqnUfRVmG5B3vka3GH/IV/3rK+JR+x5 Rfcw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fn8OhpP2; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id f17-20020a05620a15b100b0078321bb769fsi2491236qkk.714.2024.01.12.01.40.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 01:40:34 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=fn8OhpP2; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8F21C38582BE for ; Fri, 12 Jan 2024 09:40:34 +0000 (GMT) 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 8111D385829F for ; Fri, 12 Jan 2024 09:39:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8111D385829F 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 8111D385829F 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=1705052369; cv=none; b=mu4ZPRbwJOnL24r6PNgQqc//dhhZl7EXSUXFXAX6lCL2yXkYHkiy0bOu4Lpn3+u78fTzKIQEuZDEnpcyfwTm/7RHlXQWzFNbet1LwaBa/PKeYL8YXgp2WSVkpaHrk9nr6XAWob3WdW2bbzE8r4U3+0zvIULAOLQBxw+6cg+7F/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705052369; c=relaxed/simple; bh=2/kJl2KjY2C7h3IHqaKYqW3190wWWXKp4g94G1rYe4w=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=nIoAuXUeFPuQY8enSRMvnCj04LSavgUzlKxlLc0DsjYweX7Qr2b6qythTWkVwrKAYDsai7bCgFWDyq1zAZbEfeRVFJkgwCGALH4lV9J0dT19TlSI32jkOKzij5C+umNvNSjvksA9oY7v0gDYtr4pJO8pmAD1TQ+KeKdOB6PioTY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705052364; 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:resent-to:resent-from:resent-message-id; bh=SFSJq16KGHaDm2byJ/l2cIHAr5jzpaSLpeE3eC9iEls=; b=fn8OhpP21EoVVHS3jct4vL8yHcAgcfqMooRoLWxM+CoNl16A6UX+Avp+/CXrwyqpL1w+1o s9Jb2v60AF50W5P5CLeSKYG4b6S+l9BnJ8qilHqkoGln5U0GI8m1SNlc8zJ1Q33D7PBTGh jZTgyxDjvmxEM2bUuLXcqd+2OUYza8g= 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-468-MZnQhy5rOlW6W1RF7xyYUQ-1; Fri, 12 Jan 2024 04:39:22 -0500 X-MC-Unique: MZnQhy5rOlW6W1RF7xyYUQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 2AE8585A588 for ; Fri, 12 Jan 2024 09:39:22 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DB10C1C0652C; Fri, 12 Jan 2024 09:39:21 +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 40C9dJra3307024 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 12 Jan 2024 10:39:19 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 40C9dJ1v3307023; Fri, 12 Jan 2024 10:39:19 +0100 Resent-From: Jakub Jelinek Resent-Date: Fri, 12 Jan 2024 10:39:18 +0100 Resent-Message-ID: Resent-To: Joseph Myers , Marek Polacek , gcc-patches@gcc.gnu.org Date: Fri, 12 Jan 2024 10:28:51 +0100 From: Jakub Jelinek To: Joseph Myers , Marek Polacek Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c: Avoid _BitInt indexes > sizetype in ARRAY_REFs [PR113315] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.4 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_H3, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787877061510875787 X-GMAIL-MSGID: 1787877061510875787 Hi! When build_array_ref doesn't use ARRAY_REF, it casts the index to sizetype already, performs POINTER_PLUS_EXPR and then dereferences. While when emitting ARRAY_REF, we try to keep index expression as is in whatever type it had, which is reasonable e.g. for signed or unsigned types narrower than sizetype for loop optimizations etc. But if the index is wider than sizetype, we are unnecessarily computing bits beyond what is needed. For {,unsigned }__int128 on 64-bit arches or {,unsigned }long long on 32-bit arches we've been doing that for decades, so the following patch doesn't propose to change that (might be stage1 material), but for _BitInt at least the _BitInt lowering code doesn't expect to see large/huge _BitInt in the ARRAY_REF indexes, I was expecting one would see just casts of those to sizetype. So, the following patch makes sure that large/huge _BitInt indexes don't appear in ARRAY_REFs. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-01-12 Jakub Jelinek PR c/113315 * c-typeck.cc (build_array_ref): If index has BITINT_TYPE type with precision larger than sizetype precision, convert it to sizetype. * gcc.dg/bitint-65.c: New test. * gcc.dg/bitint-66.c: New test. Jakub --- gcc/c/c-typeck.cc.jj 2024-01-03 12:06:52.940863462 +0100 +++ gcc/c/c-typeck.cc 2024-01-11 12:55:05.457899186 +0100 @@ -2858,6 +2858,10 @@ build_array_ref (location_t loc, tree ar "array"); } + if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE + && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype)) + index = fold_convert (sizetype, index); + type = TREE_TYPE (TREE_TYPE (array)); rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE); /* Array ref is const/volatile if the array elements are --- gcc/testsuite/gcc.dg/bitint-65.c.jj 2024-01-11 13:03:27.843827305 +0100 +++ gcc/testsuite/gcc.dg/bitint-65.c 2024-01-11 13:03:04.807154223 +0100 @@ -0,0 +1,23 @@ +/* PR c/113315 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23" } */ + +#if __BITINT_MAXWIDTH__ >= 535 +_BitInt(535) x; +#else +_BitInt(64) x; +#endif +extern int a[]; +extern char b[][10]; + +int +foo (void) +{ + return a[x]; +} + +int +bar (void) +{ + return __builtin_strlen (b[x]); +} --- gcc/testsuite/gcc.dg/bitint-66.c.jj 2024-01-11 13:08:40.561399890 +0100 +++ gcc/testsuite/gcc.dg/bitint-66.c 2024-01-11 13:09:07.512019458 +0100 @@ -0,0 +1,12 @@ +/* PR c/113315 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23 -O2" } */ + +extern int a[5]; + +int +foo (void) +{ + _BitInt(535) i = 1; + return a[i]; +}