public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Joe Buck <jbuck@Synopsys.COM>
To: pderbysh@usa.net (Paul Derbyshire)
Cc: djgpp@delorie.com, egcs@egcs.cygnus.com
Subject: Re: Code gen question
Date: Fri, 12 Feb 1999 15:29:00 -0000	[thread overview]
Message-ID: <199902122328.PAA12101@atrus.synopsys.com> (raw)
In-Reply-To: < 3.0.6.32.19990212180551.00841100@pop.netaddress.com >; from "Paul Derbyshire" at Feb 12, 99 6:05 pm

> Which will cause cc1plus to generate better code?
> 
> inline int myclass::myfunc (int j) { return j*j*j; }
> 
> 
> inline int myclass::myfunc (const int &j) { return j*j*j; }

It depends on the code at the call site, and whether the object passed
to myfunc is in a register or in memory, plus whether the compiler can
optimize away unneeded write-to-memory, read-back-from-memory code.

In many cases, you'll get the exact same code for either of the above
two functions.  In other cases, either one or the other turns out a
bit better, usually because of some missed optimizaton.

If you really want to know, use -S and look at the assembly.

> My guess would be the latter, since the latter when inlined won't make a
> copy of the argument passed.

In principle the two should come out about the same, now that we have
the ADDRESSOF optimization.  Before we had ADDRESSOF the first one
was always better on most processors, since j gets passed in a register
while the second one forces j to be in memory.

> However, it might be that at high -O settings
> cc1plus will spot that the first version doesn't modify j and silently
> compile it like the second version.

That would be bad: what if j is already in a register?  Why would you want
to force it to memory?

> This leads me to ask: when writing short inline functions, is it better for
> code optimization to pass builtin data types (bool, int, double, etc.) and
> pointers by value or by reference?

If it fits in a register, use by-value, though in many cases the
difference is not significant.

WARNING: multiple messages have this Message-ID
From: Joe Buck <jbuck@Synopsys.COM>
To: pderbysh@usa.net (Paul Derbyshire)
Cc: djgpp@delorie.com, egcs@egcs.cygnus.com
Subject: Re: Code gen question
Date: Sun, 28 Feb 1999 22:53:00 -0000	[thread overview]
Message-ID: <199902122328.PAA12101@atrus.synopsys.com> (raw)
Message-ID: <19990228225300.yAK4MYjXHSz6p8h9J9XFFlWVSmL7g8-zW3KyWc9IKgg@z> (raw)
In-Reply-To: <3.0.6.32.19990212180551.00841100@pop.netaddress.com>

> Which will cause cc1plus to generate better code?
> 
> inline int myclass::myfunc (int j) { return j*j*j; }
> 
> 
> inline int myclass::myfunc (const int &j) { return j*j*j; }

It depends on the code at the call site, and whether the object passed
to myfunc is in a register or in memory, plus whether the compiler can
optimize away unneeded write-to-memory, read-back-from-memory code.

In many cases, you'll get the exact same code for either of the above
two functions.  In other cases, either one or the other turns out a
bit better, usually because of some missed optimizaton.

If you really want to know, use -S and look at the assembly.

> My guess would be the latter, since the latter when inlined won't make a
> copy of the argument passed.

In principle the two should come out about the same, now that we have
the ADDRESSOF optimization.  Before we had ADDRESSOF the first one
was always better on most processors, since j gets passed in a register
while the second one forces j to be in memory.

> However, it might be that at high -O settings
> cc1plus will spot that the first version doesn't modify j and silently
> compile it like the second version.

That would be bad: what if j is already in a register?  Why would you want
to force it to memory?

> This leads me to ask: when writing short inline functions, is it better for
> code optimization to pass builtin data types (bool, int, double, etc.) and
> pointers by value or by reference?

If it fits in a register, use by-value, though in many cases the
difference is not significant.


  parent reply	other threads:[~1999-02-12 15:29 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-12 15:06 Paul Derbyshire
     [not found] ` < 3.0.6.32.19990212180551.00841100@pop.netaddress.com >
1999-02-12 15:29   ` Joe Buck [this message]
1999-02-28 22:53     ` Joe Buck
1999-02-28 22:53 ` Paul Derbyshire
     [not found] <pderbysh@usa.net's>
     [not found] ` <message>
     [not found]   ` <of>
     [not found]     ` <12>
     [not found]       ` <Feb>
     [not found]         ` <1999>
     [not found]           ` <15:07:44>
     [not found]             ` <-0800>
     [not found]               ` <3.0.6.32.19990212180551.00841100.cygnus.egcs@pop.netaddress.com>
1999-02-12 15:29                 ` Jason Merrill
     [not found]                   ` < u990e3kxq8.fsf@yorick.cygnus.com >
1999-02-12 17:34                     ` Paul Derbyshire
     [not found]                       ` < 3.0.6.32.19990212203311.0083e6d0@pop.netaddress.com >
1999-02-12 17:39                         ` Jeffrey A Law
1999-02-28 22:53                           ` Jeffrey A Law
1999-02-28 22:53                       ` Paul Derbyshire
1999-02-28 22:53                   ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=199902122328.PAA12101@atrus.synopsys.com \
    --to=jbuck@synopsys.com \
    --cc=djgpp@delorie.com \
    --cc=egcs@egcs.cygnus.com \
    --cc=pderbysh@usa.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).