]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end/118692 - ICE with out-of-bound ref expansion
authorRichard Biener <rguenther@suse.de>
Wed, 29 Jan 2025 14:09:35 +0000 (15:09 +0100)
committerRichard Biener <rguenth@gcc.gnu.org>
Thu, 30 Jan 2025 09:03:02 +0000 (10:03 +0100)
The following guards the BIT_FIELD_REF expansion fallback for
MEM_REFs of entities expanded to register (or constant) further,
avoiding large out-of-bound offsets by, when the access does not
overlap the base object, expanding the offset as if it were zero.

PR middle-end/118692
* expr.cc (expand_expr_real_1): When expanding a MEM_REF
as BIT_FIELD_REF avoid large offsets for accesses not
overlapping the base object.

* gcc.dg/pr118692.c: New testcase.

gcc/expr.cc
gcc/testsuite/gcc.dg/pr118692.c [new file with mode: 0644]

index 7f3149b85eec5e48ef574b3fc71ad5f8db54baa4..10467f82c0d24757cf59f28199ee3822bc1c569b 100644 (file)
@@ -11806,6 +11806,14 @@ expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
                  set_mem_size (temp, int_size_in_bytes (type));
                return temp;
              }
+           /* When the access is fully outside of the underlying object
+              expand the offset as zero.  This avoids out-of-bound
+              BIT_FIELD_REFs and generates smaller code for these cases
+              with UB.  */
+           type_size = tree_to_poly_uint64 (TYPE_SIZE_UNIT (type));
+           if (!ranges_maybe_overlap_p (offset, type_size, 0,
+                                        GET_MODE_SIZE (DECL_MODE (base))))
+             offset = 0;
            exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
                          bitsize_int (offset * BITS_PER_UNIT));
            REF_REVERSE_STORAGE_ORDER (exp) = reverse;
diff --git a/gcc/testsuite/gcc.dg/pr118692.c b/gcc/testsuite/gcc.dg/pr118692.c
new file mode 100644 (file)
index 0000000..45fba56
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+_Complex double cf;
+
+void
+foo(char c)
+{
+  cf += *(_Complex double *)__builtin_memcpy(8143523003042804629LL + &c, 0, 0);
+}