c: Avoid _BitInt indexes > sizetype in ARRAY_REFs [PR113315]
commit95440171d0e615b0a1aa6863e41ba30df86ac3f4
authorJakub Jelinek <jakub@redhat.com>
Fri, 12 Jan 2024 16:11:49 +0000 (12 17:11 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 12 Jan 2024 16:11:49 +0000 (12 17:11 +0100)
treee79f7cf35a25b7dc446a2bb3629fd139bbdc2d97
parentd14ef0987de2f6f2dac64f4f0f068b929078a01d
c: Avoid _BitInt indexes > sizetype in ARRAY_REFs [PR113315]

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.

2024-01-12  Jakub Jelinek  <jakub@redhat.com>

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.
gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/bitint-65.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/bitint-66.c [new file with mode: 0644]