public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Brown <david.brown@hesbynett.no>
To: Toshi Morita <tm314159@yahoo.com>,  "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: Possible missed optimization opportunity with const?
Date: Thu, 18 Aug 2016 13:04:00 -0000	[thread overview]
Message-ID: <57B5B253.5070708@hesbynett.no> (raw)
In-Reply-To: <1646493700.16522102.1471473899633.JavaMail.yahoo@mail.yahoo.com>

On 18/08/16 00:44, Toshi Morita wrote:
> David Brown <david.brown@hesbynett.no> wrote:
> 
>> No, it would not be valid.  Declaring pfoo as a "const int*" tells the
>> compiler "I will not change anything via this pointer - and you can
>> optimise based on that promise".  It does /not/ tell the compiler "the
>> thing that this points to will not change".
>>
>> So the compiler is correct in reading *pfoo twice.
> 
> The revised example posted by Kei uses "const int const *pfoo" and GCC
> is able to remove the second read, so this interpretation of const seems
> incorrect?
> 
> Toshi
> 

I didn't see the post you are referring to - was it sent to the mailing
list, or only your email address?

But if I can make a guess here, the difference here is that now the
pointer object "pfoo" itself is const, and therefore cannot be modified
(without causing undefined behaviour).  So the compiler knows that it
will definitely point to "foo", and can use that information to optimise
better.

When "pfoo" was not "const", the compiler does not know that pfoo points
to foo in main - it could point somewhere else.  (In particular, a
file-scope constructor in another module might change it, since pfoo has
external linkage.)  Thus it does not know if bar() changes *pfoo, and it
has to read *pfoo twice.  You would get the same effect by making pfoo
"static", since the compiler then knows that it's value is &foo at the
start of main().


  parent reply	other threads:[~2016-08-18 13:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1637972460.15325965.1471393300211.JavaMail.yahoo.ref@mail.yahoo.com>
2016-08-17  0:25 ` Toshi Morita
2016-08-17 14:15   ` David Brown
     [not found]     ` <1646493700.16522102.1471473899633.JavaMail.yahoo@mail.yahoo.com>
2016-08-18 13:04       ` David Brown [this message]
2016-08-17 14:27   ` lhmouse

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=57B5B253.5070708@hesbynett.no \
    --to=david.brown@hesbynett.no \
    --cc=gcc@gcc.gnu.org \
    --cc=tm314159@yahoo.com \
    /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).