Bug 119360 - incomplete folding of initializers leads to dynamic initialization
Summary: incomplete folding of initializers leads to dynamic initialization
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.0
: P3 enhancement
Target Milestone: ---
Assignee: Drea Pinski
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2025-03-18 19:57 UTC by Nathan Sidwell
Modified: 2025-03-19 21:41 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2025-03-19 00:00:00


Attachments
reproducer (158 bytes, text/plain)
2025-03-18 19:57 UTC, Nathan Sidwell
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Sidwell 2025-03-18 19:57:12 UTC
Created attachment 60812 [details]
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.
Comment 1 Drea Pinski 2025-03-19 21:41:31 UTC
Confirmed. I have an idea on how to fix this, similar to PR 4131 .