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

            Bug ID: 115670
           Summary: missed optimization - anonymous structures
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico at kircheis dot it
  Target Milestone: ---

Consider following code snippet

~~~~
struct {
    int i = 42;
} const a;


auto bar(){
    return a;
}
~~~~

GCC optimizes the code to 

~~~~
bar():
        mov     eax, 42
        ret
~~~~

but the MSVC compiler can do even better even with /O0.
It seems that it does not generate any code for the function bar at all!


The root cause seems to be that if a function depends on an anonymous structure
(either the anonymous structure is part of the input parameters, like "auto
bar(decltype(a)&){return 1;}", or the return value), then the compiler
determines that if it is not used in the same translation unit, it cannot be
used from other translation units.

It would be nice if GCC would be able to do such optimization.


I know that -Wunused-function helps to find those functions (and it catches the
example I gave), but I need to delete them from the source code, which might
not be practical if integrating external/generated code.

Reply via email to