From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 1164f8,45b50f14dadfce5b X-Google-Attributes: gid1164f8,public X-Google-Thread: 102b75,45b50f14dadfce5b X-Google-Attributes: gid102b75,public X-Google-Thread: 110292,45b50f14dadfce5b X-Google-Attributes: gid110292,public From: ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe) Subject: Re: Final kick at the "Merced Guessing Game" Date: 1997/10/15 Message-ID: <621dsv$h7p$1@goanna.cs.rmit.edu.au>#1/1 X-Deja-AN: 280574126 References: <61g30t$o2n@news-rocq.inria.fr> <343C34A7.B715A348@tonys.ecology.umn.edu> <3440F805.C98BB347@tonys.ecology.umn.edu> <61tfaa$9fr@sis.cambridge.arm.com> <61uoiu$19e$1@goanna.cs.rmit.edu.au> <3443FAEF.EE62B663@powertelglobal.com> Organization: Comp Sci, RMIT University, Melbourne, Australia. NNTP-Posting-User: ok Newsgroups: comp.sys.intel,comp.arch,comp.sys.dec Shankar Unni writes: >Richard A. O'Keefe wrote: >> > bool = !(!f() | (!g() & !h())); >> >> In a word, NO. The C version absolutely requires all of the function >> calls to be performed. >First of all, this is true for "&" and "|", but not "&&" or "||". I'm >sure you knew that. (Does Fortran allow .AND. of INTEGER variables, or >only LOGICAL variables? If the latter, then "&" is a bad analog anyway). Yes, I know that. One of the points here is precisely that Fortran refuses to distinguish between & and &&. Another point is that Fortran applies exactly the same rule to *all* operators: if the compiler can determine that the *result* of some subcomputation is irrelevant to the value of the whole, it can delete that subcomputation entirely. Another point is that the C operators & and | were introduced in this example in a failed attempt to express the Fortran .AND. and .OR. operators. (Hence all the bangs, to make sure the operators only received 0/1 arguments.) What they do on general integer values is irrelevant to the argument. >Secondly, even for "&" (or "*", as in the 0*f() example) , it can elide >the call if the effect is the same whether it had or had not called the >function. I.e. if the compiler *knows* that the function has no side >effects (either by deduction, or by programmer intervention), it can >skip the call. But the compiler *can't* know that in many practical cases. (With a richer type system, e.g. with the LCL specifications that the LC-lint checker can enforce, where you _can_ pin down the possible side effects of a function you haven't seen yet, a C compiler _could_ know this, but of course the language isn't quite C any more.) The point here is that a Fortran compiler can *always* do this; it is *always* legal for a Fortran compiler to treat *any* function call as if it had no side effects > Thus, in > static int foo() { return 10; } > ... > for (i = 1; i <= 1000; i++) { > (void) 0*f(); > } >the compiler is allowed to boil down the entire loop into nothing. Sure. But just you try and move foo() into another file. >Other such optimizations are also allowed: if the function had been > static int foo() { return globalvariable = 10; } >then the entire loop can be boiled down into > globalvariable = 10; Again, so what? Fortran compilers have always been allowed to do inlining too. Nobody is arguing that inlining is a bad idea or that compilers should not or cannot optimise code they have *full information* about. >(The point is that yes, you can create contrived examples in which C is >not allowed to optimize what Fortran is, but sophisticated compiler >analysis can make up for a lot of these problems. Not a panacea, but >it's better than nothing.) No, the point is that sophisticated compiler analysis (read: not just interprocedural data flow analysis but cross *translation unit* data flow analysis) is *required*. Lest anybody mistake my attitude, let me state again that here I think Fortran and C are *both* wrong. C is wrong because it cannot optimise code that _is_ clean very well without going to a hell of a lot of work to find out that it is clean. Fortran is wrong because it _does_ optimise code on the _assumption_ that it is clean. What we really want are languages for which a compiler _can_ exploit the absence of aliasing, side effects, &c whenever it is safe to, needing only linear cost analysis of the source code to determine when it is safe. The Larch annotations for C strike me as a move in the right direction. Not only is there the potential of getting better optimisation if the compiler could know this stuff, LClint makes a _great_ debugging tool. -- John �neas Byron O'Keefe; 1921/02/04-1997/09/27; TLG,TLTA,BBTNOTL. Richard A. O'Keefe; RMIT Comp.Sci; http://www.cs.rmit.edu.au/%7Eok