[PATCH] Fold __builtin_memchr (version 3)

Martin Liška mliska@suse.cz
Tue Oct 11 09:38:00 GMT 2016


On 10/07/2016 01:01 PM, Richard Biener wrote:
> On Fri, Oct 7, 2016 at 10:41 AM, Martin Liška <mliska@suse.cz> 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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0004-Fold-__builtin_memchr-function.patch
Type: text/x-patch
Size: 8405 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20161011/3a04ed7f/attachment.bin>


More information about the Gcc-patches mailing list