diff mbox

Fix PR62239

Message ID alpine.LSU.2.11.1408261605100.20733@zhemvz.fhfr.qr
State New
Headers show

Commit Message

Richard Biener Aug. 26, 2014, 2:05 p.m. UTC
The following fixes PR62239 and moves strcat_chk folding to GIMPLE.

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.

Richard.

2014-08-26  Richard Biener  <rguenther@suse.de>

	PR middle-end/62239
	* builtins.c (fold_builtin_strcat_chk): Move to gimple-fold.c.
	(fold_builtin_3): Do not fold strcat_chk here.
	* gimple-fold.c (gimple_fold_builtin_strcat_chk): Move here
	from builtins.c.
	(gimple_fold_builtin): Fold strcat_chk here.
diff mbox

Patch

Index: gcc/gimple-fold.c
===================================================================
--- gcc/gimple-fold.c	(revision 214500)
+++ gcc/gimple-fold.c	(working copy)
@@ -1587,6 +1587,41 @@  gimple_fold_builtin_strcat (gimple_stmt_
   return true;
 }
 
+/* Fold a call to the __strcat_chk builtin FNDECL.  DEST, SRC, and SIZE
+   are the arguments to the call.  */
+
+static bool
+gimple_fold_builtin_strcat_chk (gimple_stmt_iterator *gsi)
+{
+  gimple stmt = gsi_stmt (*gsi);
+  tree dest = gimple_call_arg (stmt, 0);
+  tree src = gimple_call_arg (stmt, 1);
+  tree size = gimple_call_arg (stmt, 2);
+  tree fn;
+  const char *p;
+
+
+  p = c_getstr (src);
+  /* If the SRC parameter is "", return DEST.  */
+  if (p && *p == '\0')
+    {
+      replace_call_with_value (gsi, dest);
+      return true;
+    }
+
+  if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
+    return false;
+
+  /* If __builtin_strcat_chk is used, assume strcat is available.  */
+  fn = builtin_decl_explicit (BUILT_IN_STRCAT);
+  if (!fn)
+    return false;
+
+  gimple repl = gimple_build_call (fn, 2, dest, src);
+  replace_call_with_call_and_fold (gsi, repl);
+  return true;
+}
+
 /* Fold a call to the fputs builtin.  ARG0 and ARG1 are the arguments
    to the call.  IGNORE is true if the value returned
    by the builtin will be ignored.  UNLOCKED is true is true if this
@@ -2569,6 +2604,8 @@  gimple_fold_builtin (gimple_stmt_iterato
     case BUILT_IN_SPRINTF_CHK:
     case BUILT_IN_VSPRINTF_CHK:
       return gimple_fold_builtin_sprintf_chk (gsi, DECL_FUNCTION_CODE (callee));
+    case BUILT_IN_STRCAT_CHK:
+      return gimple_fold_builtin_strcat_chk (gsi);
     default:;
     }
 
Index: gcc/builtins.c
===================================================================
--- gcc/builtins.c	(revision 214500)
+++ gcc/builtins.c	(working copy)
@@ -198,7 +198,6 @@  static void maybe_emit_chk_warning (tree
 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
 static void maybe_emit_free_warning (tree);
 static tree fold_builtin_object_size (tree, tree);
-static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
@@ -10321,9 +10320,6 @@  fold_builtin_3 (location_t loc, tree fnd
     case BUILT_IN_MEMCMP:
       return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
 
-    case BUILT_IN_STRCAT_CHK:
-      return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
-
     case BUILT_IN_PRINTF_CHK:
     case BUILT_IN_VPRINTF_CHK:
       if (!validate_arg (arg0, INTEGER_TYPE)
@@ -11588,37 +11584,6 @@  fold_builtin_object_size (tree ptr, tree
   return NULL_TREE;
 }
 
-/* Fold a call to the __strcat_chk builtin FNDECL.  DEST, SRC, and SIZE
-   are the arguments to the call.  */
-
-static tree
-fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
-			 tree src, tree size)
-{
-  tree fn;
-  const char *p;
-
-  if (!validate_arg (dest, POINTER_TYPE)
-      || !validate_arg (src, POINTER_TYPE)
-      || !validate_arg (size, INTEGER_TYPE))
-    return NULL_TREE;
-
-  p = c_getstr (src);
-  /* If the SRC parameter is "", return DEST.  */
-  if (p && *p == '\0')
-    return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
-
-  if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
-    return NULL_TREE;
-
-  /* If __builtin_strcat_chk is used, assume strcat is available.  */
-  fn = builtin_decl_explicit (BUILT_IN_STRCAT);
-  if (!fn)
-    return NULL_TREE;
-
-  return build_call_expr_loc (loc, fn, 2, dest, src);
-}
-
 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
    LEN, and SIZE.  */