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

            Bug ID: 117370
           Summary: std::nothrow variants of operator new are not
                    optimized away when block is unused
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hubicka at gcc dot gnu.org
  Target Milestone: ---

#include <new>
void test1()
{
        int *a = new int;
        delete(a);
}
void test()
{
        int *a = new (std::nothrow) int;
        delete(a);
}
void test2()
{
        int *a = new (std::nothrow) int;
        if (!a)
                __builtin_abort ();
        delete(a);
}


compiles to:

;; Function test1 (_Z5test1v, funcdef_no=17, decl_uid=3143, cgraph_uid=17,
symbol_order=18)

void test1 ()
{
  <bb 2> [local count: 1073741824]:
  return;

}



;; Function test (_Z4testv, funcdef_no=18, decl_uid=3147, cgraph_uid=18,
symbol_order=19)

Removing basic block 5
void test ()
{
  int * a;

  <bb 2> [local count: 1073741824]:
  a_4 = operator new (4, &nothrow);
  if (a_4 != 0B)
    goto <bb 3>; [99.96%]
  else
    goto <bb 4>; [0.04%]

  <bb 3> [local count: 1073312328]:
  *a_4 ={v} {CLOBBER(eob)};
  operator delete (a_4, 4); [tail call]

  <bb 4> [local count: 1073741824]:
  return;

}



;; Function test2 (_Z5test2v, funcdef_no=19, decl_uid=3151, cgraph_uid=19,
symbol_order=20)

void test2 ()
{
  int * a;

  <bb 2> [local count: 1073741824]:
  a_3 = operator new (4, &nothrow);
  if (a_3 == 0B)
    goto <bb 3>; [0.00%]
  else
    goto <bb 4>; [100.00%]

  <bb 3> [count: 0]:
  __builtin_abort ();

  <bb 4> [local count: 1073741824]:
  *a_3 ={v} {CLOBBER(eob)};
  operator delete (a_3, 4); [tail call]
  return;

}


so only first variant is optimized out. Clang optimizes away all three
new/delete pairs

Reply via email to