From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 125892 invoked by alias); 13 Oct 2016 15:26:15 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 124037 invoked by uid 89); 13 Oct 2016 15:26:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 13 Oct 2016 15:26:03 +0000 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id C5CAFACBD; Thu, 13 Oct 2016 15:26:01 +0000 (UTC) Subject: [PATCH] Fold __builtin_memchr (simplified version 4) To: Richard Biener References: <2f0f974b776ee917d094b78cd9b77b992988499c.1471416736.git.mliska@suse.cz> <7c8c5c76-ba33-8fe5-5368-6741d39c8f83@suse.cz> <3ddd673a-cc8c-e7f0-ca3a-496fd881b8d8@suse.cz> Cc: GCC Patches From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Message-ID: <88ba4c5d-4571-5a76-259d-7f6aa64586f5@suse.cz> Date: Thu, 13 Oct 2016 15:26:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2 MIME-Version: 1.0 In-Reply-To: <3ddd673a-cc8c-e7f0-ca3a-496fd881b8d8@suse.cz> Content-Type: multipart/mixed; boundary="------------95DA5C88E80FA5AAE4C80641" X-IsSubscribed: yes X-SW-Source: 2016-10/txt/msg01050.txt.bz2 This is a multi-part message in MIME format. --------------95DA5C88E80FA5AAE4C80641 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-length: 279 Simplified version that supports only valid null-terminated string constants. Apart from that, I added checking for constant folding of expressions that have side effects. Patch can bootstrap on ppc64le-redhat-linux and survives regression tests. Ready to be installed? Martin --------------95DA5C88E80FA5AAE4C80641 Content-Type: text/x-patch; name="0003-Fold-__builtin_memchr-function-simplified.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0003-Fold-__builtin_memchr-function-simplified.patch" Content-length: 8515 >From 5028bf5cf23cda31e72a342b821474ed0c3c07b9 Mon Sep 17 00:00:00 2001 From: marxin Date: Thu, 13 Oct 2016 10:30:56 +0200 Subject: [PATCH 3/4] Fold __builtin_memchr function gcc/ChangeLog: 2016-10-13 Martin Liska * builtins.h(target_char_cst_p): Declare the function. * builtins.c (fold_builtin_memchr): Remove. (target_char_cst_p): Move the function from gimple-fold.c. (fold_builtin_3): Do not call the function. * gimple-fold.c (gimple_fold_builtin_memchr): New function. (gimple_fold_builtin): Call the function. * fold-const-call.c (fold_const_call_1): Handle CFN_BUILT_IN_MEMCHR. --- gcc/builtins.c | 59 ++++++++++----------------------------- gcc/builtins.h | 1 + gcc/fold-const-call.c | 31 +++++++++++++++++++++ gcc/gimple-fold.c | 77 +++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 109 insertions(+), 59 deletions(-) diff --git a/gcc/builtins.c b/gcc/builtins.c index ed5a635..03d8563 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -148,7 +148,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); @@ -7242,47 +7241,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. */ @@ -8338,9 +8296,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);; @@ -9906,3 +9861,17 @@ is_inexpensive_builtin (tree decl) return false; } + +/* Return true if T is a constant and the value cast to a target char + can be represented by a host char. + Store the casted char constant in *P if so. */ + +bool +target_char_cst_p (tree t, char *p) +{ + if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR) + return false; + + *p = (char)tree_to_uhwi (t); + return true; +} diff --git a/gcc/builtins.h b/gcc/builtins.h index 8d0acd0..5e83646 100644 --- a/gcc/builtins.h +++ b/gcc/builtins.h @@ -97,6 +97,7 @@ extern unsigned HOST_WIDE_INT target_percent; extern char target_percent_s[3]; extern char target_percent_c[3]; extern char target_percent_s_newline[4]; +extern bool target_char_cst_p (tree t, char *p); extern internal_fn associated_internal_fn (tree); extern internal_fn replacement_internal_fn (gcall *); diff --git a/gcc/fold-const-call.c b/gcc/fold-const-call.c index f67b245..05a15f9 100644 --- a/gcc/fold-const-call.c +++ b/gcc/fold-const-call.c @@ -28,6 +28,7 @@ along with GCC; see the file COPYING3. If not see #include "fold-const-call.h" #include "case-cfn-macros.h" #include "tm.h" /* For C[LT]Z_DEFINED_AT_ZERO. */ +#include "builtins.h" /* Functions that test for certain constant types, abstracting away the decision about whether to check for overflow. */ @@ -1463,6 +1464,36 @@ fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2) return NULL_TREE; } + switch (fn) + { + case CFN_BUILT_IN_MEMCHR: + { + char c; + if (integer_zerop (arg2) + && !TREE_SIDE_EFFECTS (arg0) + && !TREE_SIDE_EFFECTS (arg1)) + return build_int_cst (type, 0); + + if (!tree_fits_uhwi_p (arg2) || !target_char_cst_p (arg1, &c)) + return NULL_TREE; + + unsigned HOST_WIDE_INT length = tree_to_uhwi (arg2); + unsigned HOST_WIDE_INT string_length; + const char *p1 = c_getstr (arg0, &string_length); + if (p1) + { + const char *r + = (const char *)memchr (p1, c, MIN (length, string_length)); + if (r == NULL && length <= string_length) + return build_int_cst (type, 0); + } + + break; + } + default: + break; + } + return NULL_TREE; } diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index f349472..5d46405 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -57,20 +57,6 @@ along with GCC; see the file COPYING3. If not see #include "tree-cfg.h" #include "fold-const-call.h" -/* Return true if T is a constant and the value cast to a target char - can be represented by a host char. - Store the casted char constant in *P if so. */ - -static bool -target_char_cst_p (tree t, char *p) -{ - if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR) - return false; - - *p = (char)tree_to_uhwi (t); - return true; -} - /* Return true when DECL can be referenced from current unit. FROM_DECL (if non-null) specify constructor of variable DECL was taken from. We can get declarations that are not possible to reference for various @@ -1967,6 +1953,67 @@ gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi) return false; } +/* Fold a call to the memchr pointed by GSI iterator. */ + +static bool +gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi) +{ + gimple *stmt = gsi_stmt (*gsi); + tree lhs = gimple_call_lhs (stmt); + tree arg1 = gimple_call_arg (stmt, 0); + tree arg2 = gimple_call_arg (stmt, 1); + tree len = gimple_call_arg (stmt, 2); + + /* If the LEN parameter is zero, return zero. */ + if (integer_zerop (len)) + { + replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0)); + return true; + } + + char c; + if (TREE_CODE (arg2) != INTEGER_CST + || !tree_fits_uhwi_p (len) + || !target_char_cst_p (arg2, &c)) + return false; + + unsigned HOST_WIDE_INT length = tree_to_uhwi (len); + unsigned HOST_WIDE_INT string_length; + const char *p1 = c_getstr (arg1, &string_length); + + if (p1) + { + const char *r = (const char *)memchr (p1, c, MIN (length, string_length)); + if (r == NULL) + { + if (length <= string_length) + { + replace_call_with_value (gsi, build_int_cst (ptr_type_node, 0)); + return true; + } + } + else + { + unsigned HOST_WIDE_INT offset = r - p1; + gimple_seq stmts = NULL; + if (lhs != NULL_TREE) + { + tree offset_cst = build_int_cst (TREE_TYPE (len), offset); + gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR, + arg1, offset_cst); + gimple_seq_add_stmt_without_update (&stmts, stmt); + } + else + gimple_seq_add_stmt_without_update (&stmts, + gimple_build_nop ()); + + gsi_replace_with_seq_vops (gsi, stmts); + 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 @@ -3194,6 +3241,8 @@ gimple_fold_builtin (gimple_stmt_iterator *gsi) case BUILT_IN_STRNCMP: 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 --------------95DA5C88E80FA5AAE4C80641--