[llvm-bugs] [Bug 51599] New: A mutable member inhibits constant propagation

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Aug 24 05:30:47 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51599

            Bug ID: 51599
           Summary: A mutable member inhibits constant propagation
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: pdimov at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Given the following code:

```
struct X
{
    int a;
    mutable int b;
};

inline constexpr X instance{ 1, 2 };

constexpr X const & get() noexcept
{
    return instance;
}

constexpr int f1( X const& x )
{
    return x.a;
}

int f2()
{
    return f1( get() );
}

int f3()
{
    constexpr auto r = f1( get() );
    return r;
}
```

`f3` correctly returns a constant because `f1( get() )` is forcibly evaluated
as a constant expression, but `f2` does not:

```
f2():                                 # @f2()
        mov     eax, dword ptr [rip + instance]
        ret
f3():                                 # @f3()
        mov     eax, 1
        ret
        .section        .data.instance,"aGw", at progbits,instance,comdat
instance:
        .long   1                               # 0x1
        .long   2                               # 0x2
```
(https://godbolt.org/z/Yvs4GK7Yr)
(GCC has no problems: https://godbolt.org/z/Mr5e863E3)

This is simplified from code using Boost.System 1.77, where the inability to
figure out that e.g. `system_category()` returns the system category causes
much worse codegen in situations such as `error_code(5, system_category())`.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210824/86f791df/attachment.html>


More information about the llvm-bugs mailing list