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

            Bug ID: 125331
           Summary: recursively-instantiated template with `always_inline`
                    attribute generated large amount of `nop` instructions
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vittorio.romeo at outlook dot com
  Target Milestone: ---

Code:

    template<int N>
    [[gnu::always_inline]]
    inline int f() {
        if constexpr (N == 0) {
            return 42;
        } else {
            return f<N-1>();
        }
    }

    int main() {
        return f<100>();
    }

With either g++ trunk or g++ 16.1, using `-std=c++26 -O0`, the generated
assembly looks like this:

        "main":
                push    rbp
                mov     rbp, rsp
                mov     eax, 42
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                nop
                // ... ~80 more `nop`s...

Adding `-fcompare-elim` to the compiler flags removes the extra `nop`s. 

Compiler Explorer:
https://godbolt.org/z/qsEMd533v

Reply via email to