> > -----Original Message----- > From: gcc-ow...@gcc.gnu.org [mailto:gcc-ow...@gcc.gnu.org] On Behalf Of > Richard Guenther > Sent: Friday, December 02, 2011 9:35 AM > To: Andrew Haley > Cc: gcc@gcc.gnu.org > Subject: Re: volatile correctness: combine vs. target.md > > On Fri, Dec 2, 2011 at 1:59 PM, Andrew Haley <a...@redhat.com> wrote: >> On Fri, Dec 2, 2011 at 5:46 AM, <paul_kon...@dell.com> wrote: >>>> >>>> ... >>>>>>>> It's never correct to exchange volatile accesses. >>>>>> >>>>>> That's not true. volatile accesses to different memory locations >>>>>> have no special dependence. If it happens that GCC doesn't do >>>>>> this kind of things then this is only because most passes don't >>>>>> thouch volatile stmts at all (thus the reports for sub-optimal >>>>>> code with volatile - we don't even try to do legal transformations). >>>> >>>> I'm confused. Do you mean that in >>>> Volatile int *a, *b; >>>> Int j, k; >>>> J = *a; k = *b; >>>> it is ok to fetch *b before *a? I can't think of any device driver writer >>>> who would expect that. >> >> On 12/02/2011 12:27 PM, Richard Guenther wrote: >>> It's ok in terms of GCC internals. Whether it's ok in terms of the C >>> language specification I don't know (but I think it is ok). >> >> It's not. >> >> 5.1.2.3 Program execution >> >> Accessing a volatile object, modifying an object, modifying a file, or >> calling a function that does any of those operations are all side >> effects, which are changes in the state of the execution environment. >> Evaluation of an expression may produce side effects. At certain >> specified points in the execution sequence called sequence points, all >> side effects of previous evaluations shall be complete and no side >> effects of subsequent evaluations shall have taken place. (A summary >> of the sequence points is given in annex C.) >> >> ... >> >> The least requirements on a conforming implementation are: - At >> sequence points, volatile objects are stable in the sense that >> previous accesses are complete and subsequent accesses have not yet >> occurred. >> >> So, in here: >> >> Volatile int *a, *b; >> Int j, k; >> J = *a; k = *b; >> >> the fetch from *a must complete before the sequence point that is the >> semicolon. > > I see. As we do not explicitely model this dependency we probably get lucky > by the if (gimple_has_volatile_ops ()) bail-out; most passes do.
That sounds scary, if I understood you correctly. It sounds like GCC conforms to the standard only by accident rather than by design, which means that it might stop conforming to the standard by accident. Does this need cleanup -- for example, explicitly modeling the required sequencing so optimization passes are more likely to see that and get it right? For that matter, if "most" passes do, does that mean that we already have a bug if some pass that doesn't do this happens to get presented with a suitably constructed input? paul