CSE bug (gcc.c-torture/execute/980605-1.c)

David S. Miller davem@dm.cobaltmicro.com
Sun Jul 5 07:16:00 GMT 1998


This one is nasty and has been in GCC for a long time.  When it
doesn't generate incorrect code, the bug prevents libcalls and
functions marked with attribute const from being optimized away in
most cases. ;-(

For reference since the test case is quite small:

========================================
static int f(int) __attribute__((const));
int main()
{
   int f1, f2, x;
   x = 1; f1 = f(x);
   x = 2; f2 = f(x);
   if (f1 != 1 || f2 != 2)
     abort ();
   exit (0);
}
static int f(int x) { return x; }
========================================

This generates the following RTL on 32-bit Sparc targets.

========================================
(insn 9 6 11 (set (reg/v:SI 107)
        (const_int 1)) -1 (nil)
    (nil))

(insn 11 9 13 (set (reg:SI 8 %o0)
        (reg/v:SI 107)) -1 (nil)
    (insn_list:REG_LIBCALL 15 (nil)))

(call_insn/u 13 11 15 (parallel[ 
            (set (reg:SI 8 %o0)
                (call (mem:SI (symbol_ref:SI ("f")))
                    (const_int 0)))
            (clobber (reg:SI 15 %o7))
        ] ) -1 (nil)
    (nil)
    (expr_list (use (reg:SI 8 %o0))
        (nil)))

(insn 15 13 17 (set (reg:SI 108)
        (reg:SI 8 %o0)) -1 (nil)
    (insn_list:REG_RETVAL 11 (expr_list:REG_EQUAL (expr_list (symbol_ref:SI ("f"))
                (expr_list (reg/v:SI 107)
                    (nil)))
            (nil))))
========================================

The first run of CSE removes the set of REG 107 in insn 9 and changes
insn 11 to be (set (reg:SI %o0) (const_int 1))

This is fine, except for the fact that the REG_EQUAL information at
the end of the libcall block at insn 15 is now completely bogus.

The fix is to rewrite such REG_EQUAL lists with the replacements made
when CSE performs such changes within a libcall block.

Here is my attempt at a fix, comments?

Fri Jul  3 23:04:59 1998  David S. Miller  <davem@pierdol.cobaltmicro.com>

	* cse.c (rewrite_libcall_equivs): New function.
	(cse_insn): Call it when substituting the source of a SET within a
	libcall block.

--- cse.c.~1~	Thu Jul  2 16:32:44 1998
+++ cse.c	Fri Jul  3 22:46:43 1998
@@ -6098,6 +6098,33 @@
   merge_equiv_classes (op0_elt, op1_elt);
   last_jump_equiv_class = op0_elt;
 }
+
+/* Rewrite all REG_EQUIVs mentioning OLD_SRC to mention NEW_SRC at
+   the end of libcall block starting at INSN.  */
+
+static void
+rewrite_libcall_equivs (insn, old_src, new_src)
+     rtx insn;
+     rtx old_src;
+     rtx new_src;
+{
+  rtx note;
+
+  while (insn != NULL_RTX)
+    {
+      if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
+	break;
+      insn = NEXT_INSN (insn);
+    }
+
+  if (insn == NULL_RTX)
+    abort();
+
+  note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
+  if (note != NULL_RTX)
+    replace_rtx (XEXP (note, 0), old_src, new_src);
+}
+
 
 /* CSE processing for one instruction.
    First simplify sources and addresses of all assignments
@@ -6929,12 +6956,18 @@
 	  /* Look for a substitution that makes a valid insn.  */
           else if (validate_change (insn, &SET_SRC (sets[i].rtl), trial, 0))
 	    {
+	      rtx new_src = canon_reg (SET_SRC (sets[i].rtl), insn);
+
+	      /* If we are in a libcall block, we must make sure we did
+		 not change the meaning of any REG_EQUIVs living at the
+		 end of it.  */
+	      if (in_libcall_block)
+		rewrite_libcall_equivs (insn, sets[i].src, new_src);
+
 	      /* The result of apply_change_group can be ignored; see
 		 canon_reg.  */
 
-	      validate_change (insn, &SET_SRC (sets[i].rtl),
-			       canon_reg (SET_SRC (sets[i].rtl), insn),
-			       1);
+	      validate_change (insn, &SET_SRC (sets[i].rtl), new_src, 1);
 	      apply_change_group ();
 	      break;
 	    }



More information about the Gcc mailing list