This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/22278] gcc -O2 discards cast to volatile


------- Additional Comments From gdr at integrable-solutions dot net  2005-07-02 22:20 -------
Subject: Re:  gcc -O2 discards cast to volatile

"gcc2eran at tromer dot org" <gcc-bugzilla@gcc.gnu.org> writes:


| Prior versions of gcc did "respect" casts to pointer-to-volatile and did not
| optimize away the access. I've seen a lot of code that relies on that, and which
| would thus be broken by gcc 4.x (in a subtle and hard-to-debug way). One recent
| example is an elusive bug in Fedora Core 4's X.org package which bit many users
| (https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=161242).
| 
| I can't imagine a programmer casting into a pointer to volatile without really
| meaning it, so if the behavior is not defined by the standard then both
| compatibility and the principle of least astonishment seem to suggest reverting
| to the old behavior.


We must be careful in distinguishing between the following two
situations:

  (1)  volatile int foo = 0;
  
      *(volatile int*) (int*) (&foo);

  (2) int bar = 0;
      *(volatile int*) (&bar);

The former is well-formed and GCC should be careful.  In the specific
case at hand, the first cast may be passed to the function that is
subject of this PR, and GCC has no way of knowing what is going behind
the scene.  Consequently, it must make the conservative assumption.

The latter is just not well-founded based on the C standard.  It is a
QoI whether GCC should honor it or not.  I have no strong opinion
there, except saying that it is questionable.

-- Gaby


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22278


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]