> -----Original Message-----
> From: Gcc <gcc-boun...@gcc.gnu.org> On Behalf Of Stefan Schulze
> Frielinghaus via Gcc
> Sent: 26 October 2020 09:58
> To: gcc@gcc.gnu.org
> Subject: Recognizing loop pattern
>
> I'm trying to detect loops of the form
>
> while (*x != y)
> ++x;
>
> which mimic the behaviour of function rawmemchr. Note, the size of *x is
> not
> necessarily one byte. Thus ultimately I would like to detect such loops and
> replace them with calls to builtins rawmemchr8, rawmemchr16,
> rawmemchr32 if
> they are implemented in the backend:
>
> x = __builtin_rawmemchr16(x, y);
>
> I'm wondering whether there is a particular place in order to detect such
> loop
> patterns. For example, in the loop distribution pass GCC recognizes loops
> which mimic the behavior of memset, memcpy, memmove and replaces
> them with
> calls to their corresponding builtins, respectively. The pass and in
> particular partitioning of statements depends on whether a statement is used
> outside of a partition or not. This works perfectly fine for loops which
> implement the mentioned mem* operations since their result is typically
> ignored. However, the result of a rawmemchr function is/should never be
> ignored. Therefore, such loops are currently recognized as having a
> reduction
> which makes an implementation into the loop distribution pass not straight
> forward to me.
>
> Are there other places where you would detect such loops? Any comments?
Not an expert on these, but GCC has some similar stuff in
tree-scalar-evolution.c for detecting popcount loops etc.
Thanks,
Kyrill
>
> Cheers,
> Stefan