Bug 24928 - static const objects should go to .rodata
Summary: static const objects should go to .rodata
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
: 84099 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-11-18 00:32 UTC by Mike Sharov
Modified: 2018-01-30 19:09 UTC (History)
2 users (show)

See Also:
Host: athlon-gnu-linux
Target: athlon-gnu-linux
Build: athlon-gnu-linux
Known to work:
Known to fail:
Last reconfirmed: 2006-02-20 18:28:25


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Sharov 2005-11-18 00:32:02 UTC
If a class has a trivial constructor that can be completely inlined and consists of simply initializing member variables, then static const instances of this class can be initialized at compile time and placed into .rodata. This would be the same behaviour that C has for static const structs and arrays. Example:

struct A {
public:
    inline A (int v) : m_v (v) {}
private:
    int m_v;
};

int main (void)
{
    static const A s_v (42);
    return (0);
}

Compiles to (with -fno-threadsafe-statics):

.globl main
        .type   main, @function
main:
.LFB5:
        cmpb    $0, _ZGVZ4mainE3s_v
        jne     .L2
        movl    $42, _ZZ4mainE3s_v
        movb    $1, _ZGVZ4mainE3s_v
.L2:
        xorl    %eax, %eax
        ret

With the object being initialized at runtime as if it mattered. Because the values of the member variables can not be changed after initialization, there is no reason to do this at runtime. Because the constructor doesn't do anything, this would not conflict with C++'s create-on-call mandate.

Because of this behaviour there is no way to create compiled-in const objects just like creating const structs or arrays in C.
Comment 1 Andrew Pinski 2005-11-18 00:46:37 UTC
There is another PR about this.
Comment 2 Andrew Pinski 2005-11-18 00:57:31 UTC
Confirmed, very related to PR 4131.

First this is not a trivial static const object as there is no trivial constructure here.
Comment 3 Gabriel Dos Reis 2005-11-18 01:31:58 UTC
Subject: Re:   New: Trivial static const objects should go to .rodata

"msharov at hotmail dot com" <gcc-bugzilla@gcc.gnu.org> writes:

| With the object being initialized at runtime as if it mattered. Because the
| values of the member variables can not be changed after initialization, there
| is no reason to do this at runtime. Because the constructor doesn't do
| anything, this would not conflict with C++'s create-on-call mandate.

Completely agreed.

-- Gaby
Comment 4 Andrew Pinski 2018-01-29 20:04:16 UTC
*** Bug 84099 has been marked as a duplicate of this bug. ***