aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2024-12-28 15:42:56 +0100
committerJakub Jelinek <jakub@gcc.gnu.org>2024-12-28 15:42:56 +0100
commit65c8fd7b017482c6d6bd0c7a7c6e296f016e38d0 (patch)
tree5c9356c9a68f8e99b0622b25b24dd50b15b38e86
parenta326ecf5411897e975fb24696d3267bbe496627c (diff)
downloadgcc-65c8fd7b017482c6d6bd0c7a7c6e296f016e38d0.zip
gcc-65c8fd7b017482c6d6bd0c7a7c6e296f016e38d0.tar.gz
gcc-65c8fd7b017482c6d6bd0c7a7c6e296f016e38d0.tar.bz2
gimple-fold: Fix up fold_array_ctor_reference RAW_DATA_CST handling [PR118207]
The following testcases ICE because fold_array_ctor_reference in the RAW_DATA_CST handling just return build_int_cst without actually checking that if type is non-NULL, TREE_TYPE (val) is uselessly convertible to it. By falling through the code after it without *suboff += we get everything we need, the two if conditionals will never be true (we've already checked that size == BITS_PER_UNIT and so can't be 0, and val will be INTEGER_CST), but it will do the important fold_ctor_reference call which will deal with type incompatibilities. 2024-12-28 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/118207 * gimple-fold.cc (fold_array_ctor_reference): For RAW_DATA_CST, just set val to build_int_cst and fall through to the normal element handling code instead of returning build_int_cst right away. * gcc.dg/pr118207.c: New test.
-rw-r--r--gcc/gimple-fold.cc3
-rw-r--r--gcc/testsuite/c-c++-common/cpp/embed-29.c17
-rw-r--r--gcc/testsuite/gcc.dg/pr118207.c25
3 files changed, 43 insertions, 2 deletions
diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
index 2d6e207..b0353de 100644
--- a/gcc/gimple-fold.cc
+++ b/gcc/gimple-fold.cc
@@ -9707,9 +9707,8 @@ fold_array_ctor_reference (tree type, tree ctor,
constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
return NULL_TREE;
- *suboff += access_index.to_uhwi () * BITS_PER_UNIT;
unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
- return build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
+ val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
}
if (!size && TREE_CODE (val) != CONSTRUCTOR)
{
diff --git a/gcc/testsuite/c-c++-common/cpp/embed-29.c b/gcc/testsuite/c-c++-common/cpp/embed-29.c
new file mode 100644
index 0000000..0c524ef
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/embed-29.c
@@ -0,0 +1,17 @@
+/* PR tree-optimization/118207 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+struct A { unsigned char a; };
+struct B { struct A b; };
+static const unsigned char c[] = {
+#embed __FILE__
+};
+struct B d;
+
+void
+foo ()
+{
+ const struct B *t = (const struct B *) &c;
+ d.b = t->b;
+}
diff --git a/gcc/testsuite/gcc.dg/pr118207.c b/gcc/testsuite/gcc.dg/pr118207.c
new file mode 100644
index 0000000..6366721
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr118207.c
@@ -0,0 +1,25 @@
+/* PR tree-optimization/118207 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+struct A { unsigned char a; };
+struct B { struct A b; };
+static const unsigned char c[160] = {
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
+struct B d;
+
+void
+foo ()
+{
+ const struct B *t = (const struct B *) &c;
+ d.b = t->b;
+}