This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix __builtin_expect


Hi!

The following testcase (as well as linux-2.6.0-test4 kernel) is miscompiled
on IA-32 on all of 3.2/3.3 branch and trunk.
The problem is that although expand_builtin_expect_jump checks
unsafe_for_reeval, it doesn't consider stack adjustements,
so when it expands the jump, sees num_jumps == 0 (in this exact testcase
there are no conditional jumps to either of if_{true,false}_label, as
there are branches to a different label emitted at the end and unconditional
jump to if_false_label) and throws away the whole emitted sequence,
anything emitted by do_pending_stack_adjust is thrown away.
The following patch fixes this.
Ok for 3.3/HEAD?

2003-08-26  Jakub Jelinek  <jakub@redhat.com>

	* builtins.c (expand_builtin_expect_jump): Save pending_stack_adjust
	and restore it if returning NULL.

	* gcc.dg/20030826-2.c: New test.

--- gcc/builtins.c.jj	2003-08-26 10:53:02.000000000 -0400
+++ gcc/builtins.c	2003-08-26 11:55:07.000000000 -0400
@@ -4330,6 +4330,7 @@ expand_builtin_expect_jump (tree exp, rt
       && (integer_zerop (arg1) || integer_onep (arg1)))
     {
       int num_jumps = 0;
+      int save_pending_stack_adjust = pending_stack_adjust;
       rtx insn;
 
       /* If we fail to locate an appropriate conditional jump, we'll
@@ -4421,7 +4422,10 @@ expand_builtin_expect_jump (tree exp, rt
       /* If no jumps were modified, fail and do __builtin_expect the normal
 	 way.  */
       if (num_jumps == 0)
-	ret = NULL_RTX;
+	{
+	  ret = NULL_RTX;
+	  pending_stack_adjust = save_pending_stack_adjust;
+	}
     }
 
   return ret;
--- gcc/testsuite/gcc.dg/20030826-2.c.jj	2003-08-26 12:01:36.000000000 -0400
+++ gcc/testsuite/gcc.dg/20030826-2.c	2003-08-26 11:09:49.000000000 -0400
@@ -0,0 +1,64 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -fomit-frame-pointer" } */
+/* { dg-options "-O2 -fomit-frame-pointer -march=i386" { target i?86-*-* } } */
+
+extern void abort (void);
+extern void exit (int);
+
+struct S
+{
+  int *a;
+  unsigned char *b, c;
+};
+
+int u, v, w;
+
+void
+foo (unsigned short x)
+{
+  u += x;
+}
+
+int
+bar (struct S **x, int *y)
+{
+  w += *y;
+  *y = w + 25;
+  return 0;
+}
+
+int
+baz (struct S **x)
+{
+  struct S *y = *x;
+  unsigned char *a = y->b;
+
+  foo (*a);
+
+  if (__builtin_expect (y->c != 0 || y->a == &v, 0))
+    return 1;
+
+  if (__builtin_expect (*a == 1, 0))
+    {
+      int a, b = bar (x, &a);
+
+      if (a)
+	return b;
+    }
+
+  return 0;
+}
+
+int
+main (void)
+{
+  struct S a, *b = &a;
+  unsigned char c;
+
+  __builtin_memset (b, 0, sizeof (a));
+  a.a = &v;
+  a.b = &c;
+  if (baz (&b) != 1)
+    abort ();
+  exit (0);
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]