public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Inlining questions
@ 2011-10-09  0:21 Ulf Magnusson
  0 siblings, 0 replies; only message in thread
From: Ulf Magnusson @ 2011-10-09  0:21 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-10-09  0:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-09  0:21 Inlining questions Ulf Magnusson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).