[PATCH] Fix expand_builtin_memory_chk (PR middle-end/33423)

Jakub Jelinek jakub@redhat.com
Sun Sep 16 00:13:00 GMT 2007


Hi!

If __builtin___memset_chk is not folded into __builtin_memset during fab
pass, it might be folded only during expand_builtin_memory_chk right before
we want to pass that through to expand_expr.  As fold_builtin invoked
by build_call_expr can return non-GIMPLE, in particular COMPOUND_EXPRs,
but expand_expr wants to enforce GIMPLE input, we need to handle
COMPOUND_EXPRs ourselves in expand_builtin_memory_chk.
This is the same solution as is done e.g. in expand_builtin_memcpy,
expand_builtin_mempcpy_args, expand_builtin_memmove_args and
expand_builtin_strncpy.

Tested on x86_64-linux, ok for trunk/4.2?

2007-09-15  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/33423
	* builtins.c (expand_builtin_memory_chk): Handle COMPOUND_EXPRs
	returned by build_call_expr.

	* gcc.c-torture/compile/20070915-1.c: New test.

--- gcc/builtins.c.jj	2007-09-15 21:59:33.000000000 +0200
+++ gcc/builtins.c	2007-09-15 22:10:15.000000000 +0200
@@ -11558,6 +11558,13 @@ expand_builtin_memory_chk (tree exp, rtx
 	return NULL_RTX;
 
       fn = build_call_expr (fn, 3, dest, src, len);
+      STRIP_TYPE_NOPS (fn);
+      while (TREE_CODE (fn) == COMPOUND_EXPR)
+	{
+	  expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
+		       EXPAND_NORMAL);
+	  fn = TREE_OPERAND (fn, 1);
+	}
       if (TREE_CODE (fn) == CALL_EXPR)
 	CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
       return expand_expr (fn, target, mode, EXPAND_NORMAL);
@@ -11606,6 +11613,13 @@ expand_builtin_memory_chk (tree exp, rtx
 	      if (!fn)
 		return NULL_RTX;
 	      fn = build_call_expr (fn, 4, dest, src, len, size);
+	      STRIP_TYPE_NOPS (fn);
+	      while (TREE_CODE (fn) == COMPOUND_EXPR)
+		{
+		  expand_expr (TREE_OPERAND (fn, 0), const0_rtx, VOIDmode,
+			       EXPAND_NORMAL);
+		  fn = TREE_OPERAND (fn, 1);
+		}
 	      if (TREE_CODE (fn) == CALL_EXPR)
 		CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
 	      return expand_expr (fn, target, mode, EXPAND_NORMAL);
--- gcc/testsuite/gcc.c-torture/compile/20070915-1.c.jj	2007-09-15 22:14:01.000000000 +0200
+++ gcc/testsuite/gcc.c-torture/compile/20070915-1.c	2007-09-15 22:13:27.000000000 +0200
@@ -0,0 +1,20 @@
+/* PR middle-end/33423 */
+
+static struct
+{
+  char buf[15];
+} u2;
+
+void
+test6 (void)
+{
+  int len;
+  char *p;
+
+  for (len = 0; len < 2; len++)
+    {
+      p = __builtin___memset_chk (u2.buf, '\0', len, 15);
+      if (p != u2.buf)
+	return;
+    }
+}

	Jakub



More information about the Gcc-patches mailing list