[llvm-bugs] [Bug 39731] New: clang will emit better code when memory is leaked

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Nov 20 12:25:02 PST 2018


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

            Bug ID: 39731
           Summary: clang will emit better code when memory is leaked
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: tiagomacarios 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

With the code below clang will generate better code when the source
conceptually leaks memory than when it does not.

clang -std=c++2a -O3
main: # @main
  mov eax, 2
  ret


-std=c++2a -O3 -DNOLEAK
main:                                   # @main
        push    r14
        push    rbx
        push    rax
        mov     edi, 24
        call    operator new(unsigned long)
        mov     r14, rax
        mov     dword ptr [rax], 0
        xorps   xmm0, xmm0
        movups  xmmword ptr [rax + 8], xmm0
        mov     edi, 24
        call    operator new(unsigned long)
        mov     rbx, rax
        mov     dword ptr [rax], 1
        xorps   xmm0, xmm0
        movups  xmmword ptr [rax + 8], xmm0
        mov     qword ptr [r14 + 8], rax
        mov     edi, 24
        call    operator new(unsigned long)
        mov     qword ptr [rbx + 16], rax
        mov     rdi, rax
        call    operator delete(void*)
        mov     rdi, qword ptr [r14 + 8]
        test    rdi, rdi
        je      .LBB0_2
        call    operator delete(void*)
.LBB0_2:
        mov     rdi, r14
        call    operator delete(void*)
        mov     eax, 2
        add     rsp, 8
        pop     rbx
        pop     r14
        ret




https://godbolt.org/z/2oQ_Ou

namespace {
struct Node {
    int value{};
    Node* left{};
    Node* right{};
    constexpr Node(int i=0) noexcept : value(i) {};
};

auto constexpr left = &Node::left;
auto constexpr right = &Node::right;

template<typename T, typename... TP>
constexpr Node* traverse(T np, TP... paths) noexcept
{
    return (np ->* ... ->* paths);
}
}
int main()
{
    Node* const root = new Node{0};
    root->left = new Node{1};
    root->left->right = new Node{2};

    const int val = traverse(root, left, right)->value;
#if defined(NOLEAK)
    delete root->left->right;
    delete root->left;
    delete root;
#endif
    return val;
}

-- 
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/20181120/4dd64f24/attachment.html>


More information about the llvm-bugs mailing list