public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97976] New: Optimization regression in 10.1 for lambda passed as template argument
@ 2020-11-24 22:08 peter at int19h dot net
  2020-11-24 22:14 ` [Bug c++/97976] " pinskia at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: peter at int19h dot net @ 2020-11-24 22:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97976

            Bug ID: 97976
           Summary: Optimization regression in 10.1 for lambda passed as
                    template argument
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: peter at int19h dot net
  Target Milestone: ---

The following C++11 and above code:

////////////////////
extern const int* data;

template<typename T> bool func(T callback)
{
    for (const int* pi = data; pi; ++pi)
    {
        if (callback(*pi))
        {
            return false;
        }
    }
    return true;
}

bool f0(int i)
{
    return func([i](const int j){ return i == j; });
}
////////////////////


With GCC 10.1 with "-std=c++11 -O2" flags generates the following:
////////////////////
f0(int):
        cmp     QWORD PTR data[rip], 0
        sete    al
        ret
////////////////////

While GCC 9.3 with the same command line flags generates the following:
////////////////////
f0(int):
        mov     rax, QWORD PTR data[rip]
        test    rax, rax
        jne     .L3
        jmp     .L4
.L7:
        add     rax, 4
.L3:
        cmp     edi, DWORD PTR [rax]
        jne     .L7
        xor     eax, eax
        ret
.L4:
        mov     eax, 1
        ret
////////////////////

It looks like this regression started with GCC 10 and starts at -02
optimization level for C++11 and above. I have tested this with clang and msvc,
and they generate code similar to what is generated by gcc 9.3.

This behavior can also be seen in the Compiler Explorer here:

https://godbolt.org/z/r4zMnc

Thank you!
--peter

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-11-26 10:09 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-24 22:08 [Bug c++/97976] New: Optimization regression in 10.1 for lambda passed as template argument peter at int19h dot net
2020-11-24 22:14 ` [Bug c++/97976] " pinskia at gcc dot gnu.org
2020-11-24 22:24 ` peter at int19h dot net
2020-11-25  4:40 ` peter at int19h dot net
2020-11-25  8:25 ` [Bug c++/97976] Optimization relating to NULL pointer assumptions in gcc 9.1 rguenth at gcc dot gnu.org
2020-11-25 16:43 ` peter at int19h dot net
2020-11-25 18:18 ` redi at gcc dot gnu.org
2020-11-25 19:46 ` peter at int19h dot net
2020-11-25 23:55 ` redi at gcc dot gnu.org
2020-11-26  0:20 ` redi at gcc dot gnu.org
2020-11-26  1:37 ` peter at int19h dot net
2020-11-26 10:08 ` redi at gcc dot gnu.org
2020-11-26 10:09 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).