<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - clang will emit better code when memory is leaked"
   href="https://bugs.llvm.org/show_bug.cgi?id=39731">39731</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang will emit better code when memory is leaked
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>tiagomacarios@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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




<a href="https://godbolt.org/z/2oQ_Ou">https://godbolt.org/z/2oQ_Ou</a>

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;
}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>