re PR middle-end/38343 (ice for legal code with -O2)
authorJakub Jelinek <jakub@gcc.gnu.org>
Tue, 2 Dec 2008 10:13:05 +0000 (11:13 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 2 Dec 2008 10:13:05 +0000 (11:13 +0100)
PR middle-end/38343
* builtins.c (expand_builtin_mempcpy_args): Handle COMPOUND_EXPRs
potentially returned from folding memcpy.
(expand_builtin_stpcpy_args): Similarly for folding strcpy.
(fold_builtin_2): Handle BUILT_IN_STPCPY if result is ignored.

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

From-SVN: r142348

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.c-torture/compile/pr38343.c [new file with mode: 0644]

index 75fa2e7..928a0aa 100644 (file)
@@ -1,4 +1,12 @@
-2008-12-02  Danny Smith   <dannysmith@users.sourceforge.net>
+2008-12-02  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/38343
+       * builtins.c (expand_builtin_mempcpy_args): Handle COMPOUND_EXPRs
+       potentially returned from folding memcpy.
+       (expand_builtin_stpcpy_args): Similarly for folding strcpy.
+       (fold_builtin_2): Handle BUILT_IN_STPCPY if result is ignored.
+
+2008-12-02  Danny Smith  <dannysmith@users.sourceforge.net>
 
         PR target/38054
        * config/i386/winnt.c (i386_pe_encode_section_info): Condition stdcall
index a6ee5a9..3406667 100644 (file)
@@ -3406,7 +3406,7 @@ expand_builtin_memcpy (tree exp, rtx target, enum machine_mode mode)
    stpcpy.  */
 
 static rtx
-expand_builtin_mempcpy(tree exp, rtx target, enum machine_mode mode)
+expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
 {
   if (!validate_arglist (exp,
                         POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
@@ -3433,15 +3433,18 @@ expand_builtin_mempcpy_args (tree dest, tree src, tree len, tree type,
                             rtx target, enum machine_mode mode, int endp)
 {
     /* If return value is ignored, transform mempcpy into memcpy.  */
-  if (target == const0_rtx)
+  if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
     {
       tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
+      tree result = build_call_expr (fn, 3, dest, src, len);
 
-      if (!fn)
-       return NULL_RTX;
-
-      return expand_expr (build_call_expr (fn, 3, dest, src, len),
-                         target, mode, EXPAND_NORMAL);
+      while (TREE_CODE (result) == COMPOUND_EXPR)
+       {
+         expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
+                      EXPAND_NORMAL);
+         result = TREE_OPERAND (result, 1);
+       }
+      return expand_expr (result, target, mode, EXPAND_NORMAL);
     }
   else
     {
@@ -3707,14 +3710,19 @@ expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
   src = CALL_EXPR_ARG (exp, 1);
 
   /* If return value is ignored, transform stpcpy into strcpy.  */
-  if (target == const0_rtx)
+  if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
     {
       tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
-      if (!fn)
-       return NULL_RTX;
+      tree result = build_call_expr (fn, 2, dst, src);
 
-      return expand_expr (build_call_expr (fn, 2, dst, src),
-                         target, mode, EXPAND_NORMAL);
+      STRIP_NOPS (result);
+      while (TREE_CODE (result) == COMPOUND_EXPR)
+       {
+         expand_expr (TREE_OPERAND (result, 0), const0_rtx, VOIDmode,
+                      EXPAND_NORMAL);
+         result = TREE_OPERAND (result, 1);
+       }
+      return expand_expr (result, target, mode, EXPAND_NORMAL);
     }
   else
     {
@@ -10463,6 +10471,17 @@ fold_builtin_2 (tree fndecl, tree arg0, tree arg1, bool ignore)
     case BUILT_IN_STRCPY:
       return fold_builtin_strcpy (fndecl, arg0, arg1, NULL_TREE);
 
+    case BUILT_IN_STPCPY:
+      if (ignore)
+       {
+         tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
+         if (!fn)
+           break;
+
+         return build_call_expr (fn, 2, arg0, arg1);
+       }
+      break;
+
     case BUILT_IN_STRCMP:
       return fold_builtin_strcmp (arg0, arg1);
 
index 440872d..ad317b7 100644 (file)
@@ -1,3 +1,8 @@
+2008-12-02  Jakub Jelinek  <jakub@redhat.com>
+
+       PR middle-end/38343
+       * gcc.c-torture/compile/pr38343.c: New test.
+
 2008-12-01  Janis Johnson  <janis187@us.ibm.com>
 
        * gcc.target/powerpc/pr18096-1.c: Adjust location of expected error.
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr38343.c b/gcc/testsuite/gcc.c-torture/compile/pr38343.c
new file mode 100644 (file)
index 0000000..589ba33
--- /dev/null
@@ -0,0 +1,12 @@
+/* PR middle-end/38343 */
+
+static struct A
+{
+  char f[6];
+} a[] = { {"01000"} };
+
+void
+foo (void)
+{
+  __builtin_stpcpy (a[0].f, "S0022");
+}