]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end/113396 - int128 array index and value-ranges
authorRichard Biener <rguenther@suse.de>
Tue, 19 Mar 2024 14:25:16 +0000 (15:25 +0100)
committerRichard Biener <rguenther@suse.de>
Wed, 8 May 2024 10:50:23 +0000 (12:50 +0200)
The following fixes bogus truncation of a value-range for an int128
array index when computing the maximum extent for a variable array
reference.  Instead of possibly slowing things down by using
widest_int the following makes sure the range bounds fit within
the constraints offset_int were designed for.

PR middle-end/113396
* tree-dfa.cc (get_ref_base_and_extent): Use index range
bounds only if they fit within the address-range constraints
of offset_int.

* gcc.dg/torture/pr113396.c: New testcase.

(cherry picked from commit 6a55e39bdb1fdb570730c08413ebbe744e493411)

gcc/testsuite/gcc.dg/torture/pr113396.c [new file with mode: 0644]
gcc/tree-dfa.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr113396.c b/gcc/testsuite/gcc.dg/torture/pr113396.c
new file mode 100644 (file)
index 0000000..585f717
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int128 } */
+
+unsigned char m[] = {5, 79, 79, 79, 79};
+__int128 p;
+int main()
+{
+  int g1 = 0;
+  p = 0;
+  for (int aj = 0; aj < 256; aj++)
+   {
+      m[0] = -4;
+      for (; p >= 0; p -= 1) {
+        g1 = m[p];
+      }
+  }
+  if (g1 != 0xfc)
+    __builtin_abort();
+}
index 82803a8ccb1d3e4151def9be5d5d2694e29a96f3..eced5c61ca54dd4284a2dc7b2ca0254b4b0f889c 100644 (file)
@@ -550,7 +550,8 @@ get_ref_base_and_extent (tree exp, poly_int64_pod *poffset,
                    /* Try to constrain maxsize with range information.  */
                    offset_int omax
                      = offset_int::from (max, TYPE_SIGN (TREE_TYPE (index)));
-                   if (known_lt (lbound, omax))
+                   if (wi::get_precision (max) <= ADDR_MAX_BITSIZE
+                       && known_lt (lbound, omax))
                      {
                        poly_offset_int rmaxsize;
                        rmaxsize = (omax - lbound + 1)
@@ -568,7 +569,8 @@ get_ref_base_and_extent (tree exp, poly_int64_pod *poffset,
                    /* Try to adjust bit_offset with range information.  */
                    offset_int omin
                      = offset_int::from (min, TYPE_SIGN (TREE_TYPE (index)));
-                   if (known_le (lbound, omin))
+                   if (wi::get_precision (min) <= ADDR_MAX_BITSIZE
+                       && known_le (lbound, omin))
                      {
                        poly_offset_int woffset
                          = wi::sext (omin - lbound,