[Bug c++/88118] New: GCC keeps unnecessary calls to new

tiagomacarios at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Nov 20 17:51:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88118

            Bug ID: 88118
           Summary: GCC keeps unnecessary calls to new
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tiagomacarios at gmail dot com
  Target Milestone: ---

With the code below clang generates:

main: # @main
  mov eax, 2
  ret

GCC will get to the correct result, but will keep the new calls:

main:
        sub     rsp, 8
        mov     edi, 24
        call    operator new(unsigned long)
        mov     edi, 24
        call    operator new(unsigned long)
        mov     edi, 24
        call    operator new(unsigned long)
        mov     eax, 2
        add     rsp, 8
        ret


https://godbolt.org/z/Q3nV2x

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};

    Node* const node = traverse(root, left, right);
    return node->value;
}


More information about the Gcc-bugs mailing list