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]

optimizing wrapper functions



When writing code that loads dynamic modules, you might do something
like this:

int (*real_foo)(int, int);

int
foo(int a, int b)
{
  if(real_foo)
    return real_foo(a, b);

  try_to_load_real_foo();

  if(real_foo)
    return real_foo(a, b);

  return -1;
}

It would be nice if this could be optimized down to something like
this (or this but with a frame pointer):

foo:
	movl	real_foo, %eax
	testl	%eax, %eax
	jne	*%eax
	call	try_to_load_real_foo
	movl	real_foo, %eax
	testl	%eax, %eax
	jne	*%eax
	movl	$-1, %eax
	ret

Instead, I get this:

foo:
	pushl	%esi
	pushl	%ebx
	movl	real_foo,%eax
	movl	12(%esp),%esi
	movl	16(%esp),%ebx
	testl	%eax,%eax
	jne	call_it
	call	try_to_load_real_foo
	movl	real_foo,%eax
	testl	%eax,%eax
	je	fail
call_it:
	pushl	%ebx
	pushl	%esi
	call	*%eax
	addl	$8,%esp
	jmp	done
fail:
	movl $-1,%eax
done:
	popl %ebx
	popl %esi
	ret

I guess GCC would have to recognize that the called function had an
identical prototype to the wrapper, and was being called with the
wrapper's arguments, to pull this off.

zw


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