[cfe-dev] Compiler optimizing out calls to new()

via cfe-dev cfe-dev at lists.llvm.org
Mon Dec 3 17:44:26 PST 2018


Hi All,

A commit earlier this year, r325630, changed the behavior of the following program under -O1:

-----------------------------------------------------------------------------------
#include <new>
#include <stdlib.h>
extern "C" int printf(const char *, ...);
int UsedMyCode = 0;
void * operator new(std::size_t count)
{
    UsedMyCode = 1;
    return malloc(count);
}
int main(int argc, char * argv[])
{
    int* p = new int();
    *p = 10;
    printf("*p=%d.  UsedMyCode=%d\n", *p,  UsedMyCode);
    delete p;
    return 0;
}

The printed value of UsedMyCode became 0 after that checkin.

The compiler is eliminating the new/delete pair, printing just 10 for *p,  because  the usage
inside the function can be fully analyzed.

But I am wondering whether this is a valid optimization, because the implementation is not
calling the user supplied replacement as per 15.5.4.6. The complaint is that the side effects of
the user supplied replacement are not occurring.

The commit r325630 did not actually implement this optimization, but most likely it enabled
an already implemented optimization that was being foiled by something.

Any opinions?

Thanks

Sunil Srivastava
Sony Interactive Entertainment.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20181204/d2e60600/attachment.html>


More information about the cfe-dev mailing list