]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
combine: Partially revert the r12-4475 changes [PR120250]
authorJakub Jelinek <jakub@redhat.com>
Wed, 14 Jan 2026 14:53:44 +0000 (15:53 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 14 Jan 2026 14:53:44 +0000 (15:53 +0100)
The r12-4475 change added extra code to recog_for_combine to attempt to
force some constants into the constant pool.
Unfortunately, as this (UB at runtime) testcase shows, such changes are
harmful for computed_jump_p jumps.  The computed_jump_p returns false
for loads from constant pool MEMs:
    case MEM:
      return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
                && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
and so if we try to optimize a computed jump that way, it becomes
a non-computed jump which doesn't match any other jump category
(simplejump_p, tablejump_p, condjump_p, returnjump_p, eh_returnjump_p,
asm goto) and doesn't have any label recorded in JUMP_LABEL (because,
it doesn't really jump to any LABEL), so some passes like dwarf2cfi
can get confused about it and ICE.

The following patch just prevents that, by only doing the r12-4475
changes if it is not a jump.

2026-01-14  Jakub Jelinek  <jakub@redhat.com>

PR target/120250
* combine.cc (recog_for_combine): Don't try to put SET_SRC
into a constant pool if SET_DEST is pc_rtx.

* gcc.c-torture/compile/pr120250.c: New test.

gcc/combine.cc
gcc/testsuite/gcc.c-torture/compile/pr120250.c [new file with mode: 0644]

index 816324f47357289c6b82ca25ff626db878edda53..09e24347b342ba1fa8fc25703759684efde7aa60 100644 (file)
@@ -11759,7 +11759,8 @@ recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes,
       rtx src = SET_SRC (pat);
       if (CONSTANT_P (src)
          && !CONST_INT_P (src)
-         && crtl->uses_const_pool)
+         && crtl->uses_const_pool
+         && SET_DEST (pat) != pc_rtx)
        {
          machine_mode mode = GET_MODE (src);
          if (mode == VOIDmode)
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr120250.c b/gcc/testsuite/gcc.c-torture/compile/pr120250.c
new file mode 100644 (file)
index 0000000..2e8f6f9
--- /dev/null
@@ -0,0 +1,38 @@
+/* PR target/120250 */
+
+int a = 5, b = 7, c, *d;
+void *l;
+void foo (const char *s);
+void bar (void);
+void qux (const char *, int);
+
+int
+baz (int *d, int a, int b)
+{
+  int c = 0;
+  for (int i = 0; i < a; ++i)
+    for (int j = 0; j < b; ++j)
+      c += d[i * b + j];
+  return c;
+}
+
+int
+main ()
+{
+  d = __builtin_malloc (a * b * sizeof (int));
+  if (!d)
+    {
+      foo ("foo");
+      __builtin_exit (1);
+    }
+  for (int i = 0; i < a; ++i)
+    for (int j = 0; j < b; ++j)
+      d[i * b + j] = i * b + j;
+  l = (void *) &bar;
+  goto *l;
+bar:
+  c = baz (d, a, b);
+  qux ("bar", c);
+  __builtin_free (d);
+  return 0;
+}