public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Toshi Morita <tm314159@yahoo.com>
To: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Possible missed optimization opportunity with const?
Date: Wed, 17 Aug 2016 00:25:00 -0000	[thread overview]
Message-ID: <1637972460.15325965.1471393300211.JavaMail.yahoo@mail.yahoo.com> (raw)
In-Reply-To: <1637972460.15325965.1471393300211.JavaMail.yahoo.ref@mail.yahoo.com>

I was involved in a discussion over the semantics of "const" in C, and the following code was posted: 

#include <stdio.h>
int foo = 0;
const int *pfoo = &foo;
void bar (void)
{
    foo +=3D;
}
int main(void)
{
   int a, b;
   a = *pfoo;
     bar();
     b = *pfoo;
   printf("a: %d, b: %d\n", a, b);
}
 

This code when compiled with gcc 4.8.2 using the optimization option -O3 produces: 

a: 0, b: 1 


So it appears even though pfoo is a const int *, the value *pfoo is read twice. 

Would it be valid for the code to print a:0, b: 0?
If so, is this a missed optimization opportunity?

Toshi 

       reply	other threads:[~2016-08-17  0:25 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 [this message]
2016-08-17 14:15   ` David Brown
     [not found]     ` <1646493700.16522102.1471473899633.JavaMail.yahoo@mail.yahoo.com>
2016-08-18 13:04       ` David Brown
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=1637972460.15325965.1471393300211.JavaMail.yahoo@mail.yahoo.com \
    --to=tm314159@yahoo.com \
    --cc=gcc@gcc.gnu.org \
    /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).