https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119360
Bug ID: 119360 Summary: incomplete folding of initializers leads to dynamic initialization Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: nathan at gcc dot gnu.org Target Milestone: --- Created attachment 60812 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60812&action=edit reproducer compiling as g++ -O2 results in static initialization and folding. Compilng as g++ -O2 -DBUG results in dynamic init for BAR, but static init for FOO: _GLOBAL__sub_I__Z3foov: movq $16, _ZL3BAR(%rip) ret _Z3foov: movl 16, %eax ret _Z3barv: movq _ZL3BAR(%rip), %rax movl (%rax), %eax ret The problem is that we end up n initializer_constant_valid_p (varasm.cc), which I think expects fully folded expressions. When analyzing BAR's initializer it recurses into FOO's DECL_INITIAL, which is essentially (+ 16 (* 16 0)), and because of the multiplier thinks it's not suitable for a constant init. For some reason we don't make that mistake with FOO itself. I suspect we need to store a more folded initializer in DECL_INITIAL? The original code had the multiplier as 2, not zero BTW.