This is the mail archive of the
mailing list for the GCC project.
Re: k-byte memset/memcpy/strlen builtins
- From: Aaron Sawdey <acsawdey at linux dot vnet dot ibm dot com>
- To: Robin Dapp <rdapp at linux dot vnet dot ibm dot com>, gcc at gcc dot gnu dot org
- Date: Wed, 11 Jan 2017 12:05:03 -0600
- Subject: Re: k-byte memset/memcpy/strlen builtins
- Authentication-results: sourceware.org; auth=none
- References: <email@example.com>
On Wed, 2017-01-11 at 17:16 +0100, Robin Dapp wrote:
I thought I'd share some of what I've run into while doing similar
things for the rs6000 target.
First off, be aware that glibc does some macro expansion things to try
to handle 1/2/3 byte string operations in some cases.
Secondly, the way I approached this was to use the patterns
defined in optabs.def for these things:
OPTAB_D (cmpmem_optab, "cmpmem$a")
OPTAB_D (cmpstr_optab, "cmpstr$a")
OPTAB_D (cmpstrn_optab, "cmpstrn$a")
OPTAB_D (movmem_optab, "movmem$a")
OPTAB_D (setmem_optab, "setmem$a")
OPTAB_D (strlen_optab, "strlen$a")
If you define movmemsi, that should get used by expand_builtin_memcpy
for any memcpy call that it sees.
The constraints I was able to find when implementing cmpmemsi for
* don't compare past the given length (obviously)
* don't read past the given length
* except it's ok to do so if you can prove via alignment or
runtime check that you are not going to cause a pagefault.
Not crossing a 4k boundary seems to be generally viewed as
I would recommend looking at preprocessed code to make sure no funny
business is happening, and then look at your .md files. It looks to me
like s390 has got both movmem and strlen patterns there already.
If I understand correctly you are wanting to do multi-byte characters.
Seems to me you need to follow the path Richard Biener suggests and
make optab expansions that handle wider chars and then perhaps map
wcslen et. al. to them?
> For 1-byte memset/memcpy the builtin functions provide a
> way to achieve this. At first sight it seemed possible to extend
> tree-loop-distribution.c to include the additional variants we need.
> However, multibyte memsets/memcpys are not covered by the C standard
> I'm therefore unsure if such an approach is preferable or if there
> more idiomatic ways or places where to add the functionality.
> The same question goes for 2-byte strlen. I didn't see a recognition
> pattern for strlen (apart from optimizations due to known string
> in tree-ssa-strlen.c). Would it make sense to include strlen
> and subsequently handling for 2-byte strlen? The situation might of
> course more complicated than memset because of encodings etc. My
> in question used a fixed-length encoding of 2 bytes, however.
> Another simple idea to tackle this would be a peephole optimization
> I'm not sure if this is really feasible for something like memset.
> Wouldn't the peephole have to be recursive then?
Aaron Sawdey, Ph.D. firstname.lastname@example.org
050-2/C113 (507) 253-7520 home: 507/263-0782
IBM Linux Technology Center - PPC Toolchain