This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

What about this for a C++ optimisation?


How about this for a C++ optimisation? I know it's along lines discussed or
implemented before, but it's not currently supported...

Thinking along the lines of smart pointers, and the behind-the-scenes way in
which g++ passes function arguments and return values on i386 type targets,
I'd like for the pointer class to be able to be passed directly through a
function call and returned directly, rather than have the calling function
actually pass pointers to these values.

So what I suggest is:

 (1) Mark 'special' classes with an __attribute__ to indicate they should be
     handled in this way.

     Classes/structures so marked must conform to certain criteria:

     (a) The class must be able to fit in a single register (either an integer
	 register or an FPU register).

     (b) The class must only contain a single variable member (of non-bitfield
	 width) or it may contain a collection of bit fields.

     (c) The class must not have a virtual function table.

 (2) When passing such a class as an argument to a function:

     (a) The contents of the class are copied bitwise onto the stack (or
	 wherever the argument should be stored).

     (b) The copy constructor is _not_ called at this time.

     (c) [MAYBE] If the called function wishes to take the address of the
	 argument, then the a copy of the argument is taken through use of the
	 copy constructor first.

 (3) When passing such a class as a return value from a function:

     (a) The called function constructs the class as normal.

     (b) The called function then returns the value in a register.

     (c) If the calling function is going to use the value to construct a new
	 variable of the same type (copy constructor), then instead simply
	 bitwise paste the new value into the variable's storage.

     (d) If the calling function is going to return the value itself, then
	 just pass the value back without calling any methods on it.

     (e) If the calling function is going to assign to a variable of the same
	 type (operator=), then destruct the variable and then bitwise paste
	 the new value into the variable's storage.

     (f) If the calling function is going to discard the value, it should just
	 destruct it.

     (g) If the calling function is going to use the value in a temporary
	 capacity, it should just use it without copying, and destruct it when
	 it has finished with it.

     (h) So basically the value can be treated as being pre-constructed.

This trick can be used for more than just smart pointers of course... the
string class springs immediately to mind, as do iterator classes.

Cheers,
David Howells

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]