[Bug c++/17422] New: Duplicate work, missed optimizations for registration of static destructors

zack at gcc dot gnu dot org gcc-bugzilla@gcc.gnu.org
Sat Sep 11 20:07:00 GMT 2004


register_dtor_fn and the functions it calls do a lot of duplicate work.
For non-array objects, the code that eventually gets generated is
relatively simple ... the equivalent of

T object;

extern "C" {
  static void __tcf_17 (void)
  { T::~T(&object); }  // complete object destructor
};

somewhere() { 
  ...
  T::T(&object);
  atexit(__tcf_17);  // or maybe __cxa_atexit(__tcf_17, 0, __dso_handle)
  ...
}

where 17 is a sequence number.  (I'm ignoring here all the stuff for
making sure object gets constructed only once, doesn't get destructed
if the constructor failed, etc.  register_dtor_fn doesn't do any of that.)

Most of the problem is that we use build_cleanup to
fabricate the body of __tcf_17, which does all kinds of work to verify
that the destructor is accessible from somewhere().  Which is
necessary, but then we have to throw away the tree it generates and do
it over again with access checks disabled, because (the comment says)
otherwise we might take SAVE_EXPRs referring to somewhere() and try to
compile them in the context of __tcf_17.

If I didn't have to use build_cleanup here - if I could, say, call
some routine which would do all the access checks and hand me the
FUNCTION_DECL for T's complete object destructor - then I could have
register_dtor_fn bypass all of the C++ front end's function-construction
framework, which would avoid the interface_* mess.  Also, I could do
an optimization hinted at in comments: if we've got __cxa_atexit, we
can skip generating __tcf_17 altogether and just synthesize a call

  __cxa_atexit (T::~T, &object, __dso_handle);

Array delete is harder, but I could implement a variant of
build_vec_delete_1 which took the FUNCTION_DECL of the destructor to
call, rather than looking it up itself.  In fact, that would be a
compile-speed win in itself; currently that function calls
build_delete for every array element, so the destructor gets looked up
every single time.

-- 
           Summary: Duplicate work, missed optimizations for registration of
                    static destructors
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P2
         Component: c++
        AssignedTo: zack at gcc dot gnu dot org
        ReportedBy: zack at gcc dot gnu dot org
                CC: gcc-bugs at gcc dot gnu dot org


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



More information about the Gcc-bugs mailing list