Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: C optimizer Keywords: C Ultrix Message-ID: <15967@mimsy.UUCP> Date: 15 Feb 89 22:56:34 GMT References: <515@larry.UUCP> <3684@arcturus> <1420@ncar.ucar.edu> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 105 In article <1420@ncar.ucar.edu> thor@stout.ucar.edu (Rich Neitzel) writes: >In addition, should one let the compiler do optimization? This may not >be important in most cases, but for others it can destroy the >functioning of the code. For example, many external devices have >control/status registers that are cleared only when read. This sort of thing is why `volatile' is in the pANS. I suppose I should put out another rerun: Path: mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: volatile: a summary Message-ID: <11837@mimsy.UUCP> Date: 7 Jun 88 06:58:00 GMT Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 87 With any luck, this will be my last article on this topic. READ THE WHOLE THING BEFORE YOU FLAME. What is `volatile'? Two things. It is a keyword in the draft proposed ANSI C standard, and it is an attribute of some kinds of variables. As an attribute, it can be applied to objects such as device registers or shared memory regions, but not to `ordinary memory'. The attribute basically means that there is a hidden entity that may either change values stored in what looks like ordinary memory, or take special actions when values are stored, or both. As a keyword, it can be applied in various places within declarations to label variables (including pointer targets) as having the volatile attribute. Is volatile necessary? This question must be broken down to parts before it can be answered. Is the concept necessary? Is the keyword necessary? Is the keyword useful? Is the keyword a `good thing'? Is the concept necessary? That is, is the `volatility attribute' a real thing? Not always. Many applications have no need for the concept. Programs that compute Fibonacci numbers, or that list files, or do any number of useful things may never care about it. It can be vital to others, such as device drivers. Is the keyword necessary? No. (As an existence proof, consider compilers that do not now have the keyword. Most of them work by not doing much in the way of optimisation. There is more to it, though.) A perfect compiler would know as much as all its programmers combined, and could use whatever rules those programmers would use to detect `real volatility'. Such a compiler would always know when a variable should have the volatility attribute. Of course, no perfect compilers exist, nor are any likely to be written in the foreseeable future. But good compilers do exist, and better ones are conceivable; a very good compiler could use external information to find most (but not all) variables that are certain *not* to have the attribute, and could then assume that all others do, and this would suffice. (Note that computing `real volatility' can be as hard as solving the halting problem. I claim only that the compiler can use the same rules that a programmer would use. If the compiler errs only on the side of caution---attaching `real volatility' where it is unnecessary---this is safe, if not perfectly optimal.) Given that the keyword is not absolutely necessary, is it of any use? Yes. If the keyword exists, compilers are free to assume that any variable not tagged by the keyword is truly not volatile. This is a very easy way to discover which variables should be considered to have the attribute, and, knowing which variables are not volatile, the compiler can make a number of optimisations that might otherwise be unsafe. This is not without its cost: With such a compiler, the programmer must correctly label any variables which do have the volatility attribute. But because the scheme is simple, compilers that use it can be faster than, and need not be as complex as, compilers that attempt to find `real volatility' with less help from the programmer. Being less complex, such compilers are more likely to be correct; being faster, they are more pleasant to use. Is the keyword a `good thing', even given a near-perfect compiler, one that can optimise correctly even without the keyword? I think it is. A certain amount of redundancy helps to avoid errors. A good-but-not-quite-perfect compiler might compute its set of volatile declarations, then compare it with the programmer's, and warn of any mismatch. This would help find errors in both the compiler's rules and the programmer's. Too much redundancy is obscuring, but real volatility is rare enough that I believe this will not be a problem. Summary of the summary: Is the concept necessary? Yes. Is the keyword necessary? No. Is the keyword useful? Yes. Is the keyword a `good thing'? Opinion: yes. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris