public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Martin Liška" <mliska@suse.cz>
To: Richard Biener <richard.guenther@gmail.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH 2/3] Fold __builtin_memchr (version 2)
Date: Fri, 07 Oct 2016 08:41:00 -0000	[thread overview]
Message-ID: <7c8c5c76-ba33-8fe5-5368-6741d39c8f83@suse.cz> (raw)
In-Reply-To: <CAFiYyc3QxgJgo4z0hFcz1U2i6FShOm33GH5j8EOrpL+t0nLGPg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 173 bytes --]

Resending the patch, where I implemented folding in gimple-fold.c

Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.

Ready to be installed?
Martin

[-- Attachment #2: 0002-Fold-__builtin_memchr-v3.patch --]
[-- Type: text/x-patch, Size: 6113 bytes --]

From d4c1257b092f245a52386e305391d1e46e2ef088 Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Thu, 6 Oct 2016 17:52:45 +0200
Subject: [PATCH 2/3] Fold __builtin_memchr

gcc/ChangeLog:

2016-10-06  Martin Liska  <mliska@suse.cz>

	* builtins.c (fold_builtin_memchr): Remove.
	(fold_builtin_3): Do not call the function.
	* builtins.h: Declare target_char_cast.
	* gimple-fold.c (gimple_fold_builtin_memchr): New function.
	(gimple_fold_builtin): Call the function.
---
 gcc/builtins.c    | 48 +------------------------------------------
 gcc/builtins.h    |  1 +
 gcc/gimple-fold.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 47 deletions(-)

diff --git a/gcc/builtins.c b/gcc/builtins.c
index b03d53c..97723dc 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -92,7 +92,6 @@ builtin_info_type builtin_info[(int)END_BUILTINS];
 bool force_folding_builtin_constant_p;
 
 static rtx c_readstr (const char *, machine_mode);
-static int target_char_cast (tree, char *);
 static rtx get_memory_rtx (tree, tree);
 static int apply_args_size (void);
 static int apply_result_size (void);
@@ -148,7 +147,6 @@ static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
 static bool validate_arg (const_tree, enum tree_code code);
 static rtx expand_builtin_fabs (tree, rtx, rtx);
 static rtx expand_builtin_signbit (tree, rtx);
-static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
 static tree fold_builtin_isascii (location_t, tree);
 static tree fold_builtin_toascii (location_t, tree);
@@ -653,7 +651,7 @@ c_readstr (const char *str, machine_mode mode)
    host char type, return zero and put that value into variable pointed to by
    P.  */
 
-static int
+int
 target_char_cast (tree cst, char *p)
 {
   unsigned HOST_WIDE_INT val, hostval;
@@ -7246,47 +7244,6 @@ fold_builtin_sincos (location_t loc,
 			 fold_build1_loc (loc, REALPART_EXPR, type, call)));
 }
 
-/* Fold function call to builtin memchr.  ARG1, ARG2 and LEN are the
-   arguments to the call, and TYPE is its return type.
-   Return NULL_TREE if no simplification can be made.  */
-
-static tree
-fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
-{
-  if (!validate_arg (arg1, POINTER_TYPE)
-      || !validate_arg (arg2, INTEGER_TYPE)
-      || !validate_arg (len, INTEGER_TYPE))
-    return NULL_TREE;
-  else
-    {
-      const char *p1;
-
-      if (TREE_CODE (arg2) != INTEGER_CST
-	  || !tree_fits_uhwi_p (len))
-	return NULL_TREE;
-
-      p1 = c_getstr (arg1);
-      if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
-	{
-	  char c;
-	  const char *r;
-	  tree tem;
-
-	  if (target_char_cast (arg2, &c))
-	    return NULL_TREE;
-
-	  r = (const char *) memchr (p1, c, tree_to_uhwi (len));
-
-	  if (r == NULL)
-	    return build_int_cst (TREE_TYPE (arg1), 0);
-
-	  tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
-	  return fold_convert_loc (loc, type, tem);
-	}
-      return NULL_TREE;
-    }
-}
-
 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
    Return NULL_TREE if no simplification can be made.  */
 
@@ -8342,9 +8299,6 @@ fold_builtin_3 (location_t loc, tree fndecl,
 	return do_mpfr_remquo (arg0, arg1, arg2);
     break;
 
-    case BUILT_IN_MEMCHR:
-      return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
-
     case BUILT_IN_BCMP:
     case BUILT_IN_MEMCMP:
       return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
diff --git a/gcc/builtins.h b/gcc/builtins.h
index 8d0acd0..6fd207a 100644
--- a/gcc/builtins.h
+++ b/gcc/builtins.h
@@ -100,5 +100,6 @@ extern char target_percent_s_newline[4];
 
 extern internal_fn associated_internal_fn (tree);
 extern internal_fn replacement_internal_fn (gcall *);
+extern int target_char_cast (tree, char *);
 
 #endif
diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c
index 67a598c..ad20593 100644
--- a/gcc/gimple-fold.c
+++ b/gcc/gimple-fold.c
@@ -1920,6 +1920,65 @@ gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
   return false;
 }
 
+/* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator.
+   FCODE is the name of the builtin.  */
+
+static bool
+gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi)
+{
+  gimple *stmt = gsi_stmt (*gsi);
+  tree arg1 = gimple_call_arg (stmt, 0);
+  tree type = TREE_TYPE (arg1);
+  tree arg2 = gimple_call_arg (stmt, 1);
+  tree len = gimple_call_arg (stmt, 2);
+  location_t loc = gimple_location (stmt);
+
+  /* If the LEN parameter is zero, return zero.  */
+  if (integer_zerop (len))
+    {
+      replace_call_with_value (gsi, build_int_cst (type, 0));
+      return true;
+    }
+
+  if (TREE_CODE (arg2) != INTEGER_CST
+      || !tree_fits_uhwi_p (len))
+    return false;
+
+  const char *p1 = c_getstr (arg1);
+  if (p1)
+    {
+      char c;
+      const char *r;
+
+      if (target_char_cast (arg2, &c))
+	return false;
+
+      r = (const char *) memchr (p1, c, tree_to_uhwi (len));
+
+      if (r == NULL)
+	{
+	  replace_call_with_value (gsi, build_int_cst (type, 0));
+	  return true;
+	}
+      else
+	{
+	  HOST_WIDE_INT offset = r - p1;
+	  if (compare_tree_int (len, offset) <= 0)
+	    {
+	      replace_call_with_value (gsi, build_int_cst (type, 0));
+	      return true;
+	    }
+	  else
+	    {
+	      tree temp = fold_build_pointer_plus_hwi_loc (loc, arg1, offset);
+	      replace_call_with_value (gsi, temp);
+	      return true;
+	    }
+	}
+    }
+
+  return false;
+}
 
 /* Fold a call to the fputs builtin.  ARG0 and ARG1 are the arguments
    to the call.  IGNORE is true if the value returned
@@ -3150,6 +3209,8 @@ gimple_fold_builtin (gimple_stmt_iterator *gsi)
       return gimple_fold_builtin_string_compare (gsi);
     case BUILT_IN_STRNCASECMP:
       return gimple_fold_builtin_string_compare (gsi);
+    case BUILT_IN_MEMCHR:
+      return gimple_fold_builtin_memchr (gsi);
     case BUILT_IN_FPUTS:
       return gimple_fold_builtin_fputs (gsi, gimple_call_arg (stmt, 0),
 					gimple_call_arg (stmt, 1), false);
-- 
2.9.2


  reply	other threads:[~2016-10-07  8:41 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-17  6:55 [PATCH 0/3] Better folding of 2 string builtin-ins marxin
2016-08-17  6:55 ` [PATCH 2/3] Smarter folding of __builtin_memchr marxin
2016-08-17  8:41   ` Richard Biener
2016-10-07  8:41     ` Martin Liška [this message]
2016-10-07 11:01       ` [PATCH 2/3] Fold __builtin_memchr (version 2) Richard Biener
2016-10-11  9:38         ` [PATCH] Fold __builtin_memchr (version 3) Martin Liška
2016-10-12  8:34           ` Richard Biener
2016-10-12  8:36           ` Richard Biener
2016-10-12 13:19             ` Martin Liška
2016-10-13 15:26               ` [PATCH] Fold __builtin_memchr (simplified version 4) Martin Liška
2016-10-14 11:57                 ` Richard Biener
2016-08-17  6:55 ` [PATCH 3/3] Test folding of strn{case}cmp and memchr marxin
2016-08-17  7:52   ` Martin Liška
2016-10-07  8:42     ` [PATCH 3/3] Test folding of str{n}{case}cmp and memchr (version 2) Martin Liška
2016-10-11  9:39       ` [PATCH] Test folding of str{n}{case}cmp and memchr (version 3) Martin Liška
2016-10-12  8:35         ` Richard Biener
2016-10-12 13:20           ` Martin Liška
2016-10-13 15:27             ` [PATCH] Test folding of str{n}{case}cmp and memchr (simplified version 4) Martin Liška
2016-10-14 11:58               ` Richard Biener
2016-08-17  6:55 ` [PATCH 1/3] Fold BUILT_IN_STRNCASECMP marxin
2016-08-17  7:10   ` Jakub Jelinek
2016-08-17  7:52     ` Martin Liška
2016-10-07  8:39   ` [PATCH 1/3] Fold __builtin_str{n}{case}cmp functions (version 2) Martin Liška
2016-10-07 10:50     ` Richard Biener
2016-10-11  9:26       ` Martin Liška
2016-10-11 10:27         ` Richard Biener
2016-10-11  9:28       ` [PATCH] Add a helper function: create_tmp Martin Liška
2016-10-11 10:30         ` Richard Biener
2016-10-11 10:31           ` Richard Biener
2016-10-12 10:50             ` Martin Liška
2016-10-11  9:28       ` [PATCH] Check \0-termination of string in c_getstr Martin Liška
2016-10-11 10:28         ` Richard Biener
2016-10-12 13:14           ` Martin Liška
2016-10-13 15:24             ` [PATCH] Check \0-termination of string in c_getstr (simplified version) Martin Liška
2016-10-14  9:38               ` Richard Biener
2016-10-14 11:10                 ` Martin Liška
2016-10-11  9:34       ` [PATCH] Fold __builtin_str{n}{case}cmp functions (version 3) Martin Liška
2016-10-12  8:30         ` Richard Biener
2016-10-12 13:17           ` Martin Liška
2016-10-13 15:25             ` [PATCH] Fold __builtin_str{n}{case}cmp functions (simplified version 4) Martin Liška
2016-10-14 11:55               ` Richard Biener
2016-10-12 13:48 ` [RFC] Possible folding opportunities for string built-ins Martin Liška
2016-10-12 15:55   ` Joseph Myers
2016-10-12 19:45     ` Jim Wilson
2016-10-13  8:38   ` Richard Biener
2016-10-07 11:21 [PATCH 2/3] Fold __builtin_memchr (version 2) Wilco Dijkstra
2016-10-10 10:57 ` Martin Liška
2016-10-10 11:28   ` Wilco Dijkstra
2016-10-10 11:37     ` Martin Liška

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7c8c5c76-ba33-8fe5-5368-6741d39c8f83@suse.cz \
    --to=mliska@suse.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).