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: 83975 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8E4BA3858016 for ; Fri, 12 Jan 2024 09:40:14 +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+patchwork=sourceware.org@gcc.gnu.org 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]; +}