From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 8 Apr 93 16:18:39 GMT From: sampson@cod.nosc.mil (Charles H. Sampson) Subject: Re: and then Message-ID: <1993Apr8.161839.10069@nosc.mil> List-Id: In article <1993Apr7.173556.17184@sei.cmu.edu> ae@sei.cmu.edu (Arthur Evans) wr ites: >In connection with a discussion of Ada's short-circuit control forms, >sampson@nosc.mil (Charles H. Sampson) says > Ada compiler writers appear to have abdicated their responsibility > and make no attempt to determine when boolean operations can be > optimized as short-circuit. > >Well, I'm not surprised that they make no such attempt, but it's not >because they have abdicated any responsibility. ARM 4.5(5) says of >expressions involving 'and' and 'or': > The operands ... are evaluated in some order that is not defined by > the language (but before application of the corresponding operator). >In other words, both operands MUST be evaluated. That requirement is >not unreasonable given the availability of the short-circuit forms. The nature of optimization is to generate code other than the obvious _provided the semantics of the program are unchanged_. (Ada is one of the few languages to insist on that.) Furthermore, a general rule in compiler writing is "no harm, no foul". If a programmer writes if X > 1 and Y > 1 I find it hard to believe that any protest would be raised if it were dis- covered (by reading the generated code) that the compiler had optimized this as if X > 1 and then Y > 1 . A large percentage of boolean expressions that contain a binary opera- tor are of such simple forms and are worthy of optimization, in my opinion. When you look at something like if X > 1 and X * Y < 4 it gets more complicated because of the possibility that NUMERIC_ERROR might be raised. It appears to me that the letter of 11.6(4) would allow short-circuit in this case, but I'm not sure that that's the spirit of this paragraph. Whatever it might be, I would still initially write the above form and even write such things as if X > 1 and F(X) < 5 , which only a heroic optimizer could decide anything about, and change to short-circuit only if execution was unacceptable and tuning analysis showed that this statement was part of the problem. The last form clearly indi- cates to the maintainer that evaluation of F(X) is not dependent on X being larger than 1. If forced to change to short-circuit, I would add comments stating this non-dependency. Charlie P. S. I've just received information that the Verdix compilers now do such transformations when they can verify that the semantics are unchanged.