Re: [PATCH] Fold __builtin_memchr (version 3)
On 10/12/2016 10:35 AM, Richard Biener wrote: > On Tue, Oct 11, 2016 at 11:38 AM, Martin Liška wrote: >> One question that comes to my mind is whether there's a possibility >> to fully test gimple folding of all cases if some of them are already >> eaten by generic folding? > > The only way is to make GENERIC folding not trigger by pushing > constants to temporaries. > > Richard. > Good idea, I've done that in the patch with tests. I made a small revision to patch, where I utilize the new c_getstr function arguments to handle more cases. Patch can bootstrap on ppc64le-redhat-linux and survives regression tests as a whole series. Martin >From bc96a7b30764a098d47fa65bd1682005111febdf Mon Sep 17 00:00:00 2001 From: marxin Date: Thu, 6 Oct 2016 17:52:45 +0200 Subject: [PATCH 4/5] Fold __builtin_memchr function gcc/ChangeLog: 2016-10-06 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 | 41 + gcc/gimple-fold.c | 83 ++- 4 files changed, 125 insertions(+), 59 deletions(-) diff --git a/gcc/builtins.c b/gcc/builtins.c index 6696f28..385e78e0 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); @@ -7244,47 +7243,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. */ @@ -8340,9 +8298,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);; @@ -9908,3 +9863,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 993769c..d84af48 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_Z
Re: [PATCH] Fold __builtin_memchr (version 3)
On Tue, Oct 11, 2016 at 11:38 AM, Martin Liška wrote: > One question that comes to my mind is whether there's a possibility > to fully test gimple folding of all cases if some of them are already > eaten by generic folding? The only way is to make GENERIC folding not trigger by pushing constants to temporaries. Richard.
Re: [PATCH] Fold __builtin_memchr (version 3)
On Tue, Oct 11, 2016 at 11:38 AM, Martin Liška wrote: > On 10/07/2016 01:01 PM, Richard Biener wrote: >> On Fri, Oct 7, 2016 at 10:41 AM, Martin Liška wrote: >>> Resending the patch, where I implemented folding in gimple-fold.c >>> >>> Patch can bootstrap on ppc64le-redhat-linux and survives regression tests. >> >> +/* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. >> + FCODE is the name of the builtin. */ >> >> wrong comment > > Fixed. > >> >> +static bool >> +gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi) >> >> + { >> + replace_call_with_value (gsi, build_int_cst (type, 0)); >> >> use ptr_type_node (void *) instead of type here and below. > > Done. > >> >> + HOST_WIDE_INT offset = r - p1; >> + if (compare_tree_int (len, offset) <= 0) >> + { >> >> == 0 can occur in which case we have to return a pointer to the >> first char. I think len < offset can't happen with memchr? > > Here I reworked the patch as it's not desired to trigger an undefined behavior > in a host compiler for cases like: memchr ("", 'x', 5). Thus I switched to > strchr > and aforementioned hunk would make sense. > >> >> + 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); >> >> That yields valid GIMPLE by chance, I'd prefer if you'd built that to a >> stmt and use the replace-with-vops. > > Done. > > Apart from that I added handling of lhs and the patch supports folding > of CFN_BUILT_IN_MEMCHR. > > One question that comes to my mind is whether there's a possibility > to fully test gimple folding of all cases if some of them are already > eaten by generic folding? > > Tests of the series have been running. Ok. Thanks, Richard. > Martin > >> >> + return true; >> + } >> >> >>> Ready to be installed? >>> Martin >
[PATCH] Fold __builtin_memchr (version 3)
On 10/07/2016 01:01 PM, Richard Biener wrote: > On Fri, Oct 7, 2016 at 10:41 AM, Martin Liška wrote: >> Resending the patch, where I implemented folding in gimple-fold.c >> >> Patch can bootstrap on ppc64le-redhat-linux and survives regression tests. > > +/* Fold a call to the str{n}{case}cmp builtin pointed by GSI iterator. > + FCODE is the name of the builtin. */ > > wrong comment Fixed. > > +static bool > +gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi) > > + { > + replace_call_with_value (gsi, build_int_cst (type, 0)); > > use ptr_type_node (void *) instead of type here and below. Done. > > + HOST_WIDE_INT offset = r - p1; > + if (compare_tree_int (len, offset) <= 0) > + { > > == 0 can occur in which case we have to return a pointer to the > first char. I think len < offset can't happen with memchr? Here I reworked the patch as it's not desired to trigger an undefined behavior in a host compiler for cases like: memchr ("", 'x', 5). Thus I switched to strchr and aforementioned hunk would make sense. > > + 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); > > That yields valid GIMPLE by chance, I'd prefer if you'd built that to a > stmt and use the replace-with-vops. Done. Apart from that I added handling of lhs and the patch supports folding of CFN_BUILT_IN_MEMCHR. One question that comes to my mind is whether there's a possibility to fully test gimple folding of all cases if some of them are already eaten by generic folding? Tests of the series have been running. Martin > > + return true; > + } > > >> Ready to be installed? >> Martin >From 37133bb09ddb23c87bb41ea6fffd5eba5af528fd Mon Sep 17 00:00:00 2001 From: marxin Date: Thu, 6 Oct 2016 17:52:45 +0200 Subject: [PATCH 4/5] Fold __builtin_memchr function gcc/ChangeLog: 2016-10-06 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 | 30 ++ gcc/gimple-fold.c | 84 ++- 4 files changed, 115 insertions(+), 59 deletions(-) diff --git a/gcc/builtins.c b/gcc/builtins.c index 6696f28..385e78e0 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); @@ -7244,47 +7243,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. */ @@ -8340,9 +8298,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_built