comp.lang.ada
 help / color / mirror / Atom feed
From: sampson@cod.nosc.mil  (Charles H. Sampson)
Subject: Re: and then
Date: 8 Apr 93 16:18:39 GMT	[thread overview]
Message-ID: <1993Apr8.161839.10069@nosc.mil> (raw)

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.

             reply	other threads:[~1993-04-08 16:18 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-04-08 16:18 Charles H. Sampson [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-04-12 18:38 and then Charles H. Sampson
1993-04-12 13:29 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!noc.n
1993-04-11  3:55 Michael Feldman
1993-04-10 19:52 Alex Blakemore
1993-04-10 15:43 Dik T. Winter
1993-04-10 15:36 Dik T. Winter
1993-04-10  9:39 munnari.oz.au!yoyo.aarnet.edu.au!news.adelaide.edu.au!usenet
1993-04-10  1:03 Charles H. Sampson
1993-04-09 18:08 Dave Bashford
1993-04-09 14:06 Dan Rittersdorf
1993-04-08 22:35 Andrew Dunstan,,2285592,
1993-04-08 22:28 Alex Blakemore
1993-04-08 19:03 Art Duncan
1993-04-08 15:34 Christopher J. Henrich
1993-04-08 12:21 enterpoop.mit.edu!usc!cs.utexas.edu!mars.tsd.arlut.utexas.edu!gardner
1993-04-07 22:58 Mark Lundquist
1993-04-07 21:07 Ray Harwood -- Data Basix: (602)721-1988
1993-04-07 16:21 Charles H. Sampson
1993-04-07 12:42 Robert Firth
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox