[PATCH] Avoid mark_symbols_for_rename in setup_one_parameter if possible (PR tree-optimization/33434)

Jakub Jelinek jakub@redhat.com
Fri Nov 30 00:03:00 GMT 2007


Hi!

The use of mark_symbols_for_renaming in setup_one_parameter is dangerous.
The problem is that it can mark for renaming even arguments which are except
in that one (or few) added stmt already in SSA form and can contain code
like the pr33434-1.c testcase below:
  # b_1 = PHI <1(2), b_5(3)>
  b_5 = b_1 + -1;
  if (b_1 != 0)
    goto <bb 3>;
  else
    goto <bb 5>;
i.e. use an earlier SSA version after assigning a new one.  When the b
symbol is renamed, this is miscompiled, because b_1 in the cond will be
changed into the version set in the previous stmt.  This doesn't hit too
often because of:
  if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
      && (TREE_CODE (rhs) == SSA_NAME
          || is_gimple_min_invariant (rhs))
      && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
    {
      insert_decl_map (id, def, rhs);
      return;
    }
The testcase below never looks at the original parameter value,
unconditionally sets it to something else, so def is NULL and thus we do the
harmful renaming.
The patch below cures this for this situation (when p is gimple reg,
inlining is on SSA form and def is NULL), still there are cases where
things can be miscompiled (e.g. when def is in abnormal phi, rhs is not
an SSA name nor min invariant, or most probably in the other 2 callers of
mark_symbols_for_renaming in tree-inline.c.

I've bootstrapped/regtested this on x86_64-linux, ok for trunk?

2007-11-29  Jan Hubicka  <jh@suse.cz>
	    Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/33434
	* tree-inline.c (setup_one_parameter): If the value passed to
	a parameter is never used, don't set it up.

	* gcc.dg/pr33434-1.c: New test.
	* gcc.dg/pr33434-2.c: New test.
	* gcc.dg/pr33434-3.c: New test.
	* gcc.dg/pr33434-4.c: New test.

--- gcc/tree-inline.c.jj	2007-11-26 11:02:25.000000000 +0100
+++ gcc/tree-inline.c	2007-11-29 19:15:51.000000000 +0100
@@ -1508,6 +1508,14 @@ setup_one_parameter (copy_body_data *id,
       return;
     }
 
+  /* If the value of argument is never used, don't care about initializing
+     it.  */
+  if (gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
+    {
+      gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
+      return;
+    }
+
   /* Initialize this VAR_DECL from the equivalent argument.  Convert
      the argument to the proper type in case it was promoted.  */
   if (value)
--- gcc/testsuite/gcc.dg/pr33434-1.c.jj	2007-11-29 19:18:21.000000000 +0100
+++ gcc/testsuite/gcc.dg/pr33434-1.c	2007-11-29 18:37:13.000000000 +0100
@@ -0,0 +1,25 @@
+/* PR tree-optimization/33434 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+int k;
+
+void f1 (int a, int b)
+{
+  a = 1;
+  b = 1;
+  if (a)
+    while (b --)
+      k = 1;
+  else
+    if (b != 1)
+      __builtin_abort ();
+}
+
+int main (void)
+{
+  f1 (1, 1);
+  if (k != 1)
+    __builtin_abort ();
+  return 0;
+}
--- gcc/testsuite/gcc.dg/pr33434-2.c.jj	2007-11-29 19:18:25.000000000 +0100
+++ gcc/testsuite/gcc.dg/pr33434-2.c	2007-11-29 18:40:16.000000000 +0100
@@ -0,0 +1,26 @@
+/* PR tree-optimization/33434 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+int k;
+
+void f1 (int a)
+{
+  int b;
+  a = 1;
+  b = 1;
+  if (a)
+    while (b --)
+      k = 1;
+  else
+    if (b != 1)
+      __builtin_abort ();
+}
+
+int main (void)
+{
+  f1 (1);
+  if (k != 1)
+    __builtin_abort ();
+  return 0;
+}
--- gcc/testsuite/gcc.dg/pr33434-3.c.jj	2007-11-29 19:18:25.000000000 +0100
+++ gcc/testsuite/gcc.dg/pr33434-3.c	2007-11-29 19:11:56.000000000 +0100
@@ -0,0 +1,31 @@
+/* PR tree-optimization/33434 */
+/* { dg-do run } */
+/* { dg-options "-O3" } */
+
+int k;
+
+void __attribute__((noinline)) f2 (int b)
+{
+  k = b - 1;
+}
+
+void f1 (int a, int b)
+{
+  f2 (b);
+  a = 1;
+  b = 1;
+  if (a)
+    while (b --)
+      k = 1;
+  else
+    if (b != 1)
+      __builtin_abort ();
+}
+
+int main (void)
+{
+  f1 (1, 1);
+  if (k != 1)
+    __builtin_abort ();
+  return 0;
+}
--- gcc/testsuite/gcc.dg/pr33434-4.c.jj	2007-11-29 19:18:25.000000000 +0100
+++ gcc/testsuite/gcc.dg/pr33434-4.c	2007-11-29 19:15:39.000000000 +0100
@@ -0,0 +1,18 @@
+/* PR tree-optimization/33434 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+void *baz (void);
+
+static void *
+bar (void *x)
+{
+  x = baz ();
+  return x;
+}
+
+void *
+foo (void *x)
+{
+  return bar (x);
+}

	Jakub



More information about the Gcc-patches mailing list