Consider this trivial C++ test case:

#include <vector>
void foo(std::vector<int>* v, int i) { v->push_back(i); }

When I compile this with current trunk with -O2 on i686-pc-linux-gnu I see,
among other things, this:

        movl    4(%edi), %edx
        cmpl    8(%edi), %edx
        je      .L2
        testl   %edx, %edx
        je      .L3
        movl    -4(%edx), %eax
        movl    %eax, (%edx)
        movl    4(%edi), %edx
.L3:
        leal    4(%edx), %eax
        movl    %eax, 4(%edi)

Note that test for whether %edx is NULL.  That comes from this in the
.004t.gimple file:

  D.7266 = operator new (4, D.7265);
  D.7264 = (int *) D.7266;
  if (D.7264 != 0B)
    {
      try
        {
          D.7268 = *__val;
          *D.7264 = D.7268;
        }
      catch
        {
          operator delete (D.7264, D.7265);
        }
      iftmp.48 = D.7264;
    }
  else
    {
      iftmp.48 = D.7264;
    }

We get rid of the try/catch pretty quickly, but we never manage to get rid of
the NULL pointer check.

I believe the NULL pointer check is being generated by build_new_1 in cp/init.c
because we are using a type for which TYPE_NOTHROW_P is true.

This NULL pointer check serves no useful purpose.  It is essentially an
abtraction penalty.  We should figure out some way to get rid of it, either in
the C++ frontend, or in some later optimization pass.


-- 
           Summary: Useless NULL pointer check when constructing object
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ian at airs dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35878

Reply via email to