From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f0ba82308a594485 X-Google-Attributes: gid103376,public From: Nick Roberts Subject: Re: Right of Optimize Eager Evaluation Away Date: 1999/11/29 Message-ID: <38420578.D6692D2E@callnetuk.com>#1/1 X-Deja-AN: 554436782 Content-Transfer-Encoding: 7bit References: <863dtqfo26.fsf@ppp-173-146.villette.club-internet.fr> <861z9afhgd.fsf@ppp-173-146.villette.club-internet.fr> X-Original-NNTP-Posting-Host: da130d13.dialup.callnetuk.com X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Trace: 29 Nov 1999 15:02:21 GMT, da130d13.dialup.callnetuk.com Organization: Computer Consultant MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 1999-11-29T00:00:00+00:00 List-Id: Laurent Guerby wrote: > > Robert A Duff writes: > > Laurent Guerby writes: > > > The question is: is a smart Ada 95 compiler allowed to generate code > > > that looks like this: > > > > > > if C then > > > X := Super_Expensive_Function_Call_1; > > > else > > > X := Super_Expensive_Function_Call_2; > > > end if; > > > > > > that is to say, be lazy about its argument, and so might > > > save execution time. > > No. The function arguments may be evaluated in either order, so in case > > of exception, the other one might not happen. But without exceptions, > > both arguments will be evaluated. > > Is the potential raising of an exception the only language barrier > here (since as you mention the compiler is free to swap calls > defeating a class of side effects)? This seams easy to fix (via a > pragma), if one is interested in keeping the functional aspect of the > code instead of resorting to statements of course. I didn't receive Laurent's original post (poor UK feed), so - forgive me - I must guess at what he was originally suggesting, that a call such as P1(X,Y); where P1 was declared by procedure P1 (A: Boolean; B: T) is begin if A then P2; else P3(B); end if; end; would be optimised to evaluate X first, and omit the evaluation of Y if X turned out to be True. The RM95 says that a conforming implementation is allowed to execute an Ada program in any way which produces the required external effects [RM95 1.1.3(8-15)]. (Which is plain common sense, anyway, really.) This means that a compiler can perform any optimisation that doesn't change the external effects of a program. So the suggested optimisation is perfectly acceptable, provided the compiler can _guarantee_ that it will not change the external effects of the program. I get the impression that, in practice, there are not many Ada compilers which 'strongly' optimise (maybe there aren't any). However, I think a strongly optimising compiler (by today's standards) would be very likely, in effect, to produce the suggested optimisation, in the case of the subprogram P1 being expanded inline, or local and independent, and provided the compiler could be sure that the evaluation of Y could not have raised an exception or caused any external effect. -- Nick Roberts http://www.adapower.com/lab/adaos Always call for the professionals. (If they don't help, call for me ;-)