Re: GNU C keyword volatile, GNU compiler/optimizer and instruction re-ordering

"Nick Maclaren" <nmm1@cus.cam.ac.uk>
20 Sep 2002 02:38:56 -0400

          From comp.compilers

Related articles
GNU C keyword volatile, GNU compiler/optimizer and instruction re-or Arun@Winphoria.Com (Arun) (2002-09-19)
Re: GNU C keyword volatile, GNU compiler/optimizer and instruction nmm1@cus.cam.ac.uk (Nick Maclaren) (2002-09-20)
Re: GNU C keyword volatile, GNU compiler/optimizer and instruction n82117268.ch@chch.demon.co.uk (Charles Bryant) (2002-09-22)
Re: GNU C keyword volatile, GNU compiler/optimizer and instruction re- mrs@kithrup.com (Mike Stump) (2002-09-29)
Re: GNU C keyword volatile, GNU compiler/optimizer and instruction re- fjh@cs.mu.OZ.AU (Fergus Henderson) (2002-10-13)
Re: GNU C keyword volatile, GNU compiler/optimizer and instruction r nmm1@cus.cam.ac.uk (Nick Maclaren) (2002-10-13)
Re: GNU C keyword volatile, GNU compiler/optimizer and instruction r toon@moene.indiv.nluug.nl (Toon Moene) (2002-10-13)
Re: Fortran volatile, was GNU C keyword volatile nmm1@cus.cam.ac.uk (Nick Maclaren) (2002-10-18)
[1 later articles]
| List of all articles for this month |
From: "Nick Maclaren" <nmm1@cus.cam.ac.uk>
Newsgroups: comp.compilers
Date: 20 Sep 2002 02:38:56 -0400
Organization: University of Cambridge, England
References: 02-09-118
Keywords: optimize, C
Posted-Date: 20 Sep 2002 02:38:56 EDT

Arun <Arun@Winphoria.Com> wrote:
>Now, we have a C code as follows:
>
> some_function() {
> volatile int *ptr1 = 0x60000000;
> volatile int *ptr2 = 0x60000004;
>
>Question is: can a C compiler/optimizer, specifically GNU C, re-order
>the assignment of *ptr1 and val? In other words, can variable val get
>assigned before *ptr1?
>
>In the same context, can processor pipelines in general re-order the
>instructions? Interesting cases may be the Intel's Pentium series and
>Motorola PowerPC. What if possible/any, are the hints by the
>compiler/optimizer to the processor?
>
>[The standard says that the generated code has to make all of the
>references to volatile data, in the order the program makes them.
>It also says that only lvalues can be volatile, so there's no such
>thing as a volatile function. -John]


Unfortunately, volatile is a complete mess. It was intended to cover
TWO purposes, one of which was such special memory locations and the
other of which relates to exception handling, longjmp etc. As with
many such compromises, it fails on both.


To a first approximation, the handling of volatile is implementation
dependent, and the only thing that is REQUIRED to be specified is
what constitutes and access to a volatile qualified object. Which
is not the issue here. Beyond that, it is the programmer's problem
to find out what, if anything, volatile means.


The reason for this is that ALL of the cases where volatile is
specified in the standard to affect the ordering can be detected ONLY
by a program that invokes undefined behaviour! Yes, really :-(


For example, in your code, the result of converting the integers to
pointers is implementation defined, but the use of any particular
integer value is not. So your program is outside the standard, and
the standard says nothing about what it will do.


So, the executive summary is to look in the gcc implementation notes
and see what, if anything, they say about this. They are not required
to specify the behaviour, and it is then undefined. But they may say
something. You can also look at the code and see what the current
version does, but there is no guarantee that the next one will do the
same thing.


Sorry.


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email: nmm1@cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.