Bug 82845 - -ftree-loop-distribute-patterns creates recursive loops on function called "memset"
Summary: -ftree-loop-distribute-patterns creates recursive loops on function called "m...
Status: RESOLVED DUPLICATE of bug 56888
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2017-11-05 16:24 UTC by David Brown
Modified: 2017-11-05 20:51 UTC (History)
0 users

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description David Brown 2017-11-05 16:24:42 UTC
(I have tested this using several gcc versions, including x86_64 and arm ports, up to an 8.0.0 trunk test version.)

The -ftree-loop-distribute-patterns optimisation spots constructs that look like memset, memcpy, etc., calls - and generates library calls to these functions to give fast code using the library routines rather than generating large amounts of inline code (so that the set or copy operations can be done in lumps larger than one byte at a time).

But if you have have your own implementation of one of these functions (as may be done for tracing calls, or in some embedded systems - or simply because you are writing a C library), the call does not go to the library version - it will go to the new local definition of the function.  For example, given this simple memset implementation:

void *memset(void *s, int c, size_t n)
    char *p = s;    
    while (n--) *p++ = c;
    return s;

gcc on x86_64 with -O2 gives simple, solid code:

        testq   %rdx, %rdx
        movq    %rdi, %rax
        je      .L6
        addq    %rdi, %rdx
        movq    %rdi, %rcx
        addq    $1, %rcx
        movb    %sil, -1(%rcx)
        cmpq    %rdx, %rcx
        jne     .L3
        rep ret

With -O3, which enables -ftree-loop-distribute-patterns, it gives:

        testq   %rdx, %rdx
        je      .L6
        subq    $8, %rsp
        movsbl  %sil, %esi
        call    memset
        addq    $8, %rsp
        movq    %rdi, %rax

gcc spots that the loop is like an memset, and replaces it with a call to memset.  But now that leads to infinite recursion.
Comment 1 Marc Glisse 2017-11-05 16:59:05 UTC
Richard's patch seems to have been forgotten :-(

*** This bug has been marked as a duplicate of bug 56888 ***
Comment 2 David Brown 2017-11-05 17:11:05 UTC
(In reply to Marc Glisse from comment #1)
> Richard's patch seems to have been forgotten :-(
> *** This bug has been marked as a duplicate of bug 56888 ***

Sorry for the noise.  (Unless it encourages the fix from bug 56888 to be implemented!)
Comment 3 Marc Glisse 2017-11-05 20:51:58 UTC
Please don't touch the status field, I marked it as "duplicate" pointing to the other PR, that's more useful than "fixed" (which is false).

Indeed we can hope that it will serve as a reminder for people working on PR 56888.

*** This bug has been marked as a duplicate of bug 56888 ***