Inlining questions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I noticed that GCC 4.5.2 tends to generate smaller/better code when I
return a result from an inline function by reference (to something
non-local, obviously) rather than by value, even if the result is only
used as an rvalue.

Take the following for example:


int *a;

static inline int/*&*/ inl(int x) {
    return x == 1 ? a[7] : a[10];
}

int foo(int x) { return inl(x); }


With -O3, return-by-reference generates the following:

00000000 <_Z3fooi>:
   0:	83 7c 24 04 01       	cmpl   $0x1,0x4(%esp)
   5:	74 09                	je     10 <_Z3fooi+0x10>
   7:	a1 00 00 00 00       	mov    0x0,%eax
   c:	8b 40 28             	mov    0x28(%eax),%eax
   f:	c3                   	ret
  10:	a1 00 00 00 00       	mov    0x0,%eax
  15:	8b 40 1c             	mov    0x1c(%eax),%eax
  18:	c3                   	ret

Return by value generates the following:

00000000 <_Z3fooi>:
   0:	83 7c 24 04 01       	cmpl   $0x1,0x4(%esp)
   5:	74 11                	je     18 <_Z3fooi+0x18>
   7:	a1 00 00 00 00       	mov    0x0,%eax
   c:	83 c0 28             	add    $0x28,%eax
   f:	8b 00                	mov    (%eax),%eax
  11:	c3                   	ret
  12:	8d b6 00 00 00 00    	lea    0x0(%esi),%esi
  18:	a1 00 00 00 00       	mov    0x0,%eax
  1d:	83 c0 1c             	add    $0x1c,%eax
  20:	8b 00                	mov    (%eax),%eax
  22:	c3                   	ret


The difference becomes even bigger if you add conditionals or nest
calls to the inline function.

This leads to the following question:

In situations where both return-by-reference and return-by-value would
work, does the optimizer tend to do a better job with the former?

Though slightly unrelated, I'm also curious to what degree inline
functions really are equivalent to c-style macro "inlines"
performance-wise in GCC.

/Ulf


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux